nginx logs "upstream prematurely closed connection while reading response header from upstream" and users get 502.
IntermediateTroubleshooting5xx errors
Answer
Likely causes → The backend closed the connection before sending a response: the worker crashed or was OOM-killed mid-request, the app server's worker timeout (gunicorn default 30 s) killed a slow request, or the backend closed an idle keep-alive connection nginx was reusing.
Diagnose →
grep -E 'WORKER TIMEOUT|Booting worker' /var/log/app/gunicorn.log
dmesg -T | grep -i 'killed process'
kubectl get pods -n prod -o wide | grep -v ' 0 ' # restarts
Fix → Fix the slow endpoint or raise the worker timeout above the longest legit request. If OOM, fix memory. For keep-alive, set nginx keepalive_timeout to the upstream lower than the backend's.
Prevent → Align timeouts across layers (client > LB > proxy > app), and alert on worker restarts.
More on 5xx errors
Right after a release, 500s spike to 8% and logs show `java.lang.NullPointerException` in a new code path.BeginnerLong-running report exports fail with "504 Gateway Time-out" from the ALB at exactly 60 seconds.BeginnerEvery deploy causes 10–20 seconds of 502s from the ALB, even with a rolling update.IntermediateA Node.js service behind an ALB returns a steady trickle of 502s under normal load, with no app errors at all.AdvancedThrough Istio, clients get "503 upstream connect error or disconnect/reset before headers. reset reason: connection failure".IntermediateAPI Gateway returns "502 Bad Gateway" and the logs say "Execution failed due to configuration error: Malformed Lambda proxy response".Beginner