Nginx作为sockio.io websocket的代理服务器无法在远程工作

时间:2014-12-04 09:35:56

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

在我开发的macchine上,我使用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从同一个macchine访问时,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协议的一个问题...

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

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});

IT工作,从任何远程连接!我无法解释为什么但它有效。

希望这对任何其他用户都有帮助