Nginx, loadbalancer and

I am trying to use a nginx server that connect’s to a load balancer that connects to a service.
When running one service with Socket.IO it works fine, but as soon as I scale up the service to more than one it keeps connecting and disconnecting all the time.

To me it seems that the nginx server and the loadbalancer(in rancher) keeps changing the connection and therefore the socket service keeps connecting and disconnecting.

Anyone who had similar problems and actually got i working?

Here is the Nginx configuration

server {

  listen 8008; #http
  listen 8009; #https
  location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    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 Host $host;
    proxy_pass http://$http_host;


upstream {

is connected to the service on port 4600

socket Service

    var express = require('express');
    var app = express();
    app.port = process.env.PORT || 4600;
    var redis = require('');
    var socket_io = require('');
    var io = socket_io({adapter: redis({host: 'ADDRESSTOREDIS'})});
    io.on('connection', function (socket) {
            socket.on('disconnect', function() {
                console.log('socket disconnected');
        }); = io;

var server = app.listen(app.port,function () {;


Sounds like you need to configure some persistence (stickiness) somewhere. I don’t understand your configuration (mostly the Nginx part) enough to say where. Should be easy if it’s on the Rancher LB (HAProxy).

You’ll need to setup sticky sessions for the socket connections, within nginx.

Also, you should look into using something like ( This stores the sockets in Redis, so they’re available over the containers.