502 Bad Gateway

A server in front of the website tried to pass your request to the application behind it and got nothing usable back. The front door works. What is behind it does not.

502 Bad Gateway

The short version

  • A 502 means a proxy reached the site but could not get a valid response from the application behind it.
  • It is a two-machine failure: The front is healthy, the back is not.
  • Most 502s clear within minutes because process supervisors restart crashed applications automatically.
  • A 502 carrying a CDN's branding means that CDN could not reach the origin server.

Almost no website is a single machine any more. Between you and the code that builds a page there is usually a CDN, then a load balancer, then a reverse proxy, then the application itself. A 502 tells you that this chain broke at a specific link: One of the machines in the middle asked the machine behind it for a response, and what came back was not a valid HTTP response.

That is why 502 is such a useful error, despite being frustrating. It tells you the site exists, the DNS is correct, the network path is fine, and something is listening on the right port. The failure is one hop further in.

What produces one

  • The application process is not running. It crashed, or was killed by the operating system for using too much memory, or was stopped during a deploy and has not come back. The proxy connects to the socket, finds nothing, and returns 502.
  • The application crashed mid-response. It started answering and died partway through, leaving the proxy with a truncated response it cannot forward.
  • It returned something that is not HTTP. Usually a startup error or a stack trace written to standard output where a response should have been.
  • A deploy is in progress. Old processes have stopped and new ones have not finished starting. This window should be zero with a proper rolling deploy, but not every deploy is proper.
  • The connection pool is exhausted. Every upstream worker is busy, the proxy cannot get a connection, and it gives up.
  • The origin is unreachable from the CDN. A firewall rule, an origin IP change, or a certificate the CDN will not accept.

Why it usually clears quickly

Because the most common cause is a process that died, and process supervisors exist precisely to restart processes that die. Systemd, a container orchestrator, a platform supervisor - All of them notice a dead worker within seconds and start another.

So the typical 502 is measured in seconds to a couple of minutes. A 502 that persists for longer is a different and more interesting problem: It usually means the application is crashing on startup, in a loop, which means a deploy has shipped something broken. That needs a human and a rollback.

Rule of thumb: A 502 that clears on a reload was a dead worker. A 502 that persists for five minutes is a crash loop. The first fixes itself; the second does not.

502 versus 504

These two are constantly confused and mean genuinely different things.

A 502 means the upstream answered wrongly or not at all - The connection failed, or the response was invalid. It is fast: The proxy knows immediately.

A 504 means the upstream did not answer in time. The connection succeeded, the request was accepted, and then nothing came back before the proxy's timeout expired. It is slow by definition - You usually wait thirty or sixty seconds before seeing it.

The diagnostic implication is different too. A 502 says the application is not there. A 504 says the application is there and stuck, which usually means a slow database query or a blocked external call.

When the 502 has branding on it

If the error page carries a company's logo, that company's edge network is reporting that it could not reach the origin. Cloudflare pages carry a ray ID and often a more specific number in the 520–527 range, which narrows the cause considerably - Each of those means something particular.

This is worth noticing because it tells you which company is having the problem. A Cloudflare-branded 502 usually means the customer's origin server is down, not that Cloudflare is. A plain 502 from the site's own infrastructure means the problem is entirely theirs.

What to do as a visitor

Reload once, wait thirty seconds, reload again. That covers the overwhelming majority of 502s, which are a worker that died and has already been replaced.

If it persists, check the site from another network to confirm it is not specific to your route, and then stop. There is nothing on your side involved in a 502 - Your request reached their infrastructure successfully, which is more than you can say for most errors.

If it is your site

Look at the application, not the proxy. The proxy is telling you the truth.

  1. Is the process running? Check the supervisor. If it is restarting repeatedly, you have a crash loop and the application log has the reason.
  2. Is it listening where the proxy expects? A port or socket path mismatch after a configuration change is a classic.
  3. Memory. Check whether the kernel OOM killer has been terminating the process. This is the most commonly missed cause on small servers, and the application log shows nothing because the process was killed rather than crashing.
  4. Upstream timeouts and buffer sizes. In nginx, an oversized response header can produce a 502 even when the application is perfectly healthy.
  5. For a CDN 502: Confirm the origin is reachable from outside your own network, that its certificate is valid, and that the CDN's address ranges are not blocked by your firewall.

Questions people ask

What does 502 Bad Gateway mean?

A server acting as a proxy - A CDN, load balancer or reverse proxy - Tried to pass your request to the application behind it and did not get a valid response back. The front of the site is working; the part that generates pages is not.

How long does a 502 error last?

Usually seconds to a couple of minutes, because the most common cause is an application process that died and process supervisors restart those automatically. A 502 lasting longer than about five minutes normally means the application is crashing on startup in a loop, which needs a human.

Can I fix a 502 error myself?

No. Your request reached the site's infrastructure successfully - The failure happened between two of their machines. Reload once in case a worker has already been replaced, but nothing on your device is involved.

What is the difference between 502 and 504?

A 502 means the upstream server gave an invalid response or none at all, and the proxy knows immediately. A 504 means the upstream accepted the request and never finished answering, so the proxy waited out its timeout first. A 502 suggests a dead process; a 504 suggests a stuck one.

Read next

500 Internal Server Error

What a 500 Internal Server Error actually means, why it is always the server's fault, and the handful of cases where a visitor can work around it.

Error codes explained4 min read

504 Gateway Timeout

What a 504 Gateway Timeout means, why it indicates a stuck rather than a dead application, and what usually causes the delay.

Error codes explained4 min read

Cloudflare error codes

What Cloudflare errors 520, 521, 522, 523, 524, 525 and 526 mean, and whether the fault is Cloudflare or the website behind it.

Error codes explained4 min read

503 Service Unavailable

What a 503 Service Unavailable error means, why it is often deliberate, and what the Retry-After header tells you about how long to wait.

Error codes explained4 min read