Home Nginx WebSocket WebSocket ProxyingDefinition WebSockets start as HTTP, then switch protocols with an Upgrade handshake. Behind a reverse proxy those hop-by-hop headers are not forwarded automatically.
Nginx can tunnel the connection when the backend returns 101 Switching Protocols. You must pass Upgrade and Connection explicitly with proxy_set_header.
Config Start with this shape. Adjust paths for your server.
location /chat/ {
proxy_pass http://backend;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
} On older Nginx builds you may also need proxy_http_version 1.1;. Current docs note that line mainly for versions before 1.29.7.
Key Directives Name Plain meaning proxy_set_header UpgradeForward the client Upgrade header to the backend. proxy_set_header ConnectionSend upgrade (or a mapped value) so the backend can switch protocols. map $http_upgradeSet Connection to upgrade only when Upgrade is present. proxy_read_timeoutIncrease if idle sockets close after the default 60s.
Examples 1. Forward Upgrade Headers 2. Map Connection from Upgrade 3. Avoid Idle Timeouts Example 1 — Forward Upgrade Headers Minimum lines for a WebSocket path behind Nginx.
location /chat/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
} How It Works Upgrade tells the backend the client wants WebSocket.Connection upgrade completes the hop-by-hop requirement.Host still helps the app see the public hostname.Example 2 — Map Connection from Upgrade A map keeps normal HTTP requests from always sending Connection: upgrade.
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
location /chat/ {
proxy_pass http://backend;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
} How It Works Empty Upgrade maps to close.Any other value maps to upgrade.Safer default for mixed HTTP + WS locations.Example 3 — Avoid Idle Timeouts By default Nginx may close a quiet proxied connection after 60 seconds.
location /chat/ {
proxy_pass http://backend;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600;
} How It Works proxy_read_timeout extends how long Nginx waits for backend data.App pings also keep the socket alive.Match timeouts to your chat or game needs.Frequently Asked Questions Why do WebSockets fail behind Nginx? Usually Upgrade/Connection were not set, the backend is down, or a timeout closed the idle tunnel.
Do I need this for normal HTTP APIs? No. Only for protocol upgrades such as WebSocket.
Is this related to reverse proxy? Yes. It is the same proxy_pass path with extra headers. See the reverse proxy guide first.
What is status 101? Switching Protocols — the backend accepted the Upgrade. Nginx then tunnels bytes both ways.
Can I terminate WSS (TLS WebSocket)? Yes. Configure HTTPS on Nginx and proxy to ws:// or http:// on localhost, or to a TLS backend if required.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion