Diagnosis of 502 and 504
Identify which hop generated the failure
A 502 Bad Gateway often points to an unusable upstream response or failed connection; a 504 Gateway Timeout often indicates that a gateway did not receive a timely upstream response. A backend can itself return either code, so inspect access and error logs plus upstream status and timings before deciding. A 503 may mean maintenance, overload, or a deliberate local response. In the synthetic records below, the first failure occurs during connection and the second while waiting for headers.
SYNTHETIC: 502 connect() failed (Connection refused) while connecting to upstreamSYNTHETIC: 504 upstream timed out while reading response header from upstreamSeparate reachability from application readiness
Confirm the configured address, port, protocol and application health from the proxy network context. A successful connection only proves a socket accepted it; the application might be unhealthy. For a Unix socket check its existence, parent directory traversal and worker access. For DNS check the intended resolver and records; runtime resolution requirements differ by configuration and version. For upstream HTTPS check name, chain and trust store with verification enabled. This command queries only the optional local fixture backend, not an external system.
curl --max-time 3 -i http://127.0.0.1:19448/healthUnderstand the timeout and retry budget
A read timeout measures the gap between reads from upstream, not the complete request duration. Connect, send and read timeouts cover different phases. Compare upstream timings and backend logs before increasing them. Retrying a failed request can duplicate a state change; use a deliberate method-aware policy and avoid blanket retries. The lab disables retries so that each synthetic response or failure is easier to interpret.
location /api/ { proxy_pass http://127.0.0.1:19448/; proxy_connect_timeout 1s; proxy_read_timeout 1s; proxy_next_upstream off;}Recognize client closure, cache and streaming symptoms
499 is an Nginx-specific log status associated with a client closing the connection before processing completed; it is not a standard HTTP response delivered to that client. Correlate with client deadlines, request times and upstream evidence. A cache miss is not necessarily a failure: compare cache key, bypass conditions and freshness. A stale response may be allowed by policy. For WebSocket disconnects, compare upgrade success, heartbeat cadence, idle timeouts and intermediary limits rather than changing them all. These synthetic log records describe distinct observations, not proof of cause.
[ { "status": 499, "request_time": "2.000", "upstream_status": "-" }, { "status": 200, "cache": "MISS" }, { "status": 200, "cache": "STALE" }]Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Diagnosis of 502 and 504
Distinguish upstream connectivity failures from timeouts, interpret 499 and 503, and troubleshoot cache and streaming symptoms safely.
A 502 log includes connection refused. What is the next discriminating check?
A proxy connects to its upstream, but receives no response headers for longer than proxy_read_timeout. What hypothesis should you investigate?
What does a logged 499 generally indicate?
Why avoid retrying every failed write?
A cached response is marked STALE. What follows?