502 Bad Gateway
What HTTP 502 means, why proxies and CDNs return it, and how to find which layer between the user and your app actually failed.
502 Bad Gateway comes from a proxy — a load balancer, reverse proxy or CDN — that asked an upstream server for the response and got garbage back: a refused connection, a reset, a malformed reply, or nothing usable. The proxy is fine; the thing behind it isn't.
Whenever you see a 502, mentally add the missing words: "I, the proxy, got a bad response from the origin."
Common causes
- The app process is down — crashed, OOM-killed, or mid-restart during a deploy; nginx can't reach the socket and 502s
- Deploys without graceful handoff — old workers killed before new ones listen; the seconds-long gap is a burst of 502s on every release
- Upstream connection limits — backend at max connections refuses new ones
- Wrong upstream config — proxy pointed at a dead port/IP after an infrastructure change
- Response too large for proxy buffers — oversized headers from the
backend (nginx
upstream sent too big header) - HTTPS mismatch — proxy speaks plain HTTP to an origin expecting TLS, or can't validate the origin certificate (Cloudflare shows 525/526 for these)
- Keep-alive races — origin closes idle connections a hair before the proxy reuses them; sporadic, low-rate 502s that vanish when keep-alive timeouts are aligned
Debugging
- Find the failing layer. Hit the origin directly (bypassing the CDN/LB) — if it answers, the problem is between the proxy and origin: security groups, certificates, connection limits.
- Read the proxy's error log, not the app's — the app may have never
received the request. nginx's
connect() failedvsupstream prematurely closeddistinguish "down" from "crashed mid-response". - Correlate with deploys. 502 bursts aligned with releases mean the rollout isn't graceful — fix the handoff, don't tune the proxy.
A multi-region HTTP monitor timestamps every 502 precisely, which makes the deploy correlation obvious, and confirmed alerting separates a one-request blip from a dead backend. If only one region sees 502s, suspect the CDN PoP, not your origin.
500 Internal Server Error
What HTTP 500 means, the usual suspects from unhandled exceptions to bad deploys, and how to catch them before your users report them.
503 Service Unavailable
What HTTP 503 means, the overload-vs-maintenance distinction, Retry-After, and why health checks returning 503 take servers out of rotation.