springboot+nginx websocket配置

websocket用于聊天类场景再好不过了。平时链接websocket需要指定ip和端口,但如果需要用nginx做转发,将请求发送到websocket服务,这时候就需要升级协议了。

1
2
3
4
5
6
7
location /member-api/websocket {
proxy_pass http://localhost:7005;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}

就是这么简单,指定

1
2
3
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

让协议升级就可以了