HTTP Upgrade机制可以用来将HTTP连接升级到WebSocket连接,需要用到Upgrade和Connection头。对于代理服务器,需要将来自客户端的Upgrade请求带到后端服务器上,同时保持长连接。
location /ws/ {
proxy_pass http://wsbackend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}