502 Bad Gateway

A proxy, load balancer or CDN accepted your request, passed it to the server behind it, and got back something it could not use: a refused connection, a dropped connection or a malformed reply.

What 502 means

Only an intermediary sends a 502. Most sites have at least one: nginx in front of an application server, a cloud load balancer, a CDN. The intermediary was working, which is why you got an HTTP response at all. The server it forwards to (the upstream, or origin) was not.

"Invalid response" covers three situations in practice: nothing was listening where the proxy tried to connect, the connection closed before a full response arrived, or the bytes that came back were not valid HTTP.

Reading the cause from the nginx error log

nginx writes one line per failed upstream request, and the wording tells the cases apart:

Error log saysWhat happenedLook at
connect() failed (111: Connection refused) while connecting to upstreamNothing is listening on the upstream addressIs the app running? Does the port or socket path in proxy_pass or fastcgi_pass match it?
upstream prematurely closed connection while reading response header from upstreamThe app accepted the request and then closed the connectionA worker that crashed, was killed for memory use, or was restarted mid-request
upstream sent too big header while reading response header from upstreamThe response headers did not fit in nginx's bufferLarge cookies or tokens; raise proxy_buffer_size (or fastcgi_buffer_size)
no live upstreams while connecting to upstreamEvery server in the upstream group is marked as failedHealth of all backends, not just one

Apache's mod_proxy answers 502 when the backend sends a response header line it cannot parse (the default ProxyBadHeader IsError). When it cannot connect at all it sends a 503 instead, so the same fault looks different depending on the proxy.

Other common causes

  • A deploy or restart: for a few seconds the old process has gone and the new one is not accepting connections yet.
  • The upstream's keep-alive timeout is shorter than the proxy's, so the proxy reuses a connection the application has just closed. Make the application's idle timeout the longer of the two.
  • The proxy speaks plain HTTP to a port that expects TLS, or the reverse.
  • A firewall or security group between proxy and upstream that rejects the connection.
  • The upstream's host name resolves to an old address after a move.

What to do as a visitor

Wait a minute and reload. A 502 during a deploy clears by itself. If it persists, the fault is on the site's side; clearing cookies or changing browser does not help unless the 502 only happens when you are logged in (the oversized-header case above). To see whether the site is failing for everyone, check it from another network.

Fixing it on your own stack

  • Confirm the upstream is running and listening: curl -v http://127.0.0.1:3000/ from the proxy host, with the address from your proxy configuration.
  • Read the proxy's error log for the minute of the failure, then the application's log for the same request.
  • If 502s line up with deploys, start the new process before stopping the old one, or have the proxy retry another backend. nginx does this for connection errors and timeouts by default (proxy_next_upstream error timeout), provided the upstream block lists more than one server.
  • If they line up with traffic peaks, check the kernel log for out-of-memory kills and the application server's worker and backlog limits.

502 compared with 504, 503 and 500

CodeSent byThe upstream
502 Bad GatewayA proxyRefused, dropped or garbled the response
504 Gateway TimeoutA proxyWas reached but stayed silent past the proxy's timeout
503 Service UnavailableA proxy or the applicationIs deliberately not taking requests for now
500 Internal Server ErrorThe applicationAnswered, with its own failure

Behind Cloudflare the same family of faults is reported as 520, 521 and 522. A plain 502 page with Cloudflare branding and no other detail usually comes from the origin and is being passed through.

Example 502 response

HTTP/1.1 502 Bad Gateway
Server: nginx
Content-Type: text/html
Content-Length: 142
Connection: keep-alive

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>

The Server header and the stock error page name the proxy that gave up, which tells you whose error log to read. It is not the server that failed.

Defined in RFC 9110, section 15.6.3.

Questions

Is a 502 Bad Gateway my fault?

Rarely. It is produced by a proxy on the site's side when the server behind it fails. The one client-side case is very large cookies, which can push the response headers past the proxy's buffer; clearing cookies for that site fixes it for you.

What is the difference between 502 and 504?

Both come from a proxy. A 502 means the upstream gave a bad answer or none at all: connection refused, closed early, or not valid HTTP. A 504 means the upstream accepted the request and then took longer than the proxy was willing to wait.

Why do I get 502 errors only during deploys?

The application process is restarting and for a moment nothing is listening on its port, so the proxy's connection is refused. Start the new process before stopping the old one, or run more than one backend so the proxy can retry another.

Does a 502 mean the whole site is down?

Not necessarily. It means one request could not be served by the backend it was routed to. A single unhealthy server behind a load balancer produces occasional 502s while the others keep working.