Tag Archives: failed: Error during WebSocket handshake: Unexpected response code: 400

Websocket failed: Error during WebSocket handshake: Unexpected response code: 400 [Solved]

After websocket is introduced into the project, it will work normally locally and report 400 as soon as it is installed in the production environment
reason: when nginx is configured in the production environment, the nginx agent fails to pass, and the relevant information needs to be configured in the configuration file nginx.conf

Solution:

Add in the location of nginx file

proxy_ http_ version 1.1;
proxy_ set_ header Upgrade $http_ upgrade;
proxy_ set_ header Connection “upgrade”;

The first line tells nginx to use HTTP/1.1 when communicating with the node backend, which is required by WebSockets. The next two lines tell nginx to respond to the upgrade request, which is initiated by HTTP when the browser wants to use websocket. All three lines must be added