websocket+SSL
,就是在websocket协议上加入SSL层,类似https(http+SSL)
。https和http不能混合使用,wss和ws也是对应的,https下无法使用http和ws协议。HTTPS对应WSS,HTTP对应WS。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream wsbackend{ server ip1:port1; server ip2:port2; keepalive 1000; } server { listen 80; listen 443 ssl; server_name 域名; ssl on; ssl_certificate 证书.crt; ssl_certificate_key 证书.key; ssl_session_timeout 5m; ssl_session_cache shared:SSL:50m; ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_prefer_server_ciphers on; location /wss/ { proxy_pass http://127.0.0.1:8282/; #通过端口指向websocker项目 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; } } |
此时,访问 ws
或wss://localhost/wss/
就会被转发到 ip1:port1
和 ip2:port2
上。同时兼容WS和WSS协议。