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.

All Real-World Interview questions Roadmap: Interview readiness

More on 5xx errors