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:
- WebSocket failed: Error during WebSocket handshake: Unexpected response code: 400
- failed: Error during WebSocket handshake: Unexpected response code: 400
- failed: Error during WebSocket handshake: Unexpected response code: 200
- WebSocket handshake Unexpected response code 403
- Websocket code 200 error
- Nginx manager jupyter notebook v1.0.0 http websocket
- Jupyter failed to run websocket error
- Ngnix+gunicorn+Flag project deployment in Ubuntu
- When websocket transmits JSON text, the parse method reports an error
- [Transfer] mdt wds deployment windows Litetouch deployment failed, Return Code = -2147467259 0x80004005 solution…
- mdt wds deployment windows Litetouch deployment failed, Return Code = -2147467259 0x80004005
- Solution of error report in springboot Maven package websocket
- Response code: Sampler error: unexpected frame type (binary).
- JMeter running error response code: non HTTP response code: java.lang.illegalargumentexception find and solve
- Solve the problems of (deployment descriptor: project name) and (jax-ws web services) under the new eclipse project
- The deployment of etcd storage and flannel network configuration for kubernetes / k8s multi node deployment
- Deployment project error: Oracle 11g ora-12505, TNS:listener does not currently know of SID given in connect descriptor
- Solution: DevTools failed to load SourceMap:… net::ERR_HTTP_RESPONSE_CODE_FAILURE problem
- JS Ajax request is successful, the response displays “failed to load response data”
- SQL Server SSPI handshake failed Error