Search code examples
phplinuxubuntunginxwebserver

Deny access to a PHP file (Nginx)


I want Nginx to deny access to a specific PHP file, let's call it donotexposeme.php, but it doesn't seem to work, the PHP script is run as usual. Here is what I have in the config file:

location / {
    root /var/www/public_html;
    index index.php;
}

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/public_html$fastcgi_script_name;
    include fastcgi_params;
}

location /donotexposeme.php {
    deny all;
}

Of course, I do sudo service nginx reload (or restart) each time I edit the config.


Solution

  • The order in which nginx determines which location matches can be found here:

    http://wiki.nginx.org/HttpCoreModule#location

    Using either of these will be matched before any other regular expression:

    location = /donotexposeme.php
    

    Or

    location ^~ /donotexposeme\.php
    

    The first is an exact match whereas the latter is a regular expression prefix match.