Deployment project websocket failed: error during websocket Handshake: unexpected response code: 400

Fixed nginx forwarding WebSocket 400 error
Reprint link: https://www.cnblogs.com/duanweishi/p/9286461.html

because the personal server has a number of projects, configured with a secondary domain name, the need to forward the secondary domain name, in the forwarding work this quickly took the famous nginx. Before this, all the projects had no problem running forward, but today, when deploying a project with WebSocket communication, an unexpected error was reported, with the following error message:
Failed: Error during WebSocket handshake: Unexpected response code: 400
. This error worked in both the local test environment and access to non-Nginx forwarding, which inferred that the problem should have occurred in nginx forwarding.
Then, with the help of Google, saw the socket. IO official issues have a discussion about this problem, link: https://github.com/socketio/socket.io/issues/1942
Solution
read the solution in the discussion section, the problem appears in the configuration file of nginx, and the nginx.conf file needs to be modified. Type vim /etc/nginx/nginx.conf into the Linux terminal and find the location. The configuration file is as follows:

server {
        listen       80;
        server_name  school.godotdotdot.com;
        charset utf-8;

        location/{
            proxy_pass http://127.0.0.1:3000;
            proxy_set_header Host $host;
            proxy_http_version 1.1; 
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 60;
           proxy_read_timeout 600;
           proxy_send_timeout 600;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

The most important of these are the following three lines

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

The first line tells Nginx to use the HTTP/1.1 communication protocol, which websoket must use.
The second and third lines tell Nginx to respond to an HTTP upgrade request when it wants to use WebSocket.
Supplement:

	server {
	        listen       80;
	        server_name  school.godotdotdot.com;
	        charset utf-8;
	        
			proxy_http_version 1.1; 
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
	

	        location/{
	            proxy_pass http://127.0.0.1:3000;
	            proxy_set_header Host $host;
	            proxy_set_header X-Real-IP $remote_addr;
	            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	            proxy_connect_timeout 60;
	           proxy_read_timeout 600;
	           proxy_send_timeout 600;
	        }

	        error_page   500 502 503 504  /50x.html;
	        location = /50x.html {
	            root   html;
	        }
	 }

Read More: