Switching backends based on a cookie

saltyflorida nginx-forum at nginx.us
Fri Jan 29 05:20:16 MSK 2010


merlin corey Wrote:
-------------------------------------------------------
> On Thu, Jan 28, 2010 at 4:03 PM, Marcus Clyne 
> 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 at nginx.org
> > http://nginx.org/mailman/listinfo/nginx
> >
> 
> Marcus' solution should be just fine, but I feel I
> must ask an
> important question:
> 
> Doesn't it make more sense to have production,
> static, and dev as
> separate server blocks entirely with their own
> hostnames?  This is, at
> the least, traditional :).
> 
> -- Merlin
> 
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://nginx.org/mailman/listinfo/nginx

Marcus,
Thank you for the quick response. I will try the map module.
I forgot to mention that I am using caching with the HTTP Proxy module and that I only want to cache responses from the production servers. When I have the cookie set to "testing" or "staging", I'd like to bypass the cache and talk directly to the backend. Does this sound feasible?

Merlin,
I realize this setup is unorthodox. We are using Wordpress MU and it generates different pages based on the domain name. We are serving many domains with one server cluster and wanted to be able to test using the production domain names.

Thanks,
Eliot

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,46979,47012#msg-47012




More information about the nginx mailing list