server {
listen 443 ssl;
server_name localhost;
ssl on;
root html;
index index.html index.htm;
ssl_certificate ******.pem;
ssl_certificate_key *******.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location /{
proxy_pass http://localhost:3001;
}
# 关键点
location /ws {
proxy_pass http://localhost:8181; proxy_read_timeout 60s;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
}
}
原理解析:
WebSocket协议的握手和HTTP是兼容的,它使用HTTP的Upgrade协议头将连接从HTTP连接升级到WebSocket连接。这个特性使得WebSocket应用程序可以很容易地应用到现有的基础设施
就可以使用https//+域名访问和使用wss://+域名+/ws访问了
原文地址:nginx 配置https和wss
参考链接:分享下nginx配置HTTPS WSS