HTTP 502 — Bad Gateway
HTTP 502
Bad Gateway
A proxy or gateway reached the server behind it and got back a broken response, or none at all.
What 502 actually means
Your request reached a server acting as an intermediary — a CDN, load balancer, or reverse proxy such as Nginx — and that intermediary could not get a usable answer out of the machine actually running the site. The front door is working; the thing behind it is not. Because the proxy is healthy enough to generate an error page, a 502 almost always means the application process has crashed, is still starting up, or is not listening where the proxy expects it to be.
Common causes
- The application process crashed or was killed, often by the out-of-memory killer
- The app is listening on a different port or socket than the proxy configuration points at
- The upstream returned a malformed response the proxy could not parse
- A PHP-FPM pool with no free workers, so connections are refused
- A firewall or security group blocking traffic from the proxy to the application
- DNS for the upstream host failing or resolving to a stale address
- The application restarting during a deploy, with no connection draining
If you're just trying to view the page
- Wait a minute and reload — deploys and restarts produce short bursts of 502s that clear by themselves
- Hard refresh (Ctrl+F5 or ⌘ + Shift + R) in case a broken response was cached
- Check the site's status page or Downdetector to confirm it is not just you
- Nothing else — this is entirely a server-side fault
If it's your site
- Confirm the application process is actually running, and read its log for the crash that preceded the 502
- Check the proxy's error log for the specific reason — Nginx will say "connect() failed (111: Connection refused)" or "upstream prematurely closed connection", and those point at different problems
- Verify the upstream host and port in the proxy config match where the app is bound; binding to 127.0.0.1 when the proxy is on another host is a classic
- Look for OOM kills in `dmesg` or `journalctl -k`; a memory leak shows up as periodic 502s
- For PHP-FPM, check `pm.max_children` and the slow log — an exhausted pool refuses connections
- Increase `proxy_read_timeout` only if the upstream is slow rather than dead; otherwise you are hiding a 504
- Use health checks and connection draining so deploys do not drop in-flight requests
Reference
- Status code
- 502
- Reason phrase
- Bad Gateway
- Category
- 5xx — Server Error
- Cacheable by default
- No
- Defined in
- RFC 9110 §15.6.3
Common questions
- What does HTTP 502 mean?
- A proxy or gateway reached the server behind it and got back a broken response, or none at all. Your request reached a server acting as an intermediary — a CDN, load balancer, or reverse proxy such as Nginx — and that intermediary could not get a usable answer out of the machine actually running the site. The front door is working; the thing behind it is not.
- How do I fix a 502 error?
- Wait a minute and reload — deploys and restarts produce short bursts of 502s that clear by themselves
- Is 502 a client error or a server error?
- 502 is in the 5xx range, which means server error. The request was acceptable but the server could not fulfil it, so retrying later can succeed.
Related pages
- 500 Internal Server Error Something went wrong on the server and it has no more specific code to describe
- 503 Service Unavailable The server is temporarily unable to handle the request, usually because it is ov
- 504 Gateway Timeout A proxy or gateway waited for the server behind it and gave up before an answer
- 524 A Timeout Occurred Cloudflare connected to the origin successfully but the origin did not finish re
- 521 Web Server Is Down Cloudflare could not connect to the origin server because it refused the connect
- 522 Connection Timed Out Cloudflare tried to open a connection to the origin server and never got a reply
- 520 Web Server Returned an Unknown Error Cloudflare reached the origin server but got back a response it could not make s
- 525 SSL Handshake Failed The TLS handshake between Cloudflare and the origin server failed.
- 526 Invalid SSL Certificate The origin server presented a TLS certificate that Cloudflare could not validate
- 530 Origin DNS Error A Cloudflare error shown alongside a 1xxx error code that carries the real expla
- 501 Not Implemented The server does not support the functionality needed to fulfil the request, usua
- 511 Network Authentication Required You need to log in to the network itself — this is a captive portal, not the sit