nginx配置好nodejs网站后socket.io失效如何解决
nginx配置好nodejs网站后socket.io失效如何解决呢?
主要原因就是socket也需要nginx转发
配置一下nginx转发socket就好了 ,详细配置如下:
server {
listen 80;
server_name a.itying.com;
location / {
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
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_buffering off;
#反向代理的地址
proxy_pass http://bakeaaa;
}
location /socket.io {
# 此处改为 socket.io 后端的 ip 和端口即可
proxy_pass http://127.0.0.1:8001;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}