Use the error code as a routing clue first: Cloudflare 520 usually means Cloudflare reached your origin server but received an unexpected or invalid response, while HTTP 502 means a gateway or proxy received a bad response from an upstream server. For diagnosis, treat 520 as a Cloudflare to origin mystery response and 502 as a gateway to upstream failure. That distinction prevents wasted time and points you toward the right logs first.
TLDR: A Cloudflare 520 is not a standard HTTP status code; it is Cloudflare’s way of saying the origin server returned something it could not process. A 502 Bad Gateway is a standard HTTP error and usually points to a proxy, load balancer, or upstream service problem. For example, if 8% of checkout requests fail only when traffic passes through Cloudflare, but direct origin tests return normal responses, inspect Cloudflare edge logs, response headers, and origin connection limits. If the same failures appear through Nginx, HAProxy, or an application gateway, start with upstream app health and proxy logs.
- What Error 520 Means
- What HTTP 502 Means
- Cloudflare 520 vs HTTP 502: The Practical Difference
- Fast Diagnosis Checklist for Cloudflare 520
- Fast Diagnosis Checklist for HTTP 502
- How to Tell Which Layer Is Failing
- Common Misreads That Slow Teams Down
- Best Practices to Reduce Both Errors
- Final Recommendation
What Error 520 Means
Error 520 is Cloudflare’s generic “unknown error” for cases where the origin server gives Cloudflare a response that does not fit expected behavior. The browser may show a page reading “Web server is returning an unknown error”. That wording is vague, and honestly, it feels like the error equivalent of a shrug.
Common causes include:
- Empty response from the origin, where the connection opens but no usable HTTP response arrives.
- Malformed headers, such as broken syntax or oversized header values.
- Origin server crashes during request handling.
- Firewall or security software blocking Cloudflare IP ranges.
- Connection resets between Cloudflare and the server.
- Resource exhaustion, such as maxed-out PHP workers, Apache processes, or database connections.
A 520 often appears during traffic spikes, plugin failures, bad deployments, or backend timeouts that produce invalid responses instead of clean errors.
What HTTP 502 Means
HTTP 502 Bad Gateway is a standard status code defined for gateway and proxy failures. It means one server acted as a gateway and received an invalid response from another server upstream.
You may see 502 from:
- Nginx proxying to PHP FPM, Node.js, Python, or another backend.
- Apache forwarding requests to an application server.
- Load balancers sending traffic to unhealthy backends.
- API gateways waiting on slow or broken services.
- Cloud platforms where the app container is down or overloaded.
The key difference is scope. A 502 tells you that a gateway layer received a bad upstream response. A 520 tells you that Cloudflare received something odd from your origin and could not map it to a normal status code.
Cloudflare 520 vs HTTP 502: The Practical Difference
| Error | Source | Main Meaning | First Logs to Check |
|---|---|---|---|
| 520 | Cloudflare | Origin gave Cloudflare an unexpected response | Cloudflare events, origin access logs, web server error logs |
| 502 | Gateway or proxy | Gateway received a bad upstream response | Proxy logs, upstream service logs, application logs |
The catch is that both errors can look similar to users. Both may show a failed page, broken checkout, or dead API call. Yet the investigative path is different. If you start with the wrong layer, expect to waste 20 to 40 minutes checking healthy systems while the real failure keeps repeating.
Fast Diagnosis Checklist for Cloudflare 520
Start with direct origin testing. Temporarily bypass Cloudflare by testing the origin IP with a proper Host header:
curl -I https://origin-ip-address -H "Host: example.com"
If direct origin access works but Cloudflare traffic fails, inspect the connection between Cloudflare and your server.
- Check origin web server logs at the exact timestamp of the error.
- Confirm Cloudflare IP ranges are allowed in firewalls, WAF rules, and security plugins.
- Look for oversized headers, especially from cookies, authentication tokens, or debugging tools.
- Review recent deployments, plugin updates, or server config changes.
- Check process limits for PHP FPM, Apache workers, Node processes, or database pools.
- Test TLS compatibility between Cloudflare and the origin server.
One common pattern is a WordPress site with security plugins that block unfamiliar IPs. The admin sees 520 and assumes Cloudflare is broken. In reality, the origin firewall is rejecting Cloudflare requests or closing connections too early.
Fast Diagnosis Checklist for HTTP 502
For a 502, begin at the proxy or gateway. The gateway is reporting the failure, so it usually knows which upstream failed.
- Check Nginx or Apache error logs for upstream timeout, connection refused, or reset messages.
- Verify the upstream service is running, such as PHP FPM, Gunicorn, Puma, Node.js, or Tomcat.
- Inspect health checks on the load balancer.
- Check application crash logs for memory errors, fatal exceptions, or failed startup.
- Review timeout settings between proxy and backend.
- Confirm port and socket paths are correct after deployments.
A classic example is Nginx returning 502 because PHP FPM is down. The browser shows a server error, but the root cause is simple: the proxy tried to reach a backend process that was not listening.
How to Tell Which Layer Is Failing
Use a simple split test:
- Test through Cloudflare using the public domain.
- Test the origin directly using the origin IP and Host header.
- Test from the server itself using localhost or internal service URLs.
- Compare timestamps across Cloudflare, proxy, app, and system logs.
If only Cloudflare requests fail, suspect firewall behavior, bad response formatting, TLS issues, or Cloudflare-facing limits. If direct origin requests also fail, the issue is deeper inside the origin stack. If the proxy logs show upstream failure, treat it as a 502-style backend problem even when the user-facing page shows another code.
Common Misreads That Slow Teams Down
A 520 does not always mean Cloudflare caused the outage. Cloudflare is often just the messenger. It tried to connect and received a response it could not use.
A 502 does not always mean the main web server is down. Often, the web server is alive, but the upstream app has crashed or stopped responding.
It drives me crazy that many dashboards only show the final user-facing code. That hides the chain. A request may pass through Cloudflare, hit Nginx, call a Node API, query a database, and fail at step four. Without layered logs, the final status code is only a clue.
Best Practices to Reduce Both Errors
- Keep origin firewall rules current with official Cloudflare IP ranges.
- Set sane timeouts across Cloudflare, proxies, apps, and databases.
- Limit header size and watch cookie growth after marketing or authentication changes.
- Use health checks for every upstream service.
- Add structured logging with request IDs across layers.
- Monitor worker pools, memory, CPU, and connection counts.
- Deploy gradually and keep rollback steps ready.
Final Recommendation
When you see Cloudflare 520, ask: “What did my origin send to Cloudflare, and why was it unusable?” When you see HTTP 502, ask: “Which upstream service gave the gateway a bad response?”
That single shift saves time. It also keeps blame away from the wrong system. Start with the layer that generated the code, match it to logs from the same minute, then trace one hop deeper. Most 520 and 502 incidents become clear once you separate Cloudflare edge behavior from standard gateway failure.


