A proxy got an invalid response from the server behind it.
When to use it
The app behind nginx or a load balancer crashed or returned garbage.
Common causes and fixes
A proxy such as nginx, a load balancer or Cloudflare couldn’t get a valid response from the app behind it. Common causes: the app crashed or isn’t running, it listens on a different port or address than the proxy expects (127.0.0.1 instead of 0.0.0.0 inside Docker), a wrong PHP-FPM socket path or permission, or oversized response headers. nginx’s error log says “connection refused” or “upstream prematurely closed connection”.
Intermittent 502s behind AWS ALB are often keep-alive races: the app closes idle connections before the balancer does, so set the app’s keep-alive timeout above the 60-second ALB idle timeout, for example server.keepAliveTimeout = 65000 in Node.js. Cloudflare’s error page shows whether the browser, Cloudflare or your host failed. Also check for crash loops in systemd or Kubernetes, and OOM kills in dmesg.
Server error (5xx)
5xx codes are server errors: the request may be fine, but the server or something behind it failed. These are worth alerting on.
What it looks like
HTTP/1.1 502 Bad GatewayHow to send it
| Node.js / Express | res.status(502).end() |
|---|---|
| Python / Flask | return '', 502 |
| nginx | return 502; |
How to check it
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/