Resolve Nginx Forwarding websocket 400 error description
Because the personal server has more than one project, configured with a level two domain name, need to two level domain name forwarding, in the forwarding work this quickly took the famous nginx
. Before that all the project run forward is no problem, but today in the deployment of a websocket
project with communication, but the unexpected error message, as follows:
1failed: Error during WebSocket handshake: Unexpected response code: 400
。 This error is not a problem in both the local test environment and the non-forwarding of the access nginx
, thus inferring that the problem should occur in nginx
forwarding this link.
So, with google
the help of the government, I saw the socket.io
official issues
discussion about this issue, Link: https://github.com/socketio/socket.io/issues/1942
Solution Solutions
Looked under the discussion area said the scheme, the problem appears in nginx
the configuration file, need to modify the nginx.conf
file. In the linux
terminal, vim /etc/nginx/nginx.conf
Locate the location
location, and the configuration file looks like this:
1server {
2 Listen 80;
3 server_name school.godotdotdot.com;
4 CharSet Utf-8;
5
6 Location/{
7 Proxy_pass http://127.0.0.1:3000;
8 Proxy_set_header Host $host;
9 Proxy_http_version 1.1;
Ten Proxy_set_header Upgrade $http _upgrade;
One by one 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;
600 proxy_send_timeout;
17}
18
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
22}
23
+ }
The most important of these is the following three lines
1proxy_http_version 1.1;
2proxy_set_header Upgrade $http_upgrade;
3proxy_set_header Connection "upgrade";
The first line is to tell the nginx
HTTP/1.1
protocol using the communication protocol, which is the one that websoket
must be used.
The second and third lines tell you to respond to the nginx
upgrade request when it wants to use WebSocket http
.
Based on the MIT Open Source Protocol
This article link: http://blog.godotdotdot.com/2017/12/04/address nginx forwarding websocket 400 error/
Resolve Nginx forwarding WebSocket 400 error