我正在使用 node,socket.io,nginx 进行基于 http 的简单应用程序的套接字连接。下面是我配置 websockets 的 nginx.conf,我的 socket io 存在于 socket/socket.io 中。奇怪的是,当我在 localhost 和 nginx 中使用端口 80 运行节点应用程序时,相同的配置可以完美运行。不知道为什么它在 linux 中哪个真实 IP 地址失败。
http {
upstream io_nodes {
ip_hash;
server IP:PORT; --> My application's IP and port
server IP:PORT;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 1000;
# HTTP Server
server {
listen 80;
server_name IP;
server_tokens off;
#proxy_ssl_verify off;
# prevents 502 bad gateway error
large_client_header_buffers 8 32k;
location /dummy/ {
root html;
index dummy/index.html;
}
location /socket/socket.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# prevents 502 bad gateway error
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_pass http://io_nodes/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
#location /socket.io/ {
# proxy_pass http://io_nodes/socket.io/;
#}
location /assistlog/socket.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# prevents 502 bad gateway error
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_pass http://10.73.52.187:4444/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}
}
这是chrome中的控制台错误
WebSocket connection to 'ws://IP/socket/socket.io/?pathName=%2FDEMOUI%2Findex.html&EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 502
请提出解决方案。