Error Code 520: Cloudflare 520 vs 502 for Troubleshooting Server Errors

Error Code 520 usually points to an origin server giving Cloudflare a response it cannot classify, while Error 502 means a bad gateway response occurred between Cloudflare and the origin. Cloudflare describes Error 520 as an unknown error caused by an unexpected, empty, or malformed response from the origin server [source: Cloudflare]. That difference matters because a 520 often sends teams hunting through web server limits, headers, firewalls, and crashed processes, while a 502 usually starts with upstream availability, proxy, DNS, or load balancer checks.

Cloudflare 520 vs 502: The Short Difference

A 520 is Cloudflare saying, “The origin replied, but the reply made no sense to us.” It is not a normal HTTP status created by most web servers. It is Cloudflare’s catch-all response for odd origin behavior.

A 502 Bad Gateway is more direct. It means a server acting as a gateway received an invalid response from another server. In a Cloudflare setup, that may involve Cloudflare, the origin web server, a reverse proxy, a container service, or a load balancer.

Honestly, it feels like 520 exists to make server admins sigh. The browser shows one vague page, while the real cause may sit in Nginx, Apache, PHP FPM, ModSecurity, firewall rules, oversized headers, or a crashed app worker.

What Error Code 520 Usually Means

Error 520 appears when Cloudflare connects to the origin server but receives an unexpected result. The origin may close the connection. It may return no response. It may send malformed headers. It may also reset the TCP connection before Cloudflare can complete the request.

Common 520 causes include:

  • Web server crashes: Apache, Nginx, LiteSpeed, Node, or PHP workers may fail during the request.
  • Blocked Cloudflare IPs: A firewall, security plugin, or hosting rule may reject Cloudflare traffic.
  • Oversized headers: Large cookies or too many headers can break the response.
  • Empty origin response: The server accepts the connection but returns nothing useful.
  • Misconfigured security software: ModSecurity, Fail2Ban, or WAF tools may interrupt requests.
  • Application errors: A CMS plugin, checkout script, or API call may crash only on certain pages.

One common pattern is a site that works fine for static pages but fails on login, cart, checkout, or admin URLs. That hints at app-layer trouble, not a total server outage.

What Error 502 Usually Means

Error 502 means the gateway expected a valid upstream response and did not get one. Cloudflare may show a branded 502 page, or the origin may generate its own 502 before Cloudflare passes it along.

Typical 502 causes include:

  • Origin server down: The web server, app server, or upstream service is offline.
  • Bad reverse proxy setup: Nginx may point to the wrong port or dead backend.
  • PHP FPM failure: WordPress, Magento, Laravel, or custom PHP sites may break when PHP workers stop.
  • Load balancer issues: All backend nodes may fail health checks.
  • SSL or protocol mismatch: Cloudflare may connect using settings the origin does not support.
  • Timeout during upstream handoff: The origin may respond too slowly or drop the request.

A 502 is often easier to trace than a 520. The server logs usually show a clear upstream failure, connection refusal, bad socket, or process crash. Still annoying, but less mysterious.

How Troubleshooting Should Start

The first check should be the timestamp. The engineer should record the exact time, URL, visitor IP, Cloudflare Ray ID, and error code. That Ray ID is not decoration. It helps connect the browser error to Cloudflare events and origin logs.

The next move is to check whether the origin works without Cloudflare. The team can pause Cloudflare proxying for a test hostname or curl the origin IP directly with the correct Host header. If the origin fails without Cloudflare, the fault sits on the server side. If the origin works but fails through Cloudflare, attention should shift to firewall rules, SSL mode, headers, and Cloudflare settings.

520 Troubleshooting Checklist

For a 520, the team should focus on strange or incomplete origin responses. Expect to waste time on small settings that look harmless.

  1. Check error logs: Review Apache, Nginx, PHP, application, and system logs for the same second as the Cloudflare error.
  2. Allow Cloudflare IP ranges: Make sure firewall and security tools do not block Cloudflare addresses.
  3. Disable aggressive WAF rules: Test ModSecurity, host WAF rules, and CMS security plugins.
  4. Inspect response headers: Remove oversized cookies and strange custom headers.
  5. Restart app processes: Restart PHP FPM, Node, Puma, Gunicorn, or other backend services.
  6. Test heavy pages: Login, checkout, search, and admin routes often reveal the issue faster than the homepage.

If only one page causes a 520, the app is probably breaking during that route. If every page causes it, the origin, firewall, or protocol setup is more likely.

502 Troubleshooting Checklist

For a 502, the team should verify that every connection in the chain is alive. That includes Cloudflare to origin, web server to app server, and app server to database or API.

  1. Check service status: Confirm that Nginx, Apache, PHP FPM, database services, and app workers are running.
  2. Test upstream ports: Verify that the reverse proxy points to the correct port and socket.
  3. Review proxy logs: Nginx errors such as connection refused or upstream prematurely closed connection are strong clues.
  4. Check SSL mode: A mismatch between Cloudflare SSL settings and origin certificates can cause gateway failures.
  5. Review deployment changes: A bad release can break upstream routing within seconds.
  6. Check load balancer health: If all nodes fail checks, Cloudflare may have nowhere healthy to send traffic.

Key Clues That Separate 520 From 502

A 520 often appears random. It may affect only some visitors, some URLs, or requests with large cookies. It may vanish after a service restart, which is satisfying for 30 seconds and then deeply irritating when it returns.

A 502 often has a stronger pattern. It may start right after a deployment, scaling event, certificate change, or backend crash. If every request to an app route fails, but static files still load, an upstream app worker may be dead.

Error Main Meaning Best First Check
520 Unknown or malformed origin response Origin logs, headers, firewall, security tools
502 Bad gateway or invalid upstream response Proxy logs, upstream services, ports, SSL mode
Image not found in postmeta

Best Practice for Faster Fixes

The cleanest process is simple: reproduce the error, capture the Ray ID, check matching origin logs, then test the origin directly. Guessing wastes time. So does changing five settings at once. One change should be tested at a time.

Monitoring also helps. A team should track 5xx rates by route, server node, and release version. If 520 errors rise from 0.2% to 4% after a plugin update, rollback becomes a sensible first move. If 502 errors hit every request on one node, remove that node from rotation and inspect the upstream stack.

FAQ

Is Error 520 a Cloudflare problem?

Usually, no. Cloudflare displays the 520, but the cause often sits at the origin server, firewall, application, or response header layer.

Is 502 worse than 520?

Not always. A 502 can indicate a full upstream outage, but it is often easier to diagnose. A 520 can be harder because it hides behind an unknown origin response.

Can a firewall cause Error 520?

Yes. If a firewall blocks or interrupts Cloudflare IPs, the origin may return an empty or broken response that triggers a 520.

Can SSL settings cause 502 errors?

Yes. Wrong SSL mode, expired origin certificates, or protocol mismatches can cause bad gateway errors.

What should be checked first?

The team should check the exact timestamp, Cloudflare Ray ID, affected URL, and origin server logs. That usually saves the most time.