mohsenghafoory
mohsenghafoory

Reputation: 1

How to load balance in Nginx for same client IP and same browser as Session persistence

Using Nginx I got the balance servers as Session persistence, but there is a problem with my clients' IP suffering like this:

10.12.20.56
10.12.20.57
10.12.20.58

And the IP hash mechanism uses the first 3 octets as a hash. I also used this type:

hash $ remote_addr$http_user_agent;

But because my clients' browsers are alike, all clients are balancing on the same server.

This is my load-balancer.conf:


upstream backend {
  hash $remote_addr$http_user_agent;
  server 10.104.2.130:9443 ;
  server 10.104.2.140:9443;
}

server {
   listen 444 ssl default_server;
   ssl_certificate  /etc/nginx/ssl/cert.pem;
   ssl_certificate_key     /etc/nginx/ssl/key.pem;

  location / {
    proxy_pass https://backend;

    add_header Set-Cookie cip=$upstream_addr;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

  }
     location /nginx_status {
      stub_status on;
      access_log   off;
      allow  127.0.0.1;
     # deny all;
    }
}

Upvotes: 0

Views: 1301

Answers (1)

Andrea
Andrea

Reputation: 335

You should use a sticky cookie, available with sticky session. It's described here https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/#enabling-session-persistence

There are also ways to get Nginx with sticky session support, you could find some (old) instructions here https://www.acesti.it/en/nginx-sticky-session/

Or you could compile it with sticky module http://firzhanblogger.blogspot.com/2015/05/how-to-enable-nginx-sticky-module-in.html

Upvotes: 1

Related Questions