Welcome! Log In Create A New Profile

Advanced

Re: Switching backends based on a cookie

January 28, 2010 08:51PM
Eugaia Wrote:
-------------------------------------------------------
> Hi,
>
> saltyflorida wrote:
> > Is it possible to switch backend clusters of
> servers based on a cookie?
> >
> > I would like to set a cookie named "env" and do
> something like this:
> >
> > if ($http_cookie ~* "env=testing(;|$)")
> {
> > proxy_pass http://backend_testing;
> > }
> > if ($http_cookie ~* "env=staging(;|$)")
> {
> > proxy_pass http://backend_staging;
> > }
> > if ($http_cookie ~*
> "env=production(;|$)") {
> > proxy_pass
> http://backend_production;
> > }
> >
> > However the "proxy_pass" directive is not
> allowed inside an "if". Is there another way I can
> approach this?
> >
> >
> Take a look at the map module :
>
> http://wiki.nginx.org/NginxHttpMapModule
>
> One possibility would be :
>
> http {
>
> map $cookie_env $backend {
>
> testing http://backend_testing;
> staging http://backend_staging;
> production http://backend_production;
> }
>
> server {
> ...
> proxy_pass $backend;
>
> }
>
> }
>
> Marcus.
>
>
> _______________________________________________
> nginx mailing list
> nginx@nginx.org
> http://nginx.org/mailman/listinfo/nginx

Marcus, thank you for your response. The map module is very useful.
I implemented your suggestion and am now able to switch backend servers
using the cookie.

Now I have another problem: the cache is storing pages generated by
the 3 different backend clusters. Is there a way I can bypass the cache
if the cookie is set to either "testing" or "staging"?

Here is my simplified config:
http {
upstream backend_testing {
ip_hash;
server ...
}
upstream backend_staging {
ip_hash;
server ...
}
upstream backend_production {
ip_hash;
server ...
}
proxy_cache_path /mnt/nginx_cache levels=1:2
keys_zone=one:100m
inactive=7d max_size=10g;
proxy_temp_path /var/www/nginx_temp;

map $cookie_uslnn_env $mybackend {
default http://backend_production;
testing http://backend_testing;
staging http://backend_staging;
production http://backend_production;
}

server {
location / {
proxy_pass $mybackend;
proxy_cache one;
proxy_cache_key $my_cache_key;
proxy_cache_valid 200 302 304 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
proxy_cache_use_stale updating error timeout invalid_header http_500 http_502 http_503 http_504;
}
location /wp-admin {
proxy_pass $mybackend;
proxy_read_timeout 300;
}
}
}

Thanks,
Eliot
Subject Author Posted

Switching backends based on a cookie

saltyflorida January 28, 2010 06:43PM

Re: Switching backends based on a cookie

Eugaia January 28, 2010 07:06PM

Re: Switching backends based on a cookie

merlin corey January 28, 2010 07:28PM

Re: Switching backends based on a cookie

merlin corey January 28, 2010 07:30PM

Re: Switching backends based on a cookie

saltyflorida January 28, 2010 09:01PM

Re: Switching backends based on a cookie

Eugaia January 28, 2010 07:54PM

Re: Switching backends based on a cookie

merlin corey January 29, 2010 12:44PM

Re: Switching backends based on a cookie

saltyflorida January 28, 2010 07:57PM

Re: Switching backends based on a cookie

Eugaia January 28, 2010 09:54PM

Re: Switching backends based on a cookie

saltyflorida January 29, 2010 02:05AM

Re: Switching backends based on a cookie

saltyflorida January 29, 2010 02:13AM

Re: Switching backends based on a cookie

Eugaia January 29, 2010 05:58AM

Re: Switching backends based on a cookie

saltyflorida February 03, 2010 12:31AM

Re: Switching backends based on a cookie

saltyflorida January 28, 2010 08:51PM

Re: Switching backends based on a cookie

Piotr Sikora January 28, 2010 09:10PM

Re: Switching backends based on a cookie

saltyflorida January 28, 2010 09:18PM

Re: Switching backends based on a cookie

Piotr Sikora January 28, 2010 09:20PM

Re: Switching backends based on a cookie

Laurence Rowe January 29, 2010 02:24PM

Re: Switching backends based on a cookie

Eugaia January 29, 2010 06:04PM

Re: Switching backends based on a cookie

Laurence Rowe January 30, 2010 01:50PM

Re: Switching backends based on a cookie

Eugaia January 30, 2010 07:04PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 48
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready