【问题标题】:Unable to connect with socket.io using node.js and nginx无法使用 node.js 和 nginx 连接到 socket.io
【发布时间】:2013-05-19 01:31:13
【问题描述】:

我已经设置了一个 socket.io,node.js 在 nginx 后面运行。主域在 nginx 上运行,子域被代理到 node.js。所有 REST API 都工作正常,但是当涉及到套接字连接时,我确实在日志中看到了握手,但客户端显然无法获得响应。下面是来自 node.js 的日志,下面是我正在使用的配置。任何想法为什么它不起作用?

info  - handshake authorized 7WtMDKRY1tPb97CORlJ4
debug - setting request GET /socket.io/1/websocket/7WtMDKRY1tPb97CORlJ4
debug - set heartbeat interval for client 7WtMDKRY1tPb97CORlJ4
debug - client authorized for 
debug - websocket writing 1::
debug - clearing poll timeout
debug - jsonppolling writing io.j[0]("8::");
debug - set close timeout for client JoDWOVTvLg7SdYpNRlJ2
debug - jsonppolling closed due to exceeded duration
debug - setting request GET /socket.io/1/xhr-polling/7WtMDKRY1tPb97CORlJ4?t=1368925686996
debug - setting poll timeout
debug - discarding transport
debug - cleared heartbeat interval for client 7WtMDKRY1tPb97CORlJ4
debug - setting request GET /socket.io/1/jsonp-polling/7WtMDKRY1tPb97CORlJ4?t=1368925696997&i=0
debug - setting poll timeout
debug - discarding transport
debug - clearing poll timeout
debug - clearing poll timeout
debug - jsonppolling writing io.j[0]("8::");
debug - set close timeout for client 7WtMDKRY1tPb97CORlJ4
debug - jsonppolling closed due to exceeded duration
debug - setting request GET /socket.io/1/jsonp-polling/7WtMDKRY1tPb97CORlJ4?t=1368925717060&i=0
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client 7WtMDKRY1tPb97CORlJ4
debug - clearing poll timeout
debug - jsonppolling writing io.j[0]("8::");
debug - set close timeout for client 7WtMDKRY1tPb97CORlJ4
debug - jsonppolling closed due to exceeded duration
debug - setting request GET /socket.io/1/jsonp-polling/7WtMDKRY1tPb97CORlJ4?t=1368925737199&i=0
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client 7WtMDKRY1tPb97CORlJ4
debug - fired close timeout for client JoDWOVTvLg7SdYpNRlJ2
info  - transport end (close timeout)

Nginx 配置:

upstream app_yourdomain {
    server 127.0.0.1:3000;
}

# the nginx server instance
server {
    listen 80;
    server_name mydomain.com;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    location / {
      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;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";

      tcp_nodelay on;
      proxy_pass http://app_yourdomain/;
      proxy_redirect off;
    }
 }

Socket.io 正在 127.0.0.1:3000 中运行。

【问题讨论】:

    标签: node.js nginx socket.io


    【解决方案1】:

    您是否正在运行足够新的 nginx 版本? nginx 版本 1.3.13 中引入了 WebSocket 支持。 nginx -v 看看你有什么版本。另见https://chrislea.com/2013/02/23/proxying-websockets-with-nginx/

    【讨论】:

    • 我最近通过apt-get安装了它,最近升级了整个系统,但只运行了1.1.19。我现在会尝试升级它。
    • 我正在从客户端连接套接字,那么如何在代码中访问它,我的意思是如何访问 var socket = io.connect('/explorer/socket',{ 'reconnect' : true, '重新连接延迟': 500 });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2013-02-08
    • 1970-01-01
    • 2022-12-03
    • 2021-10-19
    • 1970-01-01
    • 2020-11-23
    相关资源
    最近更新 更多