Complete Guide to NGINX Reverse Proxy for WebSocket with SSL (wss://) Enablement

Nov 23, 2025 · Programming · 9 views · 7.8

Keywords: NGINX | WebSocket | SSL | reverse_proxy | wss

Abstract: This article provides a comprehensive guide on configuring NGINX as a reverse proxy for WebSocket connections with SSL encryption to achieve wss:// protocol. Based on official NGINX documentation and community best practices, it includes complete configuration examples and in-depth technical analysis covering HTTP/1.1 upgrade mechanism, proxy header settings, SSL certificate configuration, and other key concepts, enabling secure WebSocket communication without modifying backend servers.

Fundamental Principles of NGINX WebSocket Proxying

The WebSocket protocol establishes persistent connections through HTTP/1.1 upgrade mechanism. When acting as a reverse proxy, NGINX must properly handle Upgrade and Connection header fields to forward WebSocket traffic. Since version 1.3.13, NGINX natively supports WebSocket proxying without requiring additional modules.

Core Configuration Implementation

The following configuration demonstrates the basic structure for NGINX WebSocket proxying:

location /websocket/ {
    proxy_pass http://backend_host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;
}

Key configuration explanations:

SSL Encryption Integration

Enable SSL in the server block to upgrade regular WebSocket to secure wss://:

server {
    listen 443 ssl;
    server_name example.com;
    
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    
    location /websocket/ {
        proxy_pass http://backend_host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 86400;
    }
}

Advanced Configuration Optimization

For production environments, consider adding the following optimizations:

location /websocket/ {
    proxy_pass http://backend_host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    
    proxy_read_timeout 86400;
    proxy_send_timeout 86400;
    proxy_connect_timeout 60;
    
    # Buffer optimizations
    proxy_buffering off;
    proxy_buffer_size 4k;
}

Alternative Approach Comparison

While early implementations required third-party modules like nginx_tcp_proxy_module for TCP-level proxying, the official HTTP module has provided complete WebSocket support since NGINX 1.3.13. The official solution offers better compatibility and maintainability, making it the recommended approach.

Troubleshooting and Debugging

Common issues and solutions:

Performance Optimization Recommendations

For high-concurrency WebSocket applications:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.