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:
- Implementation of HTTPS file server based on nginx in win2012
- failed: Error during WebSocket handshake: Unexpected response code: 400
- Nextcloud error: “access forbidden CSRF check failed”
- Nginx reports 502 error, log connect() failed (111: Connection refused) while connecting to upstream. A personal effective solution
- Nginx reverse proxy MySQL
- Nginx front end and back end separation + service cluster reverse proxy
- Nginx: recv() failed (104: connection reset by peer) troubleshooting
- NxL job cluster nginx routing forwarding and reverse proxy
- Nginx reverse proxy report 400 error solution!
- Ngixn exception net:: err_ HTTP2_ PROTOCOL_ ERROR 200
- Nginx configures different domain names to access different projects
- AH01075: Error dispatching request to
- node.js Server MySQL database connection timeout (error: connect etimeout)
- Nginx error log (error_ Log) configuration and information explanation
- nginx: [error] invalid PID number in /var/run/nginx.pid
- PHP & nbsp; built in server array
- “[warning] failed to retrieve plugin descriptor for caused by Maven security agent settings org.apache.maven . plugins:… “
- CentOS7 nginx Failed to read PID from file /run/nginx.pid: Invalid argument?
- Nginx start error: job for nginx.service failed because the control process exited with error code
- WSS connection server error