Nginx配置
http {
...
#other config
...
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 127.0.0.1:8001;
}
server {
listen 80;
server_name www.test.com;
location /wss {
proxy_pass http://websocket ;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
server {
#https
listen 443;
server_name www.test.com;
ssl on;
ssl_certificate 1_test.com_bundle.crt;
ssl_certificate_key 2_test.com.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 / {
root /home/wsocket/client;
index index.html index.htm;
}
location /wss {
proxy_pass http://websocket ;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
客户端使用
let s1 = new WebSocket('ws://test.com/wss')
// 或者
let s2 = new WebSocket('wss://test.com/wss')
如果服务器使用https协议那么websocket只能使用wss协议