WebSocket proxying in Nginx - Time & Space Complexity
When nginx proxies WebSocket connections, it handles data streams between clients and servers.
We want to understand how the work grows as more messages pass through the proxy.
Analyze the time complexity of the following nginx WebSocket proxy configuration.
location /ws/ {
proxy_pass http://backend_server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
This config forwards WebSocket requests to a backend server, maintaining the connection for real-time data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx reads and forwards each WebSocket message between client and server.
- How many times: This happens for every message sent during the connection lifetime.
As the number of messages increases, nginx processes each one in turn.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 messages | 10 forwarding operations |
| 100 messages | 100 forwarding operations |
| 1000 messages | 1000 forwarding operations |
Pattern observation: The work grows directly with the number of messages passing through.
Time Complexity: O(n)
This means the time to handle WebSocket proxying grows linearly with the number of messages.
[X] Wrong: "nginx processes all WebSocket messages instantly regardless of count."
[OK] Correct: Each message requires processing and forwarding, so more messages mean more work.
Understanding how nginx handles WebSocket streams helps you explain real-time proxying performance clearly and confidently.
"What if nginx buffered multiple WebSocket messages before forwarding? How would the time complexity change?"
Practice
proxy_set_header Upgrade $http_upgrade; in nginx when proxying WebSocket connections?Solution
Step 1: Understand the Upgrade header role
The Upgrade header tells the server to switch from HTTP to another protocol, here WebSocket.Step 2: Recognize nginx's proxy_set_header usage
Settingproxy_set_header Upgrade $http_upgrade;passes the client's Upgrade header to the backend, enabling protocol switching.Final Answer:
To tell nginx to allow the connection to switch protocols to WebSocket -> Option AQuick Check:
Upgrade header enables protocol switch = A [OK]
- Confusing Upgrade with client IP headers
- Thinking it enables compression
- Assuming it disables caching
Solution
Step 1: Identify required HTTP version for WebSocket
WebSocket proxying requires HTTP/1.1 because HTTP/1.0 does not support the Upgrade header properly.Step 2: Check correct headers for WebSocket
The Upgrade header must be passed as$http_upgradeand Connection header must be "upgrade" to maintain the protocol switch.Final Answer:
proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; -> Option DQuick Check:
HTTP/1.1 + Upgrade + Connection upgrade = B [OK]
- Using HTTP/1.0 instead of 1.1
- Setting Connection to keep-alive or close
- Hardcoding Upgrade header instead of using $http_upgrade
proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://backend_server;
What will happen if a client tries to establish a WebSocket connection through nginx?
Solution
Step 1: Analyze HTTP version and headers
The config uses HTTP/1.1 and sets Upgrade and Connection headers correctly for WebSocket proxying.Step 2: Understand nginx behavior with this config
nginx will forward the WebSocket upgrade request to the backend, enabling a successful WebSocket connection.Final Answer:
The WebSocket connection will be successfully proxied to the backend server. -> Option BQuick Check:
Correct HTTP version and headers = successful proxy [OK]
- Thinking HTTP/2 is required for WebSocket
- Assuming protocol upgrade is automatic without headers
- Believing nginx caches WebSocket connections
proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "keep-alive"; proxy_pass http://backend;
Why might WebSocket connections fail with this setup?
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 AQuick Check:
Connection header must be upgrade for WebSocket [OK]
- Using keep-alive instead of upgrade
- Setting HTTP version to 1.0
- Hardcoding Upgrade header instead of using $http_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 CQuick Check:
HTTP/1.1 + Upgrade + Connection upgrade + upstream = A [OK]
- Using HTTP/1.0 or 2.0 instead of 1.1
- Setting Connection to keep-alive or close
- Hardcoding Upgrade header instead of using $http_upgrade
Ready to prove it?
Pass the test and this pattern moves unknown
