Tanu Gupta
Tanu Gupta

Reputation: 612

alternative of else operator and not equal operator in nginx

In my application, on the basis of cookie, I have to forward the request to certain apache port. I want something like this:

 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" and "!~" are not allowed in nginx.conf, what can I done for such type of requirement?

Upvotes: 1

Views: 4829

Answers (1)

Tanu Gupta
Tanu Gupta

Reputation: 612

Assigned a variable and used like this:

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

Upvotes: 5

Related Questions