Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
location /_private {
    deny    all;

location ~ \.php$ {
    # Workaround PHP vulnerability:
    try_files   $uri =404;

    include /etc/nginx/fastcgi_params;
    keepalive_timeout 0;

    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass    unix:/tmp/php.socket;

I would like to deny access to everything, that's in _private directory. When I try to access _private/a, I get 403 error, like supposed to. But when I try to access _private/b.php, the deny all part is completely ignored.

share|improve this question

Make your /_private location take precedence over the regex match:

location ^~ /_private {

That's it.

The nginx documentation has good information on which location block will apply to a given request. To quote:

  1. Directives with the "=" prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
  3. Regular expressions, in the order they are defined in the configuration file.
  4. If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.
share|improve this answer

Your Answer


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.