How do you handle WebSocket proxying with Nginx? nginx

How do you handle WebSocket proxying with Nginx?


Nov. 13, 2023

How do you handle WebSocket proxying with Nginx?

WebSocket is a communication protocol that provides full-duplex communication channels over a single TCP connection. It allows real-time data streaming between clients and servers, making it ideal for applications that require constant updates.

When using Nginx as a reverse proxy server, handling WebSocket connections requires some additional configuration to ensure proper forwarding of WebSocket traffic.

1. Configuring Nginx

To enable WebSocket proxying, you need to configure Nginx to pass the WebSocket connections. Below is an example Nginx configuration:

server {
   listen 80;
   server_name example.com;

   location / {
      proxy_pass http://backend;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
   }
}

In this configuration, the HTTP requests are forwarded to the "backend" server while preserving the necessary headers for WebSocket communication.

2. Enabling WebSocket support

In some cases, you might have to explicitly enable WebSocket support in Nginx. This can be done by recompiling Nginx with the additional "--with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-cc-opt='-DNGX_HTTP_HEADERS -DNGX_HTTP_STUB_STATUS' --with-stream_ssl_preread_module" options.

3. Handling WebSocket subprotocols

If your WebSocket application requires the use of subprotocols, you can specify them in the Nginx configuration using the "proxy_set_header Sec-WebSocket-Protocol" directive. For example:

server {
   listen 80;
   server_name example.com;

   location / {
      proxy_pass http://backend;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_set_header Sec-WebSocket-Protocol "my-custom-protocol";
   }
}

Make sure to replace "my-custom-protocol" with the desired subprotocol.

4. Handling load balancing

If you are using Nginx in a load-balancing setup, you need to configure sticky sessions to ensure WebSocket connections are maintained with the same upstream server. This can be achieved using the "ip_hash" directive within the "upstream" block. For example:

upstream backend {
   ip_hash;
   server backend1.example.com;
   server backend2.example.com;
   server backend3.example.com;
}

The "ip_hash" directive ensures that WebSocket connections originating from the same IP address are always routed to the same backend server, maintaining session consistency.

Conclusion

Nginx provides powerful capabilities for handling WebSocket proxying. By following the steps outlined above, you can ensure proper configuration and support for WebSocket connections between clients and backend servers, enabling real-time communication in your web applications.

nginx