Forum Moderators: phranque

Message Too Old, No Replies

set cookie for sticky session dynamically

         

jusaf

5:14 pm on Feb 8, 2011 (gmt 0)

10+ Year Member



Hello,

so here is the scenario, i have a couple of load balanced servers, with a bunch of domains pointing to the load balancer IP. The website shows a different skin depending on the domain it was accessed from (this works fine). To assure session with the load balancing i used sticky session, problem is from what i know (which is not much) i need to define the domain for the cookie, but we have multiple possible domains.. i define the cookie as following on each web server:

RewriteEngine On
RewriteRule .* - [CO=BALANCEID:balancer.lb1:.domain1.com]


since we have multiple domains, i thought i could add a rewrite rule for each domain and apache would deal with that, something like this:

RewriteEngine On
RewriteRule .* - [CO=BALANCEID:balancer.lb1:.domain1.com]
RewriteRule .* - [CO=BALANCEID:balancer.lb1:.domain2.com]
RewriteRule .* - [CO=BALANCEID:balancer.lb1:.domain3.com]
RewriteRule .* - [CO=BALANCEID:balancer.lb1:.domain4.com]


but as you probably have figured out by now, the sticky session works fine only for domain1.com, the first on the list.

is there some way to instead define one rule that will determine the domain of the cookie dynamically depending on which domain is being accessed?

or maybe some conditional statement depending on the domain being accessed...?

Any idea how to tackle this?

jusaf

6:38 pm on Feb 8, 2011 (gmt 0)

10+ Year Member



I am now trying this approach:

RewriteEngine On
RewriteCond %{HTTP_X_FORWARDED_HOST} ^(www\.)?domain1\.com/ [NC]
RewriteRule .* - [CO=BALANCEID:balancer.lb2:.domain1.com] [L]
RewriteCond %{HTTP_X_FORWARDED_HOST} ^(www\.)?domain2\.com/ [NC]
RewriteRule .* - [CO=BALANCEID:balancer.lb2:.domain2.com] [L]
RewriteCond %{HTTP_X_FORWARDED_HOST} ^(www\.)?domain3\.com/ [NC]
RewriteRule .* - [CO=BALANCEID:balancer.lb2:.domain3.com] [L]


but its not working at all, are those rules wrongly written or
that logic simply wont work?

PS: i also tried with HTTP_HOST instead of HTTP_X_FORWARDED_HOST but it had the same result.

jusaf

6:12 pm on Feb 9, 2011 (gmt 0)

10+ Year Member



I got it now, the correct variable is HTTP:X-Forwarded-Host... maybe this will help someone.