502 Bad Gateway: What It Means and How to Fix It

Appears on: All browsers. 502 Bad Gateway is an HTTP status code returned by a proxy, CDN, or load balancer when the upstream origin server returns an invalid or no response. It is almost always a server-side problem — the most a visitor can do is reload and wait.

What 502 Bad Gateway actually means

In a modern web architecture, your browser connects to a proxy layer (Nginx, Cloudflare, AWS ALB) which forwards requests to an origin server (a Node.js app, PHP process, or Python service). 502 fires when the proxy receives a response it can't interpret — the origin sent garbage, crashed mid-response, or refused the connection. The proxy is working; the backend isn't. This is distinct from 504, where the backend simply doesn't respond in time.

Most likely causes (ranked)

  1. Application server crashed or ran out of memory (PHP-FPM worker died, Node.js uncaught exception)
  2. Upstream server overloaded — high traffic spike causing connection refusals
  3. Database timeout causing the app to return an error the proxy can't forward cleanly
  4. Nginx or Apache misconfiguration — proxy_pass pointing to wrong port or socket
  5. CDN routing issue — Cloudflare or similar CDN failing to reach origin

What to try as a visitor

Step 1: Hard reload the page

Press Ctrl+Shift+R (Windows/Linux) or Cmd+Shift+R (macOS). Transient 502s from brief application restarts or traffic spikes often clear on the first retry.

Step 2: Clear browser cache

Press Ctrl+Shift+Delete → Advanced → check Cached images and files → Clear data. A previously cached 502 response can persist and serve the error even after the server recovers.

Step 3: Try a different network or DNS

Switch to a mobile hotspot, or change your DNS to 1.1.1.1 (Cloudflare) or 8.8.8.8 (Google). CDN edge node routing issues occasionally affect certain ISPs or DNS resolvers differently.

Step 4: Check the site's status page

Visit downdetector.com or the site's own status page (typically status.[sitename].com). If an incident is posted, the 502 is confirmed server-side and there is nothing more to do client-side — wait for recovery.

Step 5: For site operators — check backend logs

In Nginx: /var/log/nginx/error.log. Look for connect() failed or upstream prematurely closed connection. Verify your application server process is running (systemctl status php-fpm / pm2 list), check database connectivity, and confirm proxy_pass targets the correct address and port.

Frequently Asked Questions

Is 502 my fault or the server's fault?

Almost always the server's. 502 means the gateway (Nginx, CDN, load balancer) received a bad or empty response from the application backend. As a visitor, refreshing and waiting is your only reliable option. The error clears when the site operator fixes their backend.

What is the difference between 502 and 504?

502 Bad Gateway: upstream responded with an invalid or error response (crashed, returned garbage). 504 Gateway Timeout: upstream didn't respond within the allowed time (too slow or unreachable). Both originate from a proxy layer, but 502 means a bad response arrived and 504 means no response arrived.

Related Guides

More From This Section