Skip to content

Proxying WebSockets through Nginx — the Upgrade dance and the timeouts that matter

How to configure Nginx to proxy WebSocket connections, the HTTP/1.1 Upgrade headers, and the read/send timeouts that turn 60-second drops into long-lived sessions.

A WebSocket connection starts life as an ordinary HTTP/1.1 request that asks to be upgraded. Nginx has to pass that upgrade request through to the upstream, keep the resulting TCP connection alive, and not assume the connection is idle just because data is sparse. The configuration is short — four proxy_set_header lines and a generous timeout — but every misconfiguration shows up the same way: a client that connects, runs for 60 seconds, and dies.

How to verify

See whether WebSocket traffic is flowing:

sudo nginx -T | grep -E 'Upgrade|proxy_read_timeout|proxy_http_version'
# from a host with wscat installed
wscat -c wss://example.com/ws
# or curl, which speaks the upgrade and prints headers
curl -i -N \
  -H "Connection: Upgrade" -H "Upgrade: websocket" \
  -H "Sec-WebSocket-Key: $(openssl rand -base64 16)" \
  -H "Sec-WebSocket-Version: 13" \
  https://example.com/ws

A working upstream returns HTTP/1.1 101 Switching Protocols. Anything else (200, 404, 502, 504) is a misconfiguration somewhere.

What’s happening

The WebSocket handshake is an HTTP/1.1 request with Connection: Upgrade and Upgrade: websocket. The server responds 101, and from then on the TCP socket carries WebSocket frames, not HTTP. Nginx by default strips the Connection and Upgrade headers (they are hop-by-hop in HTTP/1.1) — so the upstream never sees the upgrade and the request is treated as a normal GET.

To make it work you have to manually re-add those headers on the upstream side, and you have to use HTTP/1.1 (proxy_http_version 1.1). Once upgraded, the connection can sit idle for minutes between frames — proxy_read_timeout 60s (Nginx default) closes the connection, killing the WebSocket.

WebSocket over TLS is wss:// — Nginx terminates TLS as normal, the upstream sees plain HTTP, the upgrade happens after the TLS handshake.

The procedure

  1. The minimum config for a WebSocket location:
    location /ws/ {
        proxy_pass         http://websocket_upstream;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade    $http_upgrade;
        proxy_set_header   Connection "upgrade";
    
        proxy_set_header   Host       $host;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
    proxy_set_header Connection "upgrade" is the static string — do not use $http_connection because the client may send keep-alive, upgrade and Nginx will faithfully pass that, which some upstreams reject.
  2. If only some requests are upgrades (e.g. a single /api/ location handles both REST and WebSocket), use a map:
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    
    location /api/ {
        proxy_pass http://api_upstream;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade    $http_upgrade;
        proxy_set_header   Connection $connection_upgrade;
        # ...
    }
    Non-upgrade requests get Connection: close (or keepalive if you have configured that on the upstream); upgrade requests get the dance.
  3. Decide the idle timeout. WebSocket sessions are long-lived; default proxy_read_timeout 60s is too short. We set 3600s (one hour) by default. If your protocol has its own ping/pong heartbeat, you can leave it shorter — when the heartbeat misses, the connection should fail-fast on both sides.
  4. Buffering — turn it off for WebSocket. The default proxy_buffering on does not actually buffer WebSocket frames (Nginx detects the upgrade), but explicit-off avoids confusion:
    proxy_buffering off;
    proxy_cache off;
  5. If your upstream is a Node.js process, set app.set('trust proxy', 'loopback') (Express) or its equivalent so the app reads X-Real-IP correctly. Otherwise, your “who connected” log shows 127.0.0.1 for everyone.
  6. Reload and validate with the curl command above. The 101 Switching Protocols is the unambiguous success signal.
  7. Watch for upstream prematurely closed connection in error.log — that means the upstream dropped the WebSocket, often because of its own idle timeout (Node’s default HTTP server has a keepAliveTimeout that closes idle sockets).

Common pitfalls

  • Forgetting proxy_http_version 1.1. Nginx defaults to HTTP/1.0 to upstreams, which has no Upgrade. The handshake silently 404s upstream.
  • Using Connection: $http_connection (the literal variable) instead of Connection: "upgrade". The variable passes through whatever the client sent, including bizarre vendor-specific extras.
  • proxy_read_timeout 60s left at default. WebSocket disconnects exactly one minute after the last frame. Looks like a flaky network.
  • TLS-only WebSocket terminator with HTTP/1.0 upstream (e.g. older Java app servers). Some old servers do not parse the Upgrade correctly. Update or shim with proxy_set_header Origin ""; if Origin mismatch is the failure.
  • Long-lived sockets pile up under unexpected load. Make sure worker_connections is sized for the maximum-concurrent-WebSockets count, not just request-per-second.

In the engagements we run, WebSocket-bearing services are the most opinionated about Nginx config — sockets that drop on the hour, headers that disappear on upgrade, behind-CDN pain. We template the WebSocket block per service and monitor connection longevity as a first-class signal in managed operations.