拒绝访问PHP文件(Nginx)

时间:2022-09-21 01:10:59

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:

我希望Nginx拒绝访问特定的PHP文件,让我们称之为donotexposeme.php,但它似乎不起作用,PHP脚本像往常一样运行。这是我在配置文件中的内容:

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.

当然,每次编辑配置时,我都会执行sudo service nginx reload(或restart)。

1 个解决方案

#1


14  

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

可以在此处找到nginx确定哪个位置匹配的顺序:

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.

第一个是精确匹配,而后者是正则表达式前缀匹配。

#1


14  

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

可以在此处找到nginx确定哪个位置匹配的顺序:

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.

第一个是精确匹配,而后者是正则表达式前缀匹配。