1

I need to configure an nginx server so that: If user has a specific cookie so the server must send a file, else the server must send another one. I read plenty of related articles, but nothing helped, also I know that there is some issues when try_files meets location if but how to resolve this...

I have an example, which should be working, but not in fact:

upstream site {
    server localhost:4321;
}

server {

    listen *:1234;
    server_name site.com;
    root /path/www/;

    ssl on;
    ssl_certificate /path/ssl.crt;
    ssl_certificate_key /path/ssl.key;

    location = / {

        set $variable /path/index.html;

        if ($http_cookie ~* "someCookie=someValue(?:;|$)") {
            set $index_file /path2/index.html;
        }

        rewrite .* $index_file break;

        try_files  $uri/index.html $uri.html $uri @site;
    }

}
kashesandr
  • 111
  • 5

2 Answers2

0

Try something like this:

if ($cookie_myCookieNameIsBlaBlaBla ~* "cookieValueThatIWannaMatch") {
   # my logic in here
}

Just remember that according to nginx's documentation, if is evil, so be careful.

More info:

Cya!

Stefano Martins
  • 1,131
  • 7
  • 10
0

I did it this way, not sure the best solution, but it does work.

location @rewrite {
    if ($http_cookie ~* "someCookie=someValue(?:;|$)") {
        rewrite .* /path1/file1.html last;
    }
    rewrite .* /path2/file2.html last;
}

location = / {
    try_files $uri/index.html $uri.html $uri @rewrite;
}
kashesandr
  • 111
  • 5