2010-05-29 28 views
17

我看過一些關於使用Nginx檢查cookie的有限資源,但我找不到我正在尋找的答案,希望Nginx的一些大師可以幫我一把。如果cookie存在,Nginx重定向

基本上我有,我想重定向到一個不同的域,除非用戶有一個cookie,這裏的虛擬主機是我所創建的:

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; 
} 

但它似乎不工作,我得到的錯誤:

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

我不知道如何繼續在這裏,任何想法傢伙?

回答

19

這很有道理。 我會定義另一個虛擬主機(beta.example.com)與不同的根文件夾 並在遇到餅乾 - 做一個重寫

不能爲站點設置不同的根有條件的,但你可以重定向(重寫)到另一個域有條件

這傢伙的例子讓我有點前 http://nicknotfound.com/2009/01/12/iphone-website-with-nginx/