1

socket.io v. 1.0
nginx v 1.4.6
nodejs v 0.10.32
ubuntu v 14.04
droplet IP (for example): 123.45.678.90
nodejs port listen on: 3001
domain: mydomain.com

Everythigs works correctly both on localhost in WebStorm and on my droplet address like: 123.45.678.90:3001. Problems begin when i try to bind my domain and nginx by the follow config:

server {
    listen 80;

    server_name mydomain.com;

    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

When i try to connect the mydomain.com from browser, sometimes it works correct, but sometimes i see that message:

{ host: 'mydomain.com',
  connection: 'close',
  'accept-encoding': 'gzip, deflate',
  'user-agent': 'WebIndex',
  accept: 'text/html' }

/var/www/bm/socket/index.js:29
    var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split
                                            ^
TypeError: Cannot call method 'split' of undefined

Here is my socket/index.js, part includes io.use:

io.use(function(socket, next){
                async.waterfall([
                        function(callback){
                                console.log(socket.request.headers);
                                var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split('.')[0];
                                console.log(sid);
                                sessionStore.load(sid, function(err, session){
                                        if (arguments.length == 0) {
                                                return callback(null, null);
                                        } else {
                                                return callback(null, session)
                                        }
                                });
                        },
                        function(session, callback){
                                socket.handshake.session = session;
                                callback(null);
                        }
                ], function(){
                        if (socket.handshake.session) {
                                return next();
                        } else {
                                console.log('error, be careful');
                                return next(new Error('not authorized'));
                        }
                });
        });

What happend? I tried to catch that 'WebIndex' and change part of io.set:

console.log(socket.request.headers);
if (socket.request.headers['user-agent'] == 'WebIndex') {
  return callback(null, null);
}
var sid = socket.request.headers.cookie.split('express.sid=s%3A')[1].split('.')[0];
console.log(sid);

As a result, i got this into my browser on address mydomain.com:

WebSocket connection to 'ws://mydomain.com/socket.io/?EIO=3&transport=websocket&sid=QzYL8Ou8QN556_WEAAAC' failed: Error during WebSocket handshake: Unexpected response code: 400

A few seconds socket.io worked correctly but then i got this 400 error message. Any ideas?

haveacigar
  • 51
  • 5

1 Answers1

1

I solved that problem with full reinstalling nginx:

// Delete nginx and linked packages: 
sudo apt-get remove nginx*

// Delete nginx working directories and logs:
sudo rm -rf /etc/nginx/ /usr/sbin/nginx /usr/share/man/man1/nginx.1.gz

// Delete all remaining nginx files from apt base:
sudo apt-get --purge autoremove nginx && sudo dpkg --purge nginx

// Reinstall nginx with pure config files
sudo apt-get -o DPkg::options::=--force-confmiss --reinstall install nginx

And it works!

Termininja
  • 6,620
  • 12
  • 48
  • 49
haveacigar
  • 51
  • 5
  • I have the same issue with nginx for Windows. As there is actually no installation, other by unzip -> run nginx, I am wondering if I should do something else? More details here: http://stackoverflow.com/questions/29043879/socket-io-with-nginx – Aleks Mar 17 '15 at 14:32