简体   繁体   中英

Sticky session for load balancing with multiple domains

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.

Any idea how to tackle this?

EDIT:

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.

So i've managed to make it work... the second approach was good, only the env var was not good and i also removed the trailing slash:

RewriteCond %{HTTP:X-Forwarded-Host} ^(www\.)?domain1\.com [NC]
RewriteRule .* - [CO=BALANCEID:balancer.lb1:.domain1.com] [L]
RewriteCond %{HTTP:X-Forwarded-Host} ^(www\.)?domain2\.com [NC]
RewriteRule .* - [CO=BALANCEID:balancer.lb1:.domain2.com] [L]

Note that if u want to access that domain via PHP its:

$_SERVER['HTTP_X_FORWARDED_HOST']

Maybe this will save some time to someone else ;)

thanks for the replies!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM