# 实际websocket服务器地址
upstream wss_svr {
server 192.168.213.182:3653 weight=1;
}
# 443 ssl端口配置,实际websockets(wss)地址
server {
listen 443;
server_name 192.168.213.182;
ssl on;
# 自己的证书,放在与nginx.conf同一文件夹下。(若放不同文件夹注意路径问题)
ssl_certificate dhxtest.crt;
ssl_certificate_key dhxtest_nopass.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:10m;
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 / {
proxy_redirect off;
proxy_pass http://wss_svr; # 转发
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # 最后两行的set_header表示将http协议头升级为websocket协议
proxy_set_header Connection upgrade;
}
}
最后结果:访问 wss://127.0.0.1:443,nginx转发请求到 ws://192.168.213.182:3653,然后和3653端口的websocket服务进行通信