使用spring websocket做在线聊天,本地使用ws连接没有问题。但是部署到Linux上在连接访问就会报错400,错误信息Error during WebSocket handshake: Unexpected response code: 400。Linux使用nginx做代理,并且开启了ssl。
以下为nginx配置
Nginx的原因。Nginx必须在websocket对应的location上加上代理头,使用Http的Upgrade和Connection协议头升级Http 1.1协议,以支持websocket:
location /your_websocket_uri {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
https://www.nginx.com/blog/websocket-nginx/
<div class='ref'><h4>引用来自“CoserSeu”的评论</h4><p>Nginx的原因。Nginx必须在websocket对应的location上加上代理头,使用Http的Upgrade和Connection协议头升级Http 1.1协议,以支持websocket:</p>
location /your_websocket_uri {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
https://www.nginx.com/blog/websocket-nginx/
集结各类场景实战经验,助你开发运维畅行无忧