用nginx反向代理功能将WS转为加密websocket (wss)
原来写的服务器端程序支持websocket非加密协议,即仅支持 ws://ip/file 形式的访问,现在网站的主协议变成了https了,再通过ws://协议来访问原服务器,浏览器阻止了。查找原因就是,如果一个网页的协议是https的,则内部的子连接必须也是安全套接字,如果原来是ws协议,则现在必须变为wss协议。
由于时间紧,来不及修改原服务器了。发现nginx可以将ws协议通过反向代理的方式转为wss,目前苟且采用这种替代方案吧。
nginx配置成websocket的反向代理的配置文件如下 :
- server {
- listen 443;
- server_name live.xxxxx.com;
-
- access_log logs/live.xxxxx.log main;
-
- ssl on;
- ssl_certificate /etc/live.xxxxx.com.cer; # 这里是服务端的证书路径
- ssl_certificate_key /etc/live.xxxxx.com.key; # 这里是秘钥路径
- ssl_session_timeout 5m;
- ssl_session_cache shared:SSL:10m;
- ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
- #ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
- ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
- ssl_prefer_server_ciphers on;
- ssl_verify_client off;
-
- location /wss {
- proxy_redirect off;
- proxy_pass http://127.0.0.1:8006/live;
- proxy_http_version 1.1;
- proxy_set_header Connection "upgrade";
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Host $host;
- proxy_set_header Remote_addr $remote_addr;
- proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
- proxy_read_timeout 100s;
- }
- }
复制代码
如下两行表示将http协议头升级为websocket协议: proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
如需要人工处理请留言回帖,留下****v
|