0

Is there a better way to do this? I can't find a way to nest or apply boolean operators to conditions in nginx.

Basically if there is a cookie set(non-anonymous user) we want to hit the server. If the cookie is not set and the file exists we want to server the file otherwise hit the server.

        set $test "D";
        if ($http_cookie ~* "session" ) {
            set $test  "${test}C";
        }
        if (-f $request_filename/index.html$is_args$args) {
            set $test  "${test}F";
        }
        if ($test = DF){
            rewrite (.*)/ $1/index.html$is_args$args?
            break;
        }
        if ($test = DCF){
            proxy_pass http://django;
            break;
        }
        if ($test = DC){
            proxy_pass http://django;
            break;
        }
        if ($test = D){
            proxy_pass http://django;
            break;
        }
1

2 Answers 2

2
location / {
    if ($cookie_session) {
        proxy_pass http://django;
    }
    try_files $uri/index.html$is_args$args @django;
}

location @django {
    proxy_pass http://django;
}
1
location / { 
    if ($cookie_session) {
        rewrite ^ /django/;
    }
    if (-f $request_filename/index.html$is_args$args) {
        rewrite (.*)/ $1/index.html$is_args$args; #did you mean ; instead of ?
    }
    proxy_pass http://django;
}
location /django/ {
    proxy_pass http://django;
}

Not sure if much better, but avoided using things considered unsafe in the if statements.

2
  • You don't need the rewrite capture, nginx already does that for you in $request uri. rewrite ^ /django$request_uri; Nov 20, 2012 at 11:09
  • Edited per @Martin Fjordvald rec.
    – Grumpy
    Nov 20, 2012 at 11:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .