lundi 30 mai 2016

Not getting response from socket.io configured with nginx

Created a node.js app and configured with nginx reverse proxy. The app was working fine till i configured it with nginx. But then in nginx application is able to connected to server but the connection is not reverted back client. But server is printing logs as it is connected. The code snippets are below. And I forget to mention i use socket-io with ember.js

nodejs/server.js

var express = require('express')
  , app = express()
  , http = require('http')
  , server = http.createServer(app)  
  , io = require('socket.io').listen(server);

server.listen(3000);


io.sockets.on('connect', function (socket) {
  console.log('------------->> Client Conected !!'); 


  socket.on('message', function (msg) {   
        onMessage(msg); 
   });

  function onMessage(msg) {      
      console.log('------------->> client msg - '+msg);       
   }

  socket.on('disconnect', function () {      
      console.log("------------->> Client disconected !!");
    });  
 });
console.log("server started @ 3000");

emberjs/socketio-client-controller.js

import Ember from 'ember';

export default Ember.Controller.extend({
  socketIOService: Ember.inject.service('socket-io'),
  init: function() {
    this._super.apply(this, arguments);
    var socket = this.get('socketIOService').socketFor('http://ift.tt/1TRflhS');
  socket.on('connect', function() {
      console.log("connected");
      socket.send('Hi Server');
      socket.on('message', this.onMessage, this);
    }, this);
  },

  onMessage: function(data) {
      console.log(data);
  }
});

This was working fine till nginx came to play. But still managed to configure as below

nginx.conf

http {

    upstream node {
        server ip-node-app:3000;
    }   

 server {
        listen       80;
        server_name  nginx.domain.com;

            location / {
            root   html;
            index  index.html index.htm;
        }

           location /socketio/server {
             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_buffers 8 32k;
             proxy_buffer_size 64k;
             proxy_pass http://node;
             proxy_redirect off;          
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection "upgrade";      
        }

        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 Host $http_host;
             proxy_set_header X-NginX-Proxy true;           
             proxy_buffers 8 32k;
             proxy_buffer_size 64k;
             proxy_pass http://node;
             proxy_redirect off;          
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection "upgrade";      
        }       
        }
    }

Once the client triggers request to server I'm seeing the console.log printing

"------------->> Client Conected !!"

but the client doesn't get any response from server at all(which means the client won't connect to server but server receives client connection's request). What Am I missing here?




Aucun commentaire:

Enregistrer un commentaire