Nginx禁止指定目录运行PHP脚本

时间:2022-07-23
本文章向大家介绍Nginx禁止指定目录运行PHP脚本,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Nginx下禁止指定目录运行PHP脚本

Nginx更简单,直接通过location条件匹配定位后进行权限禁止。

在server配置段中增加如下的配置

如果是单个目录

location ~* ^/uploads/.*.(php|php5)$ 

{  

deny all;

}  

如果是多个目录

location ~* ^/(attachments|uploads)/.*.(php|php5)$ 

{ 

deny all; 

}  

注意:这段配置文件一定要放在下面配置的前面才可以生效。

location ~ .php$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

} 

*后给一个完整的配置示例

location ~ /mm/(data|uploads|templets)/*.(php)$ {

deny all;

}

location ~ .php$ {

try_files $uri /404.html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

} 

配置完后记得重启Nginx生效。