title: Node.js Socket.io WebSocket Nginx Configuration?
body: <p>I am using Citrix load balancer.</p>
  
  <p>Behind that , there is 4 virtual Nginx server.
  ip's like 172.16.10.40, 172.16.10.41, 172.16.10.42, 172.16.10.43</p>
  
  <p>And 1 test server
  172.16.10.50.</p>
  
  <p>Nodejs installed on test server located on 172.16.10.50.</p>
  
  <p>I've created a subdomain for nodejs like sub.example.com.</p>
  
  <p>My nodejs app is working on 8070 port.</p>
  
  <p>I want to use Websocket not xhr-pooling.
  With my codes and configs below, In Chrome Console i saw that </p>
  
  <pre><code>Status Code:101 Switching Protocols
  </code></pre>
  
  <p>But Nothing appers on Frames. No push.</p>
  
  <p>If i change  socketURL to</p>
  
  <pre><code>var socketURL   = http://172.16.10.50:8070
  </code></pre>
  
  <p>Websocket working without anyproblem in test platform (172.16.10.50). </p>
  
  <p>But, In real Platform I have to use '<a href="http://sub.example.com:8070" rel="nofollow">http://sub.example.com:8070</a>';</p>
  
  <p>If i set socket.io  :  'transports', ['xhr-polling'] ; 
  xhr-polling is working. But I want to use WebSocket.</p>
  
  <pre><code> nginx version: nginx/1.4.1 
   node v0.8.8 
   socket.io v0.9.16
  </code></pre>
  
  <p>What i should do? </p>
  
  <p>Thank you.</p>
  
  <p>app.js</p>
  
  <pre><code>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();
      });
  
  });
  </code></pre>
  
  <p>example.js</p>
  
  <pre><code>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);
              });
          }       
      }
  }
  </code></pre>
  
  <p>nginx Config</p>
  
  <pre><code>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;
  
      }
  }
  </code></pre>
  
  <p>error.log</p>
  
  <pre><code>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"
  </code></pre>