nginx dynamic routing per browser user session

2.2k views Asked by At

Multiple web applications behind nginx. I have different web applications running behind nginx.

  1. Main web-site application --> This is website which provides redirect link to different applications.
  2. Different Applications --> App-1/App-2/App-3
  3. Nginx --> which routes to correct application.

This is my diagram below i am using for my setup to handle multiple applications.

enter image description here

How App-1/App-2/App-3 applications are accessible

A. Browser sends request for access to main website & main website provides them 304 redirect with cookies values ( Device_type=xx & IP=45) to hold off.

* Device_Type=App-1 or App-2 --> which device to connect. * IP = 45 , last octet of IP address of system.

So that just by seeing device type & IP last octet value one can redirect to correct application.

B. Browser sends request to nginx with cookies & nginx just by seeing device type & IP last octet value one will redirect to correct application. And all further request goes to correct application depending upon cookie values & rules of nginx.

This works fine absolutely fine if only applications( App-1/App-2/App-3) is accessible from one browser per APP only.

Issue :- No two application can be accessible from same browser. i.e. if both applications runs from same browser even if different tabs they would share cookies.

Now if they share cookies then nginx will be re-directing to incorrect APP most of times.

I know this is not best approach but it was done as is before.

Achieve :- I want to have dynamic routing on nginx depending upon per user browser session.So that each time user request for anything i would route it correct APP

I have been investigating a lot in nginx but nothings from nginx which holds browser session so that all subsequent request goes to same APP Server. Looking into ceryx-dynamic-nginx also.

  • If you think Cookie is not good idea & then i am open to ideas to change it.
1

There are 1 answers

4
Abdullah Shahin On

all you need to do in order to direct a request based on cookie to particulare server is to make a map as follows:

map $cookie_IP $app_upstream {
   default main_app;
   45      app1;
   46      app2;
   66      app3;
}

and you need to create app stream for each

upstream main_app {
   server 192.168.12.10;
}
upstream app1 {
    server 10.122.123.45;
}
upstream app2 {
    server 10.122.123.46;
}
upstream app3 {
    server 10.122.123.66;
}

and under your location you do

location / {
    proxy_pass http://$app_upstream$uri;
}

if you have customized url for each upstream, then, what you need to do is to create server for each upstream and make it to direct based on hostname value.