配置map --http 模块
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
最重要的就是在反向代理的配置中增加了如下两行,其它的部分和普通的HTTP反向代理没有任何差别。
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
测试方法
npm install -g wscat
wscat -c ws://echo.websocket.org
wscat -c localhost:$port/xxx/xxx
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 192.168.100.10:8010;
}
server {
listen 8020;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}
}
}