Web Development

NGINX returns 502 on WebSocket upgrade when TLS session resumption and upstream keepalive enabled

noahnode
1mo ago
Environment

Environment

Ubuntu 22.04, NGINX 1.22 from official apt repo, TLS terminated on NGINX (client SSL), proxy_pass to upstream Node.js 16.14 (keepalive 32), upstream uses HTTP/1.1 and websockets

Symptoms

Symptoms

WebSocket clients intermittently receive HTTP/1.1 502 Bad Gateway during the Upgrade handshake. Failures happen only after some time of steady traffic; subsequent reconnect succeeds. Normal HTTP(S) requests to same upstream are unaffected.

Observations

Observations

NGINX error.log shows: "upstream prematurely closed connection while reading response header from upstream" coinciding with Upgrade requests. Upstream keepalive_pool shows many idle connections. tcpdump shows client TLS handshake resumption from same IP then sudden FIN from upstream; no Node.js errors logged.

Already attempted

Already attempted

Increased proxy_read_timeout, set proxy_http_version 1.1, added proxy_set_header Connection 'Upgrade', toggled keepalive_timeout and reduced keepalive to 8; problem reduced but still occurs. Restarting Node.js clears issue temporarily.

Question

Question

What causes Upgrade (WebSocket) handshakes to get 502 only when TLS session resumption and upstream keepalive are enabled, and how to make NGINX reliably proxy WebSocket Upgrade connections without breaking long-lived TLS resumption or upstream keepalive?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
aliceadmin Answer
user
Posted 1mo ago • 0 confirmations

First check backend idle timeouts: make Node.js keepAliveTimeout longer than NGINX's proxy_read_timeout so NGINX doesn't reuse a socket the app has already closed. As a quick mitigation, avoid reusing backend sockets for Upgrade by mapping $http_upgrade and forcing Connection: close for non-upgrade flows (so Upgrade gets a fresh backend handshake). Verify by opening a persistent WebSocket and repeatedly reconnecting with TLS session resumption; failures should stop. Roll back by restoring previous keepalive values if new behavior causes load issues and instead enable proxy_next_upstream retry with short keepalive.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Upstream (Node.js) silently closed idle keepalive sockets earlier than NGINX expects (idle timeout mismatch) 90%
Upstream process closes connections when TLS session-resumed requests cause different connection handling (edge case in app code resetting sockets on resumed connections) 30%
OS-level ephemeral port reuse or TCP TIME_WAIT boundary causing transient socket reuse issues between NGINX and upstream 20%

Summary

502 occurs when NGINX attempts to reuse an upstream keepalive connection that the upstream has already closed; with TLS session resumption enabled, clients attempt fast reconnects and NGINX reuses backend sockets for Upgrade requests. If a backend has closed the socket (silent idle timeout, or backend closed on TLS session mismatch), NGINX observes a premature close during header read and returns 502. Reduced keepalive lowered frequency, but root cause is mismatch between backend idle timeouts and NGINX keepalive reuse for Upgrade handshakes.

Suggested Fixes

Diagnostic step 1
Check and align timeouts: inspect Node.js server keepAliveTimeout and NGINX keepalive_timeouts. Set Node.js server.keepAliveTimeout > NGINX proxy_read_timeout + a margin.
Diagnostic step 2
Disable NGINX reuse for WebSocket Upgrade requests by adding proxy_set_header Connection $connection_upgrade and set proxy_http_version to 1.1 and add: map $http_upgrade $connection_upgrade { default 'upgrade'; '' 'close'; } to force new backend TCP handshake on Upgrade.
Diagnostic step 3
If keepalive reuse is required, enable proxy_next_upstream to retry on error and lower keepalive connections to reduce stale sockets.

Continue troubleshooting

Matched from the title, description and tags