Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'd like to set up nginx location match but only for a specific source address. Unfortunately if I already match a location, but not the condition I can't fall through to the next location. What's the way to solve it nicely?

server {
    ...
    location / {
        index app.php;
        try_files $uri @rewriteapp;
    }
    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }
    ...
    location ~ ^/admin {
        if ($source_trusted = "UNKNOWN") {
            return 403;
        }
    }
    ...
    location ~ ^/... {
        fcgi_pass ...
        ...
    }
}

With this config going to /admin from a source that sets $source_trusted = OK (via geo module) results in a 404, rather than the page loading.

share|improve this question

1 Answer 1

up vote 2 down vote accepted
+50

Remember that nginx generally only evaluates one location unless specifically instructed to use another, such as when calling to a named location in try_files (which you do).

So when you hit the ^/admin location, you only need to do whatever is appropriate. In this case, it's most likely going to be exactly the same try_files as you used in location /.

    location ~ ^/admin {
        if ($source_trusted = "UNKNOWN") {
            return 403;
        }
        try_files $uri @rewriteapp;
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.