Networking

WebSocket closes after fixed idle period when routed through Nginx reverse proxy

Environment

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

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

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

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

Diagnostic steps

  1. 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.
  1. 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.
  1. 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.
  1. 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

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

Question

How to determine whether Nginx, an intermediate firewall, or backend TCP keepalive is causing the disconnect and how to safely test fixes?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
techtony Answer
user
Posted 2w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 2w ago• Trouble ID

Most likely causes

Intermediate load balancer/NAT closes idle TCP connections after a fixed timeout 75%
Nginx proxy has an idle timeout (proxy_read_timeout/keepalive_timeout) lower than observed lifetime 70%
Backend not sending periodic pings and client not configured to respond, leading intermediates to treat connection as idle 65%

Summary

Common causes: Nginx proxy timeouts (proxy_read_timeout, proxy_send_timeout, keepalive_timeout), missing proxy headers or websocket-specific directives, upstream idle timeouts at load balancer or NAT, or lack of TCP keepalives. Diagnosis should confirm which hop is closing the connection and whether periodic pings prevent the drop.

Suggested Fixes

Diagnostic step 1
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. Result meaning: 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.
Diagnostic step 2
Use tcpdump or ngrep on the proxy host to capture TCP packets for the websocket connection across the disconnect interval. Result meaning: 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.
Diagnostic step 3
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. Result meaning: 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.

Continue troubleshooting

Matched from the title, description and tags