问题是,当我使用nginx时套接字没有建立。任何人都可以帮助我按照步骤将Socket oi与nginx集成。 我试过这个
location /field {
# the following is required for WebSockets
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;
# supposedly prevents 502 bad gateway error;
# ultimately not necessary in my case
proxy_buffers 8 32k;
proxy_buffer_size 64k;
# the following is required
proxy_pass
proxy_redirect off;
# the following is required as well for WebSockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
tcp_nodelay on; # not necessary
}
答案 0 :(得分:2)
首先检查你的nginx版本。根据v1.3.13之后支持的页面websockets;
http://nginx.com/blog/nginx-nodejs-websockets-socketio/
然后将您的nginx conf与nginx博客中所述的以下配置进行比较;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 192.168.100.10:8010;
}
server {
listen 8020;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
http://nginx.com/blog/websocket-nginx/
另请检查您为socket.io服务器选择的端口的防火墙配置。 (我已经读过一些ISP阻止80& 443以外的端口的websocket连接,请同时检查服务器是否使用tcpdump等接收数据包。)
如果到目前为止每次都没问题,请检查你的nginx错误日志(/var/log/nginx/error.log),看看是否有任何与socket.io相关的错误信息。您可以将其粘贴到此处进行进一步分析。
然后,如果nginx日志中没有套接字错误,请启动具有DEBUG模式的节点应用程序,如下所示;
DEBUG=* node yourfile.js
并检查是否有任何套接字连接消息打印到控制台。您也可以将其粘贴以供进一步分析。