NGINX通过允许一个在客户端和后端服务器之间建立的隧道来支持WebSocket。为了NGINX发送来至于客户端Upgrade请求到后端服务器,Upgrade和Connection头部必须被设置明确。 

示例,这里我们使用nginx来做代理。

nginx的主配置文件有“ include /opt/nginx/vhost/*.conf;”在另外一个虚拟主机配置代理项目加上如下两个参数:

proxy_set_header Upgrade $http_upgrade;             
proxy_set_header Connection "upgrade";


支持WebSocket

vi /opt/nginx/vhost/test8182.conf
#临时开放8182端口
    server {
        listen       8182;
        server_name  localhost;
        large_client_header_buffers 4 128k;
        client_max_body_size 300m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        proxy_buffer_size 64k;
        proxy_buffers   4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
#测试环境
        location /ws  {
           index  index.html index.htm index.jsp;
           proxy_pass  http://192.168.1.18:8082/ws;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $host:$server_port;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection "upgrade";
        }
    }