Node.js Socket.io WebSocket Nginx配置?

时间:2013-08-03 15:30:53

标签: node.js nginx proxy websocket socket.io

我正在使用Citrix负载均衡器。

在这之后,有4个虚拟Nginx服务器。 ip就像172.16.10.40,172.16.10.41,172.16.10.42,172.16.10.43

和1个测试服务器 172.16.10.50。

安装在测试服务器上的Nodejs位于172.16.10.50。

我已经为像sub.example.com这样的nodejs创建了一个子域。

我的nodejs应用正在使用8070端口。

我想使用Websocket而不是xhr-pooling。 使用下面的代码和配置,在Chrome控制台中我看到了

Status Code:101 Switching Protocols

但帧上没有任何内容。没有推动。

如果我将socketURL更改为

var socketURL   = http://172.16.10.50:8070

Websocket在测试平台上没有任何问题(172.16.10.50)。

但是,在真实平台中,我必须使用'http://sub.example.com:8070';

如果我设置了socket.io:'transports',['xhr-polling']; xhr-polling正在发挥作用。但我想使用WebSocket。

 nginx version: nginx/1.4.1 
 node v0.8.8 
 socket.io v0.9.16

我该怎么办?

谢谢。

app.js

var app =
  server = require('http').createServer(app)
  , io = require('socket.io').listen(server,{ log: false })
  , url = require('url')
  , http= require('http')
  ,redis   = require("redis");

//io.set('transports', ['xhr-polling']); 

var livefeed        = redis.createClient();
server.listen(8070);

livefeed.on("message", function(channel, message){
    console.log("%s, the message : %s", channel, message);  
    io.sockets.in(channel).emit(channel,message);
});

io.sockets.on('connection', function (socket) {
    console.log("["+socket.id+"] connected"); 
    socket.on('subscribe', function (data) {
        //console.log("joining : %s",data.channel);
        socket.join(data.channel);
      });
    socket.on('unsubscribe', function(room) {  
        //console.log('leaving room', room);
        socket.leave(room); 
    });
    socket.on('disconnect', function (socket) {
        connected_socket--;
        console.log("Client disconnected");     
        SocketCount();
    });

});

example.js

var socketURL   =   'http://sub.example.com:8070';
var socket      = false;

var BKSocket = {
   connectSocket : function(){
        if(socket === false){
            try{
                socket = io.connect(socketURL,{'connect timeout': 1000});
            }catch(e){
                socket = false;
            }
        }
    },
    livefeeds:function(){
        this.connectSocket();

        if(socket !== false){
            socket.on('connect', function(data){                
                socket.emit('subscribe', {channel:'livefeed'});
            });             
            socket.on('livefeed', function (data) {
                console.log(data);
            });
        }       
    }
}

nginx配置

upstream backend {
    server 127.0.0.1:8070;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    server_name sub.example.com;

    #server_name _;
    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_pass http://backend/;
        proxy_redirect off;

        #proxy_http_version 1.1;
        #proxy_set_header Upgrade $http_upgrade;
        #proxy_set_header Connection $connection_upgrade;

        access_log off;
        error_log   /var/log/nginx/sub.example.com.error.log;

    }
}

error.log中

2013/11/25 09:40:08 [error] 29812#0: *25900 upstream prematurely closed connection while reading response header from upstream, client: 172.16.10.10, server: v2.bitenekadar.com, request: "GET /socket.io/1/websocket/NPXo9XDAKbAapgpyLqCd HTTP/1.1", upstream: "http://172.16.10.50:8070/socket.io/1/websocket/NPXo9XDAKbAapgpyLqCd", host: "v2.bitenekadar.com"
2013/11/25 09:41:36 [error] 29812#0: *26046 upstream prematurely closed connection while reading response header from upstream, client: 172.16.10.10, server: v2.bitenekadar.com, request: "GET /socket.io/1/websocket/RxBjIryz50FjUs1RLqCe HTTP/1.1", upstream: "http://172.16.10.50:8070/socket.io/1/websocket/RxBjIryz50FjUs1RLqCe", host: "v2.bitenekadar.com"
2013/11/25 09:42:10 [error] 29812#0: *26046 upstream prematurely closed connection while reading response header from upstream, client: 172.16.10.10, server: v2.bitenekadar.com, request: "GET /socket.io/1/websocket/ZMuHPZgFcOGmULNdNStr HTTP/1.1", upstream: "http://172.16.10.50:8070/socket.io/1/websocket/ZMuHPZgFcOGmULNdNStr", host: "v2.bitenekadar.com"
2013/11/25 09:43:17 [error] 29812#0: *26063 upstream prematurely closed connection while reading response header from upstream, client: 172.16.10.10, server: v2.bitenekadar.com, request: "GET /socket.io/1/websocket/J3qPn40WioPviZZMNSts HTTP/1.1", upstream: "http://172.16.10.50:8070/socket.io/1/websocket/J3qPn40WioPviZZMNSts", host: "v2.bitenekadar.com"
2013/11/25 09:45:23 [error] 29812#0: *26181 upstream prematurely closed connection while reading response header from upstream, client: 172.16.10.10, server: v2.bitenekadar.com, request: "GET /socket.io/1/websocket/CtOaZ65Dq7dAX6jEOAap HTTP/1.1", upstream: "http://172.16.10.50:8070/socket.io/1/websocket/CtOaZ65Dq7dAX6jEOAap", host: "v2.bitenekadar.com"

2 个答案:

答案 0 :(得分:7)

您使用的是哪种版本的nginx?尽管根据文档做了一切,我们遇到了类似的问题。事实证明,我们的nginx版本(1.2.x)太旧了,并且无法正常工作,尽管接受配置没有问题。

更新到1.4.4并且工作正常!

顺便说一句,这是我们目前正在使用的配置:

upstream devserver_pc {
    server localhost:9003;
}

server {
    listen 80;

    root /vagrant/pc/static;
    index index.html index.htm;

    access_log /var/log/nginx/pc.access.log;
    error_log  /var/log/nginx/pc.error.log;

    server_name pc.bvb-infotainment.vm;

    client_max_body_size 20M;
    location /static {
        alias /vagrant/pc/static;
    }

    location /socket.io/websocket {
        proxy_pass http://devserver_pc;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location / {
        proxy_http_version 1.1;
        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   Host             $http_host;
        proxy_set_header   Upgrade          $http_upgrade;
        proxy_redirect     off;
        proxy_pass         http://devserver_pc;
    }
}

答案 1 :(得分:1)

尝试使用proxy_set_header:

server {
  listen   80;
  server_name  app.local;
  root  /home/app/public;
  passenger_enabled on;
  rails_env development;

  location /any_location {
     proxy_pass http://localhost:3001/realtime_page;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
  }
}

http://blog.joshsoftware.com/2013/05/28/websocket-over-nginx/

什么是服务器响应(标题)?请求标头如何?