HTTP 502 Bad Gateway: finding the broken link between the proxy and the app

Open this example in the tool

Try it in the tool

HTTP Status Codes

Every HTTP status code with its RFC, meaning and when to use it.

cURL to Code

Turn curl commands into fetch, axios, Python requests or Go.

HTTP 502 Bad Gateway is returned by a server acting as a gateway or proxy when the server behind it (the upstream) gave it an invalid response, or none that it could use. It is defined in RFC 9110, section 15.6.3. The visitor sees the proxy’s error page, but the fault is behind it.

browser  →  CDN / load balancer  →  nginx  →  application
                                   ^ the 502 is generated here, when the application misbehaves

Causes, from most to least common

  1. The application is not running: it crashed, was killed for lack of memory, or has not started yet. The proxy tries to connect and the connection is refused.
  2. The proxy points at the wrong place: another port, a socket file that does not exist, or a name that now resolves to another address. nginx resolves a hostname in proxy_pass once, at start-up, so an upstream that changed address may need a reload.
  3. The application closes the connection in the middle of a response, for example because a worker was recycled, or the request hit a limit in the app server.
  4. Headers too big: nginx logs upstream sent too big header while reading response header from upstream. Increase proxy_buffer_size, or send smaller cookies.
  5. Protocol mismatch: the proxy talks HTTPS to an upstream that speaks plain HTTP, or the reverse, or the TLS certificate of the upstream is rejected.
  6. A bad response: the upstream sends something that is not HTTP, or a response that breaks the rules of the protocol.

Find the broken hop

  1. Read the proxy’s error log. It names the cause: connect() failed (111: Connection refused), upstream prematurely closed connection, no live upstreams.
  2. Call the application directly, skipping the proxy: curl -i http://127.0.0.1:3000/health from the same machine. If that works, the application is healthy and the problem is in the proxy’s settings; if not, read the application’s log.
  3. Check the process: is it running, listening on the expected port (ss -ltnp), and not out of memory (dmesg | grep -i killed)?
  4. Check what changed: a deploy, a new image, a restart, a port, a changed environment variable.
  5. In a container setup, confirm that the container is ready (health check) and that the proxy uses the service name and the container’s port, not the published one.

A curl command like the one in step 2 can be converted to code with the curl converter if you want a repeatable health check in your own tooling.

502 behind a CDN

With a CDN in front, the same status may come from the CDN, from your load balancer, or from nginx, and the page may look the same. Look at the response headers (server, via, cf-ray, x-amzn-requestid) to see which layer answered, then read that layer’s log. Cloudflare reports its own origin errors as 520 to 526; search the HTTP status codes tool for 522 or cloudflare to read what each one means.

Preventing 502 during a deploy

  • Start the new version, wait for a health check, and only then stop the old one (rolling or blue-green).
  • Let the old process finish its running requests (graceful shutdown) before it exits.
  • Keep the proxy’s idle timeout shorter than the application’s keep-alive time, so that the proxy never reuses a connection that the application has already closed.

See 504 Gateway Timeout for the case where the upstream is alive but too slow, and 500 for a failure inside the application itself.

Frequently asked questions

Is a 502 caused by my browser?

Almost never. It is produced by a server in the middle (a reverse proxy, CDN or load balancer) that could not get a proper answer from the server behind it. Reloading helps only if the fault was brief.

What is the difference between 502 and 504?

502 means the proxy got an answer that is invalid, or the connection broke or was refused. 504 means the proxy got no answer at all within its time limit. The first points at a crashed or misconfigured upstream, the second at a slow one.

Why do I get 502 right after a deploy?

The old process has stopped before the new one is ready to accept connections, or it listens on another port or socket. Use a readiness check and start the new instance before the old one is stopped.

Does Cloudflare use 502 too?

Yes, but it also has its own 52x codes (520 to 526) that say more about what went wrong between Cloudflare and your origin, such as an unreachable server or a failed TLS handshake. Those numbers are not in the standard.

More in this reference