1. 程式人生 > >nginx配置支持https和wss(websocket)協議

nginx配置支持https和wss(websocket)協議

cat 監聽 list protoc protocol clas conn ati www.

server {
        listen 80;
        listen 443 ssl http2;
        server_name lyz810.com;
 
        root /website/lyz810-main;
 
        ssl_certificate     certificate/lyz810.com.crt;
        ssl_certificate_key certificate/lyz810.com.key;
 
        location /websocket/ {
                internal;
 
                
if ( $http_sec_websocket_protocol = "" ) { return 400; } proxy_pass http://127.0.0.1:$arg_port; proxy_http_version 1.1; proxy_set_header X-Client-IP $remote_addr; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection
"upgrade"; proxy_read_timeout 300s; } location /chatRoom { rewrite .* /websocket/?port=8100; } location /chatRobot { rewrite .* /websocket/?port=8101; } }

服務器內部起了2個websocket服務,分別監聽8100、8101端口nginx根據訪問不同的location,選擇代理到那個ws服務商

/websocket/是個內部location,直接訪問會報錯

參考鏈接:

http://www.cnblogs.com/weidiao/p/7389744.html

http://www.cnblogs.com/mzsg/p/5623494.html

nginx配置支持https和wss(websocket)協議