Nginx load balancing to keep sessions between IIS servers

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Nginx load balancing to keep sessions between IIS servers

zakirenish
I have set up a load balancer with NGINX for two IIS web servers that works
with sessions. Here is the NGINX configuration file I have created for the
load balancing:

#Log Format
log_format upstreamlog '$server_name to: $upstream_addr [$request] '
    'upstream_response_time $upstream_response_time '
    'msec $msec request_time $request_time';

#Upstream
upstream mybalancer {
    ip_hash;
    server server1.com:80;
    server server2.com:80;
}

#Server
server {
    listen 80;
    listen [::]:80;
    server_name server3.com;

    access_log /var/log/nginx/access.log upstreamlog;

    location / {
    proxy_pass http://mybalancer; 
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    }  
}

When I make a request to server3.com it gets redirected -for example- to
server1.com. Next I make the login, go to a specific page, let's say:
server1.com/welcome/maps. Everything is ok.

Now I turn off server1.com, and NGINX redirects me to server2.com, but
prompts me to the login page.

My question:

It's possible to configure NGINX to keep the same sessions when one server
goes down? This means that -in my example- NGINX could redirect me to
server2.com/welcome/maps with the same session.

PD:

I have read on other posts about setting this options:

proxy_cookie_path ~*^/.* /;
add_header "Set-Cookie" "lb=$upstream_http_X_Loadbalance_ID";

but does not works.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,287447,287447#msg-287447

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx load balancing to keep sessions between IIS servers

Francis Daly
On Tue, Mar 24, 2020 at 06:01:35PM -0400, robe007 wrote:

Hi there,

> I have set up a load balancer with NGINX for two IIS web servers that works
> with sessions.

What is "a session"?

The answer to that may make it clear how to achieve what you want.

> upstream mybalancer {
>     ip_hash;
>     server server1.com:80;
>     server server2.com:80;
> }
>
> #Server
> server {
>     server_name server3.com;
>     location / {
>     proxy_pass http://mybalancer; 
>     }  
> }
>
> When I make a request to server3.com it gets redirected -for example- to
> server1.com. Next I make the login, go to a specific page, let's say:
> server1.com/welcome/maps. Everything is ok.

That... probably should not happen.

In a reverse proxy situation, your client should not know (or care)
whether it is talking to server1 or to server2 -- it only interacts
with server3.

(It is possible that I am just misunderstanding the terminology here.)

> Now I turn off server1.com, and NGINX redirects me to server2.com, but
> prompts me to the login page.

I think "no".

server2 asks you to login; nginx does not. And that difference matters
here.

> It's possible to configure NGINX to keep the same sessions when one server
> goes down? This means that -in my example- NGINX could redirect me to
> server2.com/welcome/maps with the same session.

Back to the first question -- what is a session?

I suspect that in your system it is "state stored on the back-end server1
or server2"; and it is not anything that the browser knows about and
not anything that nginx knows about. The browser might have a "key"
to the session in a cookie.

If that is the case, then the fix would be for you to make sure that
server1 and server2 both have the same shared idea of a session, so that
the same cookie sent to either server will end up with the same response.

And there is nothing the nginx can do about arranging that.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx