WebSocket closes after fixed idle period when routed through Nginx reverse proxy
Environment
Node.js backend using ws library, Nginx 1.22 as TLS-terminating reverse proxy on a Linux host, clients are browsers over internet and internal network.
Symptoms
WebSocket connection opens successfully, data flows, then connection closes after ~60s with no application-level close sent; backend logs show socket close; direct connections to backend (no proxy) remain open.
Observations
Nginx logs show no error; proxy_read_timeout set to default; upstream keepalive not configured; client is behind corporate NAT in some cases; no TLS renegotiation visible. Browser reports websocket closed; close code absent or 1006 (abnormal closure).
Already attempted
Increased proxy_read_timeout to 120s and proxy_send_timeout same; added proxy_set_header Connection 'upgrade' and Upgrade $http_upgrade; restarted Nginx; problem persisted at original interval on some clients.
Diagnostic steps
- Enable debug logging on Nginx (error_log with debug) and capture the proxy TCP flow around disconnect time; correlate timestamps with backend and client logs.
- What the result means: If Nginx logs an idle timeout or explicitly closes the upstream, it indicates proxy timeout. If Nginx shows no activity and backend logs show abrupt TCP FIN/RST, an intermediate (LB/NAT) likely closed the connection.
- Use tcpdump or ngrep on the proxy host to capture TCP packets for the websocket connection across the disconnect interval.
- What the result means: If a FIN/RST originates from the proxy host, proxy is closing. If packets show no FIN until client reports close, then an intermediate device may be dropping idle flows or injecting RSTs.
- Temporarily configure the backend or client to send application-level pings every 20–30s (ws.ping or websocket ping frames) and observe whether connection stays up beyond previous timeout.
- What the result means: If pings prevent disconnects, an idle-flow timeout on an intermediate device or proxy is likely. If pings do not help and logs show closed from proxy, adjust proxy timeouts instead.
- Verify Nginx websocket proxy directives: proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; map $http_upgrade $connection_upgrade { default upgrade; '' close; }
- What the result means: If these directives are missing, the handshake may be downgraded or not kept-alive properly. Correct config should maintain upgrade and reduce protocol-level issues.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
How to determine whether Nginx, an intermediate firewall, or backend TCP keepalive is causing the disconnect and how to safely test fixes?
Verified community troubleshooting
Collect packet traces at the proxy to see which box sends the FIN/RST at the disconnect time. Run tcpdump on the Nginx host for the socket 30s before and after the drop; if Nginx initiates close, raising proxy_read_timeout and keepalive_timeout may help. If the FIN originates upstream (load balancer/NAT), implement 20–30s application-level ping frames from server or client to keep the flow alive and ask infra to raise idle timeouts. Verify fixes by reproducing with and without pings and comparing tcpdump traces and timestamps.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Networking31% match
CORS preflight (OPTIONS) passes on localhost but fails when routed through Nginx reverse proxy — why?
Open1 answer - Web Development30% match
Nginx returns 504 Gateway Timeout only for backend requests over ~45s to my Node API
Open1 answer - Networking29% match
Chunked Transfer-Encoding uploads fail with 413 only through TLS-terminating NGINX proxy
Open0 answers - Web Development28% match
NGINX returns 413 for Transfer-Encoding: chunked uploads only when proxying to Node.js over HTTP/2 TLS offload
Solved1 answer - Web Development27% match
NGINX returns 504 Gateway Timeout for long-running uploads through a Node backend — how to diagnose?
Open1 answer