SocketJs报错failed: Error during WebSocket handshake: Unexpected response code: 400
问题出现在nginx的配置文件,需要修改nginx.conf文件。配置文件如下所示:
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;
}
}
其中最重要的是下面这三行
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
其中第一行是告诉nginx使用HTTP/1.1通信协议,这是websoket必须要使用的协议。
第二行和第三行告诉nginx,当它想要使用WebSocket时,响应http升级请求。
如果有有配置ssl,在ssl的配置里也要写上