Nginx反向代理+Websocket 导致找不到请求路径 No handler found for GET

1、网络环境

Nginx作为前端服务器,并且为解决跨域问题对后台服务做了反向代理。

2、所有http请求都好着,Websocket后台提示“No handler found for GET”,找不到服务路径。

 3、通过全网查询,最后解决办法,在反向代理location中增加

location /lawAccept/ {
        proxy_set_header Connection 'upgrade';
        proxy_set_header Upgrade $http_upgrade; 
        proxy_pass http://localhost:8085/lawAccept/; 
}            

4、参数解释

访问websocket时的链接通常为

socket = new WebSocket("ws:192.168.2.24:8080/oneAccept/evalwebsocket/")

此时协议信息如下

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat

通过与http请求头对比,发现多了

Upgrade: websocket
Connection: Upgrade

此种用法使用的协议为ws协议,必须借助http做第一次握手,现在前端和后端增加了nginx作为代理,则需要在nginx中将使用的ws协议让后端知晓

proxy_set_header Connection 'upgrade';  //告诉接收端,要进行协议升级

proxy_set_header Upgrade $http_upgrade; //升级为websocket

posted @ 2022-02-25 15:50  蓝色土耳其  阅读(3283)  评论(0编辑  收藏  举报