else运算符的替代,而不是nginx中的运算符

时间:2014-09-12 12:46:29

标签: cookies nginx webserver operators

在我的应用程序中,基于cookie,我必须将请求转发到某个apache端口。我想要这样的东西:

 server {
    listen       80;
    server_name example.com;
    location /
    {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-FORWARDED_PROTO https;
        if ($http_cookie ~ 'ver=1' ) {
            proxy_pass   http://127.0.0.1:6060;
        }
        else {
            proxy_pass   http://127.0.0.1:7070;
        }
    }
}

As" else"和"!〜"在nginx.conf中是不允许的,我可以为这种类型的要求做些什么呢?

1 个答案:

答案 0 :(得分:4)

指定了一个变量并使用如下:

server {
    listen       80;
    server_name ver.jeevansathi.com;
    location /
    {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-FORWARDED_PROTO https;
        set $cookie_redirect 0;
        if ($http_cookie ~ 'ver=1' ) {
            set $cookie_redirect 1;
        }
        if ($cookie_redirect ~ 1) {
            proxy_pass   http://127.0.0.1:6060;
        }
        if ($cookie_redirect ~ 0 ) {
            proxy_pass   http://127.0.0.1:7070;
        }
    }
}