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
- 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.
- 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_passonce, at start-up, so an upstream that changed address may need a reload. - 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.
- Headers too big: nginx logs
upstream sent too big header while reading response header from upstream. Increaseproxy_buffer_size, or send smaller cookies. - 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.
- 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
- Read the proxy’s error log. It names the cause:
connect() failed (111: Connection refused),upstream prematurely closed connection,no live upstreams. - Call the application directly, skipping the proxy:
curl -i http://127.0.0.1:3000/healthfrom 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. - Check the process: is it running, listening on the expected port (
ss -ltnp), and not out of memory (dmesg | grep -i killed)? - Check what changed: a deploy, a new image, a restart, a port, a changed environment variable.
- 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
- HTTP 301 Moved Permanently: redirects, caching and SEO
- HTTP 302 Found: temporary redirects, and when to use 303 or 307 instead
- HTTP 304 Not Modified: how conditional requests and ETags save bandwidth
- HTTP 400 Bad Request: the common causes and how to find the bad part
- HTTP 401 Unauthorized: missing or invalid credentials, and 401 vs 403
- HTTP 403 Forbidden: why access is refused and how to fix it
- HTTP 404 Not Found: causes, how to debug it and what to return instead
- HTTP 429 Too Many Requests: what it means and how to handle it
- HTTP 500 Internal Server Error: how to find the cause and what to tell users
- HTTP 503 Service Unavailable: maintenance pages, overload and Retry-After
- HTTP 504 Gateway Timeout: why the proxy gave up waiting, and the timeouts to check