Sub topic 12
Proxying WebSocket Connections
This chapter assumes you understand HTTP requests, response headers, Nginx server and location blocks, and upstream groups. It also assumes that a WebSocket application is running on a local or reachable TCP port.
Why WebSockets need special proxy settings
A WebSocket starts as an HTTP request. The client asks the server to upgrade the connection by sending these headers:
Upgrade: websocketConnection: Upgrade
If the server accepts, the connection changes from a normal HTTP exchange into a long-lived, two-way WebSocket connection.
Upgrade and Connection are hop-by-hop headers. A proxy does not automatically pass them to the next server. Nginx must set them explicitly. Nginx must also use HTTP/1.1 for the connection to the upstream application.
The connection flow looks like this:
Configure the upgrade
The map directive converts the client's Upgrade header into a safe Connection value. Put the map in the http context, not inside a server or location block.
This example defines an upstream application and proxies requests under /ws/:
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream app_backend {
server 127.0.0.1:3000;
}
server {
listen 80;
server_name example.com;
location /ws/ {
proxy_pass http://app_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
}
}When the client sends Upgrade: websocket, the map sets Connection to upgrade. For an ordinary HTTP request without an upgrade header, it sets Connection to close. This prevents Nginx from sending an inappropriate upgrade request to the backend.
The proxy_read_timeout value controls how long Nginx waits while receiving data from the upstream. It measures inactivity, not the total lifetime of the WebSocket. A connection can remain open longer than 300 seconds if messages or ping frames continue to arrive.
Common errors
- HTTP 400 or 426: Check that the client uses the correct WebSocket URL and that both upgrade headers reach the backend.
- HTTP 502: Check that the application is listening on the configured address and port. Also check that local firewall rules allow the connection.
- Connections close after one minute: Increase
proxy_read_timeout, and ensure the application sends periodic ping or heartbeat frames. - The application sees the wrong host or scheme: Pass
HostandX-Forwarded-Proto, then configure the application to trust those headers only from your Nginx proxy. - Only some paths work: Check Nginx location matching and the URI sent by
proxy_pass.
Validate and reload the configuration before testing:
sudo nginx -t && sudo systemctl reload nginxUse a WebSocket client such as wscat to connect to ws://example.com/ws/. Watch the Nginx error log and the application log while connecting.
Exercise
Why must a WebSocket location set both proxyhttpversion 1.1 and the Upgrade and Connection headers?
Show answer
WebSocket upgrades require HTTP/1.1, and Upgrade and Connection are hop-by-hop headers that Nginx does not forward automatically.