Server error · 502
502 Bad Gateway
A 502 Bad Gateway means a proxy, load balancer, or CDN accepted the request, then failed to get a valid HTTP response from the origin behind it. Visitors see the proxy’s error page.
HTTP status codes explained · the longer reference on the blog.
What 502 means
The status is generated by the gateway in front of the application: nginx, Caddy, Traefik, a cloud load balancer, or a CDN. That gateway opened a connection to an upstream — an app process, PHP-FPM, a serverless function, or another proxy — and the bytes that came back were empty, cut off, reset, or not HTTP.
A 500 Internal Server Error is a status the application itself managed to send. A 504 Gateway Timeout means the upstream stayed quiet until the gateway’s timer ran out. A 503 Service Unavailable is an explicit “not taking requests” from something that could still speak HTTP. A 502 sits in the gap: the gateway tried, and the upstream’s reply was unusable. On Cloudflare, a similar origin failure is often shown as error 520 instead of a generic 502.
Common causes
The app process is down or crash-looping
systemd, Docker, PM2, or PHP-FPM is stopped, or it exits as soon as it starts. The proxy is still listening, so the TCP connection to the proxy works, and the upstream socket then closes.
The proxy is aimed at the wrong place
The upstream port changed in a deploy, the socket path is stale, or DNS for the upstream host resolves to an old instance. The proxy connects to something that is not your app.
The upstream died mid-response
An out-of-memory kill, a segfault, or a worker timeout closes the connection after headers started, or before a status line. The proxy reports 502 and the app log shows the crash.
The protocol does not match
The proxy speaks HTTP to an upstream that expects TLS, or the reverse. The first bytes are not a status line, so the gateway calls the response invalid.
How to fix it
Read the proxy error for this request
nginx phrases like “connect() failed”, “upstream prematurely closed connection”, and “recv() failed” point at a refused port, a crashed worker, and a reset connection. Start there, not in the browser page.
Confirm the app is listening
Check the process and the port or socket the proxy uses. Start the app, then reload the proxy. A proxy that cached a dead upstream IP needs a reload after the instance is replaced.
Check why the process exited
Look for OOM kills, a full disk, a missing env var, or a fatal error on boot. Restarting in a loop without reading that log brings the 502 back on the next request.
Compare the origin with the edge
Request the origin directly, skipping the CDN. If the origin returns 200 and the public host returns 502, the failure is between the CDN and the origin, or in how the CDN is configured to reach it.
Is it your site, or just you?
A 502 is served by the gateway to whoever asks. It does not depend on your cookies. Try a private window and a phone on cellular data. If all of them see 502, visitors are seeing it too.
The homepage checker requests the URL from our network. HTTP 502 from the checker means the gateway is returning that status outside your ISP. If the checker returns 200 while you still see 502, your DNS may be pointing at a different edge, or you are looking at a cached error page.
How to monitor for 502
Monitor the public URL and leave the expected status at 200. A 502 fails the check and, after a second consecutive failure, sends an alert with the status code. That second check filters a process restart that recovers in one interval. When the origin answers 200 again, a recovery alert follows.
The checker on the ismysiteup homepage requests the URL from our servers and shows the HTTP status, response time, and SSL details. No account is required, and that checker allows 5 checks per hour from the same network. A free account watches 5 URLs, each checked every 10 minutes, and sends alerts on WhatsApp, Telegram, Slack, or email. A monitor expects HTTP 200 unless you set a different expected status (100–599). One failed check is confirmed with a second check before the first alert, and a later success sends a recovery alert. Pro (every 5 minutes) and Business (every minute) are listed on the pricing page; paid checkout is not open yet.
Related status codes
- 500Internal Server ErrorThe application failed while handling a request it understood.
- 503Service UnavailableThe server is reachable and is refusing new work for the moment.
- 504Gateway TimeoutA proxy connected to the origin and never got a response in time.
- 520Web Server Returned an Unknown ErrorCloudflare connected, and the origin’s reply was empty or not valid HTTP.
- 521Web Server Is DownCloudflare reached the origin IP, and the origin refused the connection.
FAQ
What does 502 Bad Gateway mean?
A server in the middle received an invalid response from the server it called on your behalf. Your app usually did not send a normal error page. The proxy gave up on the upstream’s reply.
Is 502 my host, my app, or Cloudflare?
Whichever layer is the gateway. Read the error page branding and the proxy log. If Cloudflare shows 502, also check error 520, which Cloudflare uses when the origin’s response is empty or unrecognized.
Will a 502 alert me on the free plan?
Yes. A free monitor checks as often as every 10 minutes and treats any status other than the expected one as a failure. The default expected status is 200. Alerts go to WhatsApp, Telegram, Slack, or email after the failure repeats once.