WebSocket proxying in nginx means forwarding WebSocket connections from clients to backend servers, allowing real-time communication through nginx as a middleman.
Click to reveal answer
beginner
Which nginx directive is essential to support WebSocket proxying?
The directive proxy_set_header Upgrade $http_upgrade; is essential to handle the WebSocket handshake upgrade request properly.
Click to reveal answer
intermediate
Why do we need to set proxy_http_version 1.1; for WebSocket proxying in nginx?
WebSocket requires HTTP/1.1 because it uses the Upgrade header to switch protocols. Setting proxy_http_version 1.1; ensures nginx uses the right HTTP version for the handshake.
Click to reveal answer
intermediate
What headers must nginx forward to support WebSocket connections?
nginx must forward the Upgrade and Connection headers to allow the WebSocket handshake to succeed.
Click to reveal answer
beginner
Show a minimal nginx configuration snippet to proxy WebSocket connections.
Why might WebSocket connections fail with this setup?
medium
A. Because Connection header should be "upgrade" not "keep-alive" for WebSocket
B. Because proxy_http_version should be 1.0 for WebSocket
C. Because Upgrade header should be hardcoded to "websocket"
D. Because proxy_pass URL is missing a trailing slash
Solution
Step 1: Check Connection header value
For WebSocket proxying, Connection header must be "upgrade" to signal protocol switch.
Step 2: Identify impact of incorrect Connection header
Using "keep-alive" prevents the protocol upgrade, causing WebSocket connection failure.
Final Answer:
Because Connection header should be "upgrade" not "keep-alive" for WebSocket -> Option A
Quick Check:
Connection header must be upgrade for WebSocket [OK]
Hint: Connection header must be "upgrade" for WebSocket [OK]
Common Mistakes:
Using keep-alive instead of upgrade
Setting HTTP version to 1.0
Hardcoding Upgrade header instead of using $http_upgrade
5. You want to proxy WebSocket connections through nginx to two backend servers for load balancing. Which configuration snippet correctly supports WebSocket proxying with load balancing?
hard
A. upstream ws_backend {
server backend1.example.com;
server backend2.example.com;
}
location /ws/ {
proxy_pass http://ws_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "close";
}
B. upstream ws_backend {
server backend1.example.com;
server backend2.example.com;
}
location /ws/ {
proxy_pass http://ws_backend;
proxy_http_version 1.0;
proxy_set_header Upgrade websocket;
proxy_set_header Connection "keep-alive";
}
C. upstream ws_backend {
server backend1.example.com;
server backend2.example.com;
}
location /ws/ {
proxy_pass http://ws_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
D. upstream ws_backend {
server backend1.example.com;
server backend2.example.com;
}
location /ws/ {
proxy_pass http://ws_backend;
proxy_http_version 2.0;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
Solution
Step 1: Confirm HTTP version and headers for WebSocket
HTTP/1.1 is required, and headers Upgrade and Connection must be set to allow protocol upgrade.
Step 2: Check load balancing setup and header correctness
upstream ws_backend {
server backend1.example.com;
server backend2.example.com;
}
location /ws/ {
proxy_pass http://ws_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
} uses correct HTTP version, headers, and defines upstream servers for load balancing properly.
Final Answer:
upstream ws_backend {
server backend1.example.com;
server backend2.example.com;
}
location /ws/ {
proxy_pass http://ws_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
} -> Option C