nginx 502 'upstream prematurely closed connection' with keepalive to Node
We started seeing occasional 502s on an internal reporting endpoint. The request took about 90 seconds, which was the first clue, because everything else on that service answered in under a second.
The stack was nginx 1.24.0 in front of a Node 20 service, both on the same host, with nginx proxying to 127.0.0.1:8080. There was no load balancer in between and no obvious resource pressure.
## The symptom
About one request in fifteen died with a bare 502 page. Retrying usually worked. The Node service showed no error at all, which is what made this annoying: from the application's point of view the response was sent.
The nginx error log had this:
2024/03/11 14:22:07 [error] 1234#1234: *5678 upstream prematurely closed connection while reading response header from upstream, client: 10.0.1.4, server: api.internal, request: "GET /v1/report HTTP/1.1", upstream: "http://127.0.0.1:8080/v1/report", host: "api.internal"
And a moment later, on a different request:
2024/03/11 14:22:09 [error] 1234#1234: *5681 recv() failed (104: Connection reset by peer) while reading response header from upstream
## Reading the error line
while reading response header from upstream means nginx had sent the request and was waiting for the first byte of the response. The connection closed before any headers arrived. That rules out anything in the response body and points at the connection itself.
Two candidates: the upstream closed an idle connection that nginx reused, or something in the middle timed out. There was nothing in the middle, so we looked at keepalive.
## keepalive on one side only
Our nginx config had an upstream block with keepalive 32, and the location block used HTTP/1.1:
nginx
upstream node_api {
server 127.0.0.1:8080;
keepalive 32;
}server {
location /v1/ {
proxy_pass http://node_api;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_read_timeout 120s;
}
}
That part was correct. The problem was on the Node side. We were using the built-in http.createServer() with its default keepAliveTimeout of 5 seconds, while nginx's pool was perfectly happy to hold an idle connection much longer. nginx would pull a connection out of the pool, send a request, and Node had already decided that connection was stale and closed it.
The race only showed up under low traffic, when connections sat idle long enough. During a busy period the keepalive pool stayed warm and the 502s vanished, which is why this looked like a phantom.
## What we changed
We raised the server timeout above nginx's idle pool timeout:
js
const server = http.createServer(app);
server.keepAliveTimeout = 65_000;
server.headersTimeout = 70_000;
server.listen(8080, '127.0.0.1');
headersTimeout has to be larger than keepAliveTimeout, otherwise Node closes the socket while it is still waiting for headers and you get a new, different warning in the log. We also set keepalive_timeout 65s in the nginx upstream block so the two sides agree.
The general rule I took away: if you enable keepalive on a proxy, go and look at the upstream server's idle timeout too. A one-sided keepalive configuration is worse than no keepalive, because it turns a clean reconnect into a race.