NGinx 拒绝对文件夹的所有访问,但 PHP 脚本不受规则影响
NGinx Denying all access to folders but PHP scripts are not being affected by rule
我正在尝试在我的服务器配置文件中为 NGinx 设置安全指令。我有以下指令:
location /config {
deny all;
return 404;
}
该目录中的所有文件都受到限制,但 PHP 文件不受该指令的影响,我的意图是拒绝一切。我假设我的配置文件中的其他指令覆盖了这个指令,但我是 NGinx 的新手。
这是服务器的完整配置代码:
server{
listen 80;
server_name mydomain.com;
root myrootpath;
index index.php index.html index.htm;
include security-directives;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
security-directives 文件包含第一个代码块中详述的指令。
正则表达式位置块优先于您的前缀位置块,因此 .php
文件不包含在规则中。
使用 ^~
修饰符使您的前缀位置优先于正则表达式位置块。
例如:
location ^~ /config {
return 403;
}
详情见this document。
我正在尝试在我的服务器配置文件中为 NGinx 设置安全指令。我有以下指令:
location /config {
deny all;
return 404;
}
该目录中的所有文件都受到限制,但 PHP 文件不受该指令的影响,我的意图是拒绝一切。我假设我的配置文件中的其他指令覆盖了这个指令,但我是 NGinx 的新手。
这是服务器的完整配置代码:
server{
listen 80;
server_name mydomain.com;
root myrootpath;
index index.php index.html index.htm;
include security-directives;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
security-directives 文件包含第一个代码块中详述的指令。
正则表达式位置块优先于您的前缀位置块,因此 .php
文件不包含在规则中。
使用 ^~
修饰符使您的前缀位置优先于正则表达式位置块。
例如:
location ^~ /config {
return 403;
}
详情见this document。