常规http访问的地址下
var ws = new WebSocket('ws://ip:port/ws')
如果使用https,则提示需要使用wss
解决办法:nginx在1.3版本以后支持websocket协议,websocket连接进入的时候,进行一个连接升级将http连接变成websocket的连接
server {
listen 443;
server_name 域名;
ssl on;
ssl_certificate /root/ssl/1.crt;
ssl_certificate_key /root/ssl/2.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location /ws {
proxy_pass http://ip:61614; # 代理mq
proxy_read_timeout 60s;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
}
前端页面使用:var ws = new WebSocket('wss://{域名}/ws'); 即可
OVER
网友评论