nginx returns `504 Gateway Time-out` with `upstream timed out (110: Connection timed out) while reading response header from upstream`.
BeginnerTroubleshootingLoad balancers and reverse proxies
Answer
Likely causes → The connection to the upstream succeeded but the app didn't send response headers within proxy_read_timeout (60 s default). The request is slow (heavy query, lock, external call) or the app is overloaded.
Diagnose →
grep 'upstream timed out' /var/log/nginx/error.log | awk -F'request: ' '{print $2}' | sort | uniq -c | sort -rn | head
# add $upstream_response_time and $request_time to log_format to see distribution
Fix → Fix the slow endpoint (indexes, caching, async jobs for long work). Raise proxy_read_timeout only for specific known-long endpoints:
location /reports/ {
proxy_read_timeout 300s;
proxy_pass http://app;
}
Prevent → Log upstream timings, set SLO-based alerts on p99, and move long tasks to background workers with polling or callbacks.
More on Load balancers and reverse proxies
nginx returns `502 Bad Gateway` and the error log says `connect() failed (111: Connection refused) while connecting to upstream`.BeginnerAn ALB target group shows all targets `unhealthy` with `Health checks failed with these codes: [404]`.BeginnerAn ALB returns sporadic `502`s, and access logs show `elb_status_code=502` with `target_status_code=-`. Load is low.Intermediatenginx logs `upstream prematurely closed connection while reading response header from upstream`.Intermediatenginx error log shows `no live upstreams while connecting to upstream` and every request gets 502.IntermediateAn NLB shows all targets healthy, but clients from the internet time out connecting through it.Intermediate