Amit Mourya
Amit Mourya

Reputation: 538

NGINX : How to have sticky session with least connected

I am using ip_hash(Sticky Session) and I have a scenario for our web portal where portal is accessible over web, and we have support module which is also hosted on the same portal, and upstream is having load balanced with 22 nodes

  1. No problem for customers using portal for Sticky Session.
  2. Users loging in from support center are having performance issue because sticky session is making upstream to point same node for all user's which are having same external public IP.

Need help regarding point 2. I want to distribute load for them as well, as there are concurrent 1000 user working form support center

My Config look likes

upstream appserver{
    ip_hash;
    server 192.168.0.x:3811;
    server 192.168.0.x:3812;
    server 192.168.0.x:3813;
    server 192.168.0.x:3814;
    server 192.168.0.x:3815;
    server 192.168.0.y:3811;
    server 192.168.0.y:3812;
    server 192.168.0.y:3813;
    server 192.168.0.y:3814;
    server 192.168.0.y:3815;
    ...
    ...
}    

server {
    location "/support" {
        allow ...;
        deny  all;
        alias ...;
        index index.html;
    }

    location / {
        proxy_pass         http://appserver;
        proxy_cookie_path / "/; secure;";
        proxy_set_header   X-IBanking   "127.0.0.1";
        proxy_set_header   X-Real-IP    $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header   X-NginX-Proxy    true;
        proxy_set_header   X-Request-Id $txid;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header Strict-Transport-Security max-age=15552000;
        proxy_http_version 1.1;
        proxy_redirect off;
    }

    gzip on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    gzip_buffers 16 8k;
    gzip_vary on;
}

Upvotes: 2

Views: 2096

Answers (1)

Holger Will
Holger Will

Reputation: 7526

I don't think its possible with nginx alone. I'm using koa-generic-session with redis. in this way all your nodes connect to redis to read/write the session values, and the seesion follows the user to any upstream server, as long as that server is connected to your redis server.

this example is taken from https://github.com/koajs/generic-session

var session = require('koa-generic-session');
var redisStore = require('koa-redis');
var koa = require('koa');

var app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore()
}));

app.use(function *() {
  switch (this.path) {
  case '/get':
    get.call(this);
    break;
  case '/remove':
    remove.call(this);
    break;
  case '/regenerate':
    yield regenerate.call(this);
    break;
  }
});

function get() {
  var session = this.session;
  session.count = session.count || 0;
  session.count++;
  this.body = session.count;
}

function remove() {
  this.session = null;
  this.body = 0;
}

function *regenerate() {
  get.call(this);
  yield this.regenerateSession();
  get.call(this);
}

app.listen(8080);

this is what i'm currently using for sessions in a multible upstream environment.

Upvotes: 2

Related Questions