其他运营商的替代和nginx的不等于运算符(alternative of else operator

2019-10-20 22:28发布

在我的应用程序,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;
        }
    }
}

至于“其他”和“!〜”不准在nginx.conf,我能为这种类型的需求呢?

Answer 1:

分配一个变量和像这样使用:

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


文章来源: alternative of else operator and not equal operator in nginx