Original address: https://www.cnblogs.com/duanweishi/p/9286461.html
explain
Because there are multiple items on the personal server and the secondary domain name is configured, the secondary domain name needs to be forwarded. In the forwarding work, the famous nginx
is adopted. Before that, there was no problem in forwarding all projects. However, when deploying a project with websocket
communication, an unexpected error was reported. The error message is as follows:
1failed: Error during WebSocket handshake: Unexpected response code: 400
。 There is no problem with this error in the local test environment and accessing non nginx
forwarding. Therefore, it is inferred that the problem should occur in nginx
forwarding.
So, with the help of Google
, I saw socket. IO
The official issues
has a discussion on this issue, link: https://github.com/socketio/socket.io/issues/1942
Solution
After reading the scheme in the discussion area, the problem appears in the configuration file of nginx
, and the nginx.conf
file needs to be modified. In the linux
terminal, type VIM/etc/nginx/nginx.conf
, find the location
, and the configuration file is as follows:
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;
10 proxy_set_header Upgrade $http_upgrade;
11 proxy_set_header Connection "upgrade";
12 proxy_set_header X-Real-IP $remote_addr;
13 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
14 proxy_connect_timeout 60;
15 proxy_read_timeout 600;
16 proxy_send_timeout 600;
17 }
18
19 error_page 500 502 503 504 /50x.html;
20 location = /50x.html {
21 root html;
22 }
23
24 }
The most important of these are the following three lines
1proxy_http_version 1.1;
2proxy_set_header Upgrade $http_upgrade;
3proxy_set_header Connection "upgrade";
The first line tells nginx
to use the http/1.1
communication protocol, which must be used by websoket
.
The second and third lines tell nginx
to respond to the HTTP
upgrade request when it wants to use websocket.