What the message looks like
2026/10/29 11:20:45 [error] 1234#1234: *4521 upstream prematurely closed connection while reading response header from upstream, client: 203.0.113.5, server: example.com, request: "POST /api/export HTTP/2.0", upstream: "http://127.0.0.1:8080/api/export"Common causes
- The backend process crashed or was killed during the request (segfault, out-of-memory kill, a worker recycled after a request limit).
- The backend was restarted or reloaded at that moment, for example by a deployment or a service manager.
- A protocol mismatch:
proxy_pass http://to a port that expects HTTPS, orfastcgi_passto a plain HTTP port. - The backend closes idle keepalive connections sooner than nginx reuses them.
- The backend rejects the request, for example a header or body that is too large, and drops the connection.
Check
- How often and for which URLs
grep 'prematurely closed' /var/log/nginx/error.log | tail -n 10 - Did the backend crash or restart? (PHP-FPM example)
- Was a process killed by the kernel?
sudo dmesg -T | grep -iE 'out of memory|killed process|segfault' | tail -n 10 - Does the backend answer directly? (adjust the port)
curl -sv -o /dev/null http://127.0.0.1:8080/
Fix
- Find out why the backend dies: read its own log and the kernel log, and fix crashes or memory exhaustion first.
- Check the scheme: use
proxy_pass https://...for a TLS backend,fastcgi_passonly for FastCGI sockets or ports. - When keepalive is used towards the backend, set the backend idle timeout higher than nginx's
keepalive_timeoutin theupstreamblock. - Avoid restarting the backend during traffic: use a graceful reload (
systemctl reload ...) in deployments. - Test and apply:
nginx -t && systemctl reload nginx.
Typical scenario
Your PHP service generates PDF invoices. For certain customers nginx answers 502, the error log says "upstream prematurely closed connection while reading response header from upstream", and at the same moment the PHP-FPM log says "child 1234 exited on signal 11 (SIGSEGV)". A PHP extension crashes on this data. Compare the timestamps in both logs before you change the nginx configuration, because nginx only reports the symptom here.
FAQ
How does this differ from 111 Connection refused?
With 111 nothing listens on the port. Here the connection was accepted and then closed without a response, so the process is running but failing or disconnecting.
Why does it only happen on some requests?
Usually these requests trigger the crash or exceed a limit, for example a large export or upload. Compare the failing URLs in the log.
How can I tell that the backend crashed?
The backend's log usually says so: with PHP-FPM "exited on signal 11" or "exited on signal 9 (SIGKILL)", with Gunicorn "Worker … was sent SIGKILL". Signal 9 at the same time often points to the OOM killer, which you confirm with journalctl -k or dmesg.
Does nginx retry the request on another server?
That depends on proxy_next_upstream. By default nginx retries on errors and timeouts, but only if several upstream servers are defined. POST requests are not retried without the non_idempotent option, so nothing is executed twice.
Why is it a 502 and not a 504?
A 504 happens when the backend stays silent for too long. Here the backend closed the connection, actively or by crashing, before it could answer. nginx sees a premature end, not a timeout, and therefore returns 502 Bad Gateway.