Scene
Business Services provide WebSocket services based on WebSocket to push report data, clients view reports on browsers, and pass requests to backend services via HTTP proxies.
Problem
When the client views the report data on the browser, the report function is abnormal, and the browser development tool views The WebSocket handshake on the service side and rejects it, returning to the bad request.
Analysis
4,001 is due to the client, so it should be a client request parameter or a path problem. By comparing the normal and abnormal websocket request messages, the abnormal messages are missing the HTTP header of Upgrade:websocket.
Upgrade belongs to the Hop-to-hop type of HTTP cache, unlike End-to-end, the latter in two client requests to the final backend service will not change, such as User-agent, while the former will be processed by proxy, the ability to use proxy Class Hop-to-hop header processing.
Solution Solutions
Nginx has the following solution, in the HTTP request head to fill up the Upgrade, provided that the agent to the back end of the message before the Upgrade.
map $http_upgrade $connection_upgrade {default upgrade;'' close;}proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade;
Reference
About Nginx+websocket 400 issues HTTPS://WWW.JIANSHU.COM/P/D5B136A27BDF
End-to-end and Hop-by-hop Headers https://tools.ietf.org/html/rfc2616#section-13.5.1
WebSocket + Proxy request failed, returned 400