How-To Guide

How to Configure WebSockets Through a Reverse Proxy

Proxy WebSocket connections correctly: Upgrade headers, timeouts, sticky routing and scale, so ws:// sockets survive the proxy tier.

On this page

  1. Introduction
  2. What the Upgrade Needs
  3. The Critical Headers
  4. Timeouts and Idle Connections
  5. Sticky Routing and Scaling
  6. Draining and Deploys
  7. Secure WebSockets and Headers
  8. Common Pitfalls and Conclusion

Introduction

A WebSocket connection starts as an HTTP request with an Upgrade header and then becomes a long-lived bidirectional socket. That transition is exactly what a reverse proxy must be told to allow: by default a proxy may buffer the response, close idle connections, and not forward the upgrade headers, so the ws:// connection fails or drops. Configuring the proxy means passing the upgrade, disabling buffering, setting long read timeouts, and planning for how it routes and scales long-lived connections.

What the Upgrade Needs

The client sends Connection: Upgrade and Upgrade: websocket with the request. The server answers with 101 Switching Protocols and the connection becomes a WebSocket. A proxy in the middle must forward those headers and not interfere with the switch, or the handshake fails.

The proxy must also not buffer the connection, because a WebSocket is a stream of frames, not a request/response pair. And it must keep the connection open for a long time, so its read timeout must exceed the expected connection lifetime or it will close idle sockets.

The proxy must also preserve the request’s identity: the client address, the host and any authentication headers, so the application can authorise and log the connection after the switch. A proxy that drops or rewrites these breaks the handshake’s context.

# nginx: proxy a WebSocket endpoint
location /ws/ {
  proxy_pass http://app;
  proxy_http_version 1.1;                       # required for Upgrade
  proxy_set_header Upgrade $http_upgrade;        # pass the Upgrade header
  proxy_set_header Connection "upgrade";         # required alongside Upgrade
  proxy_read_timeout 3600s;                       # keep long-lived sockets open
  proxy_buffering off;                            # do not buffer frames
  proxy_set_header Host $host;
}

The Critical Headers

Three settings make or break the upgrade: HTTP version 1.1 (the Upgrade mechanism needs it), the Upgrade header forwarded from the client, and the Connection header set to upgrade. Miss any and the proxy strips the upgrade and the client gets a plain HTTP response, not a WebSocket.

The Upgrade header value is client-specific (websocket), so forward it from the client ($http_upgrade) rather than hard-coding it, in case of future upgrade types. The Connection header, however, is often hard-set to upgrade for this location, since the client’s Connection header may contain other tokens.

# the three settings that matter
proxy_http_version 1.1                -> the Upgrade mechanism needs 1.1
proxy_set_header Upgrade $http_upgrade -> forward the client's Upgrade value
proxy_set_header Connection "upgrade"  -> signal the switch
# miss any => the proxy strips the upgrade; the client gets plain HTTP, not WS

Timeouts and Idle Connections

A WebSocket can be idle between messages, and a proxy read timeout will close it if no data flows within the timeout. Set the read timeout longer than the expected idle period, or, better, have the application send periodic pings so there is activity and the connection stays alive.

The idle-timeout setting must exceed the ping interval, so the pings refresh the timeout before it fires. This is the same keepalive discipline as gRPC: the application-level ping is what keeps the long-lived connection through intermediaries that time out idle flows.

# keeping the WebSocket alive
proxy_read_timeout  >  the app's ping interval
app sends a ping every N seconds -> refreshes the proxy timeout, keeps it open
# without pings, an idle socket hits the read timeout and is closed
# the ping is the keepalive; the proxy timeout just needs to be longer

Sticky Routing and Scaling

A WebSocket is a single long-lived connection to one backend, so load balancing must route it to that backend for its lifetime. If the proxy rebalances mid-connection, the socket breaks. Use a load-balancing policy that is stable per connection (least-connections or a hash of connection id), and do not assume round-robin per request.

Scaling means more backend instances and connections spread across them, but each connection is pinned. Plan the connection count per instance and the proxy’s capacity to hold many open connections, because each WebSocket holds a connection open on both the proxy and the backend.

# long-lived connections and balancing
[ ] balancing must be stable PER CONNECTION (least_conn / connection hash)
[ ] a rebalance mid-connection breaks the socket
[ ] each WebSocket pins a backend + a proxy connection => plan capacity
[ ] on deploy, drain: stop accepting new, let existing sockets finish or migrate

Draining and Deploys

A deploy that restarts a backend closes every WebSocket to it, and every client reconnects. Drain deliberately: stop routing new connections to the instance, tell existing clients to reconnect (a reconnect with backoff on the client), and give them a deadline before the process exits.

Because WebSockets are long-lived, a rolling deploy with abrupt shutdown causes a reconnect storm. Stagger the drain, and have the client reconnect with jitter so the backends are not hit by every client at once.

# deploy drain for WebSockets
1. stop routing NEW connections to the instance
2. signal existing clients to reconnect (or let them on close)
3. wait for a drain window, then close the remaining sockets
4. client reconnects with backoff + jitter to avoid a reconnect storm
# abrupt shutdown => every socket to that instance drops at once

Secure WebSockets and Headers

A secure WebSocket uses wss:// (WebSocket over TLS), and the proxy terminates TLS as for any HTTPS traffic. Preserve the original client address using the proxy protocol or X-Forwarded-For, so the application sees the real client for rate limiting and logging.

Authentication happens at the handshake: the client sends credentials (a cookie, an Authorization header or a token in the URL) with the Upgrade request, and the server validates before accepting the switch. Because the connection is long-lived, token expiry mid-connection must be handled, either by re-authenticating over the socket or by closing it.

# secure WebSockets
wss://  = WebSocket over TLS; the proxy terminates TLS as for HTTPS
preserve the real client (proxy protocol / X-Forwarded-For) for rate limiting
auth at the handshake: cookie, Authorization header, or token in the request
# long-lived: handle token expiry mid-connection (re-auth or close)

Common Pitfalls and Conclusion

Do not forget proxy_http_version 1.1 or the Upgrade and Connection headers; without them the handshake becomes plain HTTP. Do not leave buffering on, which breaks the frame stream. Do not set a short read timeout without application pings. Do not use per-request round-robin for a long-lived connection. And do not shut down abruptly, causing a reconnect storm.

A reverse proxy must be told to allow the WebSocket upgrade: forward Upgrade and Connection with HTTP/1.1, disable buffering, set a long read timeout refreshed by application pings, keep routing stable per connection, and drain on deploys. Then a ws:// or wss:// connection passes through the proxy tier and stays open, instead of failing the handshake or dropping on an idle timeout.

Sources: RFC 6455: The WebSocket Protocol, NGINX: WebSocket proxying, MDN: WebSocket API.

FAQ

Why does my WebSocket fail at the proxy?
Usually because the proxy is not forwarding the upgrade: missing proxy_http_version 1.1, or a missing Upgrade / Connection header. Without them the proxy treats the request as plain HTTP and the 101 switch never happens.
Why do WebSockets drop after a while?
Because the proxy read timeout closed an idle connection. Set the read timeout longer than the expected idle period, and have the application send periodic pings so there is traffic to refresh the timeout before it fires.

Related Examples