Nginx重定向(如果存在cookie) [英] Nginx redirect if cookie present
问题描述
我在Nginx上检查Cookie的资源有限,但是我并没有真正找到想要的答案,希望你们中的一些Nginx大师可以帮助我.
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.
基本上,我有一个虚拟主机,除非用户具有cookie,否则我想将其重定向到其他域,这是我创建的:
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?
推荐答案
这很有道理. 我将用另一个不同的根文件夹定义另一个虚拟主机(beta.example.com) 并在遇到Cookie时-进行重写
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
这个家伙的例子对我有帮助 http://nicknotfound.com/2009/01/12/iphone- website-with-nginx/
This guy's example helped me a bit ago http://nicknotfound.com/2009/01/12/iphone-website-with-nginx/
这篇关于Nginx重定向(如果存在cookie)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!