WebSocket upgrade handling
A connection that changes protocol
A chat client may need to send a message at any time and receive a reply without making a new HTTP request. WebSocket provides a bidirectional message channel. In the classic HTTP/1.1 handshake, the client requests a protocol change and a compatible server accepts it with status 101. Nginx then relays traffic between the two connections. A successful 101 is not proof that the application has authenticated the user or can exchange messages.
GET /chat HTTP/1.1Host: stream.testUpgrade: websocketConnection: UpgradeSec-WebSocket-Version: 13Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=The fixed example key is illustrative, not a value to hardcode in a client. Real clients generate a fresh random nonce and validate the server handshake. After 101, data consists of WebSocket frames, not a series of independent HTTP responses. The conventional HTTP/1.1 upgrade is distinct from extended CONNECT mechanisms in HTTP/2 or HTTP/3; do not assume those mechanisms are supported by this configuration.
Forward upgrade intent explicitly
Connection and Upgrade describe the current HTTP hop. The reverse proxy creates a separate upstream hop, so it must construct the fields needed by the backend. The configuration below explicitly selects upstream HTTP/1.1. This works on the older lab binary and avoids reliance on the default, which changed from 1.0 to 1.1 in Nginx 1.29.7. The WebSocket tunnel feature dates to 1.3.13.
location = /chat { proxy_pass http://127.0.0.1:19040; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade;}This is suitable for a dedicated upgrade endpoint, but always sending Connection: upgrade to a mixed HTTP endpoint is misleading. A map at http scope can select the upstream Connection value. The backend must still validate the handshake; the map does not authenticate clients.
map $http_upgrade $lab_connection { default close; ~*^websocket$ upgrade;}# Inside server:location = /chat { proxy_pass http://127.0.0.1:19040; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $lab_connection;}Verify more than the handshake
Use the Chapter 30 local origin and verify_http.py: ordinary HTTP to /chat should return 400, an upgraded session should echo two small messages and close, and a fresh session should work. A 400 may indicate missing upgrade fields, a 502 an unavailable origin; inspect the actual upstream response before deciding. For browser deployments validate Origin against an explicit allowlist at the application, use wss with verified TLS, authenticate and authorize messages, and never put reusable credentials in URL query strings. The fixture intentionally rejects browser Origin headers and is not production authentication.
Practice
Explain why the connection header is generated for the upstream hop. Predict what happens if the backend responds with 200 instead of 101. Next: server-sent events and buffering. Source: https://nginx.org/en/docs/http/websocket.html
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
WebSocket upgrade handling
Understand HTTP/1.1 WebSocket upgrades, forward the necessary headers, and verify a local two-way session.
What does a 101 response indicate in the classic WebSocket handshake?
Why explicitly reconstruct Connection and Upgrade?
What does the lab map return for a missing Upgrade value?
What should a browser-facing chat service additionally check?
Why set proxy_http_version 1.1 explicitly?