问题描述:在项目中引用Socket.Io,在项目部署后报错,本地运行不报错
错误原因:需要在配置文件nginx.conf中配置相关信息
解决方案:
在nginx文件的location中添加
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
第一行告诉Nginx在与Node后端通信时使用HTTP / 1.1,这是WebSockets所必需的。接下来的两行告诉Nginx响应升级请求,当浏览器想要使用WebSocket时,该请求由HTTP启动。这三行都是必须添加的。
例如:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://localhost:3100;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}