Fix Nginx "upstream prematurely closed connection" (502)
Nginx 502 with "upstream prematurely closed connection" means the backend hung up mid-response. Tell a 502 from a truncated 200, then check the backend log.
"upstream prematurely closed connection" means the backend closed the connection before it finished answering Nginx's request, so the fix is on the backend (crash, restart, out-of-memory), not in the Nginx config. error.log fills with lines like this one. Timestamp, PID, client: and server: are shortened to ….
… [error] … upstream prematurely closed connection while reading response header from upstream, … request: "GET /crash HTTP/1.1", upstream: "http://127.0.0.1:9001/crash", …Nginx connected to the backend and sent the request, and the backend closed the connection before it finished answering. This is not a typo in the Nginx config. It is the trace of a backend that crashed or hung up on its own. A 502 where the connection cannot be made at all is sorted by message in another post.
[1] Read the end of the message. Two endings follow the same beginning, and the client sees different things. A test backend that closes on purpose reproduced both.
| Message ending | When the backend hung up | What the client got |
|---|---|---|
while reading response header from upstream | before sending a single byte of the response header | 502 |
while reading upstream | after sending the header and part of the body | status 200, body cut short |
Access log for the first case:
127.0.0.1 - - [25/Oct/2026:11:28:20 +0000] "GET /crash HTTP/1.1" 502 157 "-" "curl/8.14.1" "-"The second case leaves a 200 in the access log. The backend promised Content-Length: 1000, sent 10 bytes and closed. curl ends like this. Counting 5xx paths in the access log (find the paths behind 5xx) will not catch this case, so read error.log as well.
127.0.0.1 - - [25/Oct/2026:11:28:20 +0000] "GET /partial HTTP/1.1" 200 10 "-" "curl/8.14.1" "-"curl: (18) end of response with 990 bytes missing[2] Count which backend and which path is hanging up. Every line carries an upstream: address. The totals below come from calling /crash three times and /partial twice.
$ grep -o 'prematurely closed connection while reading [a-z ]*' error.log | sed 's/ from upstream//' | sort | uniq -c 3 prematurely closed connection while reading response header 2 prematurely closed connection while reading upstream$ grep -o 'upstream: "[^"]*"' error.log | sort | uniq -c 3 upstream: "http://127.0.0.1:9001/crash" 2 upstream: "http://127.0.0.1:9001/partial"If the hits cluster on one path, the code behind that request is dying. If they spread across paths, suspect something backend-wide: process restarts, memory pressure, a worker limit.
[3] Bypass Nginx and call the backend directly. If the same request is dropped there too, Nginx is not the cause. When the connection closes without a reply, curl exits with 52.
$ curl -sS -i http://127.0.0.1:9001/crashcurl: (52) Empty reply from serverIf the direct call works and only the proxied one fails, something Nginx passes differs: request size, headers, Host. Compare your proxy_set_header lines with the backend's request limits.
[4] Open the backend's own log for the same second. The answer to this error lives there, not in Nginx.
- The app server's error log and stack traces (did a worker die on an exception?)
- The process manager's restart history (does it recycle workers by age or request count?)
dmesg -T | grep -i 'out of memory'(did the kernel kill the process?)
[5] Know the retry rules. With two or more upstream servers, Nginx counts this error as a failure and may try the next server, depending on the request method. proxy_next_upstream defaults to error timeout, and error covers "an error occurred while … reading the response header". A POST, LOCK or PATCH that has already been sent to a server is not passed on unless non_idempotent is set (official docs). With two upstreams where only port 9001 hangs up:
GET /crash HTTP 200… [error] … upstream prematurely closed connection while reading response header from upstream, … request: "GET /crash HTTP/1.1", upstream: "http://127.0.0.1:9001/crash", …… [warn] … upstream server temporarily disabled while reading response header from upstream, … request: "GET /crash HTTP/1.1", upstream: "http://127.0.0.1:9001/crash", …127.0.0.1 - - [25/Oct/2026:11:30:51 +0000] "GET /crash HTTP/1.1" 200 3 "-" "curl/8.14.1" "-"The GET logged the error, but the other server answered and the client got 200. A freshly started Nginx whose first request is a POST behaves differently:
POST /crash HTTP 502127.0.0.1 - - [25/Oct/2026:11:31:01 +0000] "POST /crash HTTP/1.1" 502 157 "-" "curl/8.14.1" "-"The POST got 502 with no retry. The temporarily disabled warning means the failed server is skipped for fail_timeout (10 seconds by default) with max_fails at its default of 1. The test config had no zone, and the official docs describe zone as the shared-memory area that lets worker processes share run-time state. So without a zone and with several workers, failure counts may be kept per worker, and later POSTs that land on another worker can still return 502 for a while (how many was not measured). With a single upstream, every method gets a 502. With several, the first POST to hit the bad server still fails.
[6] Raising proxy_read_timeout does nothing for this error: the peer closed the connection, nothing timed out. Timeouts are logged separately as upstream timed out (the 504 post).
Analyze your Nginx log in the browser ↗ — nothing is uploaded.
Check it with the HTTP status codes tool ↗ — calculated in your browser.