Runbook · Cloud Ops

nginx 502/504: the upstream, not the browser

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.

502 is a bad response

Connection refused, reset, crashed upstream, exhausted pool, oversized header.

504 is a timeout

The connection worked; the response did not arrive within proxy_read_timeout.

Timing narrows it

Instant 502 is connect-time; 504 exactly at the timeout is the timeout; earlier means another proxy gave up first.

Read the error line

error.log names the upstream and the reason. Restarting nginx hides it.

Detect

The error line is the source of truth

CommandWhat it shows
tail -n 200 /var/log/nginx/error.logThe 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.
Failure modes

Two free, three in the paid chapter

FM-01 · Nothing is listening

connect() failed (111: Connection refused) — wrong port, wrong bind address, or the app never started.

FM-03 · 504 at exactly the read timeout

The backend's p99 is longer than proxy_read_timeout; raising it without measuring just moves the failure.

FM-02 · PHP-FPM pool is full

Paid chapter: (11: Resource temporarily unavailable), worker sizing, slow endpoints.

FM-04 · DNS resolved once at startup

Paid chapter: resolver + variable proxy_pass, TTL, blue/green.

FM-05 · Keepalive mismatch

Paid chapter: idle timeouts, Connection "", premature close.

Migration

What the paid chapter covers

StageWhat you get
Measurep99, concurrency, saturation by upstream.
TimeoutsA timeout chain where the backend gives up before nginx does.
KeepaliveUpstream keepalive without premature closes.
DNSRe-resolution that follows a moving upstream.
RollbackReload, not restart; 24-hour observation checklist.
Verification status
Pre-flight reviewed 2026-11-16; paid chapter is documented behaviour, no live reproduction. Verified against the published sources listed below. The 7-day refund covers the gap.

Get the full runbook

Five failure modes, the timeout chain, keepalive, DNS re-resolution and rollback.