The Symptom
Symptom
Root Cause Analysis
Root Cause
This is a classic TCP race condition. Node.js has a default `keepAliveTimeout` of 5 seconds. Nginx has a default `keepalive_timeout` of 60 seconds (but was configured to 75s in our cluster). Nginx would reuse an idle TCP connection that had been open for 4.9 seconds and send a new request. At that exact millisecond, Node.js decided the connection was idle for 5 seconds and abruptly closed the TCP socket. Nginx received a RST packet while trying to send data, resulting in a 502 error for the user.
5 Whys Root Cause Drill-Down
Resolution
Resolution
// The fix: Ensure upstream timeout > proxy timeout
const server = app.listen(8080);
server.keepAliveTimeout = 65000; // 65 seconds
server.headersTimeout = 66000; // Keep-alive + 1s
Discussions 0