502 is a bad response
Connection refused, reset, crashed upstream, exhausted pool, oversized header.
Intermittent 502/504 is one of five things: the upstream closed the connection, nginx still sends to a stale address, the PHP pool is full, a timeout is shorter than the backend's p99, or a bad upstream is still in rotation.
Pre-flight checks reviewed 2026-11-16. Paid chapter is version-locked to nginx 1.26+ and PHP-FPM; no live reproduction.
Connection refused, reset, crashed upstream, exhausted pool, oversized header.
The connection worked; the response did not arrive within proxy_read_timeout.
Instant 502 is connect-time; 504 exactly at the timeout is the timeout; earlier means another proxy gave up first.
error.log names the upstream and the reason. Restarting nginx hides it.
| Command | What it shows |
|---|---|
| tail -n 200 /var/log/nginx/error.log | The upstream address and the exact errno. |
| curl -w "%{http_code} %{time_starttransfer}" | Whether the failure happens at connect or at the read timeout. |
| ss -ltnp | grep -E ':9000|:3000' | Whether the upstream is actually listening. |
| nginx -T | grep proxy_ | Effective timeouts, retries, keepalive and resolver. |
connect() failed (111: Connection refused) — wrong port, wrong bind address, or the app never started.
The backend's p99 is longer than proxy_read_timeout; raising it without measuring just moves the failure.
Paid chapter: (11: Resource temporarily unavailable), worker sizing, slow endpoints.
Paid chapter: resolver + variable proxy_pass, TTL, blue/green.
Paid chapter: idle timeouts, Connection "", premature close.
| Stage | What you get |
|---|---|
| Measure | p99, concurrency, saturation by upstream. |
| Timeouts | A timeout chain where the backend gives up before nginx does. |
| Keepalive | Upstream keepalive without premature closes. |
| DNS | Re-resolution that follows a moving upstream. |
| Rollback | Reload, not restart; 24-hour observation checklist. |
Five failure modes, the timeout chain, keepalive, DNS re-resolution and rollback.