简体   繁体   中英

Nginx redirect if cookie present

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 example.com;

  if ($http_cookie ~* "dev_cookie" ) {
    root /home/deploy/apps/example/current/public;
    passenger_enabled on;
    rack_env production;
    break;
  }
  rewrite ^/(.*) http://beta.example.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?

That makes sense. I would define another virtual host (beta.example.com) with that different root folder and upon encountering cookie - do a rewrite

You can't set different roots for a domain conditionally, but you can redirect (rewrite) to another domain conditionally

This guy's example helped me a bit ago http://nicknotfound.com/2009/01/12/iphone-website-with-nginx/

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