Nginx redirect if cookie present

Posted by Joseph Silvashy on Stack Overflow See other posts from Stack Overflow or by Joseph Silvashy
Published on 2010-05-29T19:31:57Z Indexed on 2010/05/29 20:12 UTC
Read the original article Hit count: 232

Filed under:

I've seen some limited resources on checking for cookies with Nginx, but I couldn't really find the answer I was looking for, hopefully some of you Nginx masters can give me a hand.

Essentially I have a vhost that I'd like to redirect to a different domain unless the user has a cookie, here is what I've created:

server {
  listen 80;
  server_name rerecipe.com;

  if ($http_cookie ~* "ngl_beta_pass" ) {
    # set $beta off;
    root /home/deploy/apps/rerecipe/current/public;
    passenger_enabled on;
    rack_env production;
    break;
  }
  rewrite ^/(.*) http://beta.rerecipe.com/$1 permanent;
}

But it doesn't seem to work, I get the error:

[emerg]: "root" directive is not allowed here in /opt/nginx/conf/nginx.conf:45

I'm not sure how to proceed here, any ideas guys?

© Stack Overflow or respective owner

Related posts about nginx