Nginx 代理 WebSocket 的要点是设置Upgrade和Connection响应头。 配置 Nginx 根据Upgrade(即$http_upgrade)来设置Connection:
如果请求头中有Upgrade,就直接设置到响应头中,并把Connection设置为upgrade。如 WebSocket 请求头会带上Upgrade: websocket,则响应头有
Upgrade: websocket
Connection: upgrade
否则把Connection设置为close。如普通HTTP请求。 最终 Nginx 配置如下:
nginx.conf 中 http 配置
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
conf.d下的.conf具体配置文件
server {
listen 8000;
location / {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有