【问题标题】:Nginx as proxy server for sockio.io websocket not working on remoteNginx 作为 sockio.io websocket 的代理服务器无法远程工作
【发布时间】:2014-12-04 09:35:55
【问题描述】:

在我的开发机器上,我使用 nginx 作为 websocket 的代理服务器,配置如下:

server {
  listen       80;

  location /socket.io {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;

    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:3001;
    proxy_redirect off;
  }

  location / {
    proxy_pass http://trunk.alban:81;
    client_max_body_size 500M;

  }
}

当我通过 firefox 从同一台机器访问时,websockets 正确连接并开始交换消息:

[root@localhost nodejs]# node websocket.js 

2014-12-04 10:27:16 ==> Listening on port: 3001
2014-12-04 10:27:16 ==> Auth token: alyt4qxs9k9k95a7q1if6r3a8b7yzaor
2014-12-04 10:27:29 ==> Client authorized: alyt4qxs9k9k95a7q1if6r3a8b7yzaor == alyt4qxs9k9k95a7q1if6r3a8b7yzaor
2014-12-04 10:27:29 ==> IP: [127.0.0.1], URL:[http://trunk.alban], Agent: [Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0] 
2014-12-04 10:27:35 ==> Broadcasting clients: amend-view-status_13 {}

但是当我从另一台机器访问时,它连接但它不发送/接收任何消息,只是循环切换协议,连接和断开:

2014-12-04 10:32:23 ==> Client authorized: alyt4qxs9k9k95a7q1if6r3a8b7yzaor == alyt4qxs9k9k95a7q1if6r3a8b7yzaor
2014-12-04 10:32:23 ==> IP: [127.0.0.1], URL:[http://192.168.57.130], Agent: [Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0] 
2014-12-04 10:32:43 ==> Client Disconnected
2014-12-04 10:32:44 ==> Client authorized: alyt4qxs9k9k95a7q1if6r3a8b7yzaor == alyt4qxs9k9k95a7q1if6r3a8b7yzaor
2014-12-04 10:32:44 ==> IP: [127.0.0.1], URL:[http://192.168.57.130], Agent: [Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0] 
2014-12-04 10:33:04 ==> Client Disconnected
2014-12-04 10:33:06 ==> Client authorized: alyt4qxs9k9k95a7q1if6r3a8b7yzaor == alyt4qxs9k9k95a7q1if6r3a8b7yzaor
2014-12-04 10:33:06 ==> IP: [127.0.0.1], URL:[http://192.168.57.130], Agent: [Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0] 

我安装了 nginx 1.6.2 和 socket.io 1.2.1。

我已禁用所有可能的防火墙。

注意:如果我删除 nginx 作为代理服务器,只需直接指向 socket.io 服务器,它在任何机器上都可以正常工作。

NOTE2:强制使用轮询它没有问题。好像是nginx/ws协议的问题……

有什么想法吗?

【问题讨论】:

    标签: node.js nginx socket.io-1.0


    【解决方案1】:

    好的,我发现了问题:在客户端我设置了传输选项:

    this.socket = io.connect(this.adress, {query: 'token='+this.token /* , resource: 'ws/socket.io' */, transports:['websocket', 'polling']});
    

    通过删除它:

    this.socket = io.connect(this.adress, {query: 'token='+this.token});
    

    它可以通过任何远程连接运行!!!我无法解释原因,但它确实有效。

    希望这对其他用户有所帮助

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2013-03-22
      • 1970-01-01
      • 2011-01-26
      • 2021-01-27
      相关资源
      最近更新 更多