Nginx 从 1.3 版本开始支持 WebSocket ,并且可以为 WebSocket 应用程序做反向代理和负载均衡。
在https 配置 SSL证书的localhost下面,新增一个localhost,里面写websocket的配置,如果配置了反向代理,就不需要加端口号去访问
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443;
server_name xxxx.xxxx.net;
ssl on;
ssl_certificate /opt/ldkjdata/nginx/nginx-1.8.0/ca/xxxxx.xxxxx.net.pem ;
ssl_certificate_key /opt/ldkjdata/nginx/nginx-1.8.0/ca/xxxx.xxxxx.net.key ;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://xxxxxx.xxxxxx.net:8080;
}
location /websocket {
proxy_pass http://xxxxxx.xxxxx.net:8080/websocket;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Origin "";
proxy_redirect off;
}
}
image.png
网友评论