如何在apache和nginx禁止上傳目錄里php的執(zhí)行權(quán)限,接下來將詳細講解。
apache下禁止指定目錄運行php腳本
在虛擬主機配置文件中增加php_flag engine off指令即可,配置如下:
options followsymlinks
allowoverride none
order allow,deny
allow from all
php_flag engine off
另外一種方法,是設(shè)置在htaccess里面的,這個方法比較靈活一點,針對那些沒有apapche安全操作權(quán)限的網(wǎng)站管理員:
apache環(huán)境規(guī)則內(nèi)容如下:apache執(zhí)行php腳本限制 把這些規(guī)則添加到.htaccess文件中
代碼如下:
rewriteengine on rewritecond % !^$
rewriterule uploads/(.*).(php)$ – [f]
rewriterule data/(.*).(php)$ – [f]
rewriterule templets/(.*).(php)$ –[f]
nginx下禁止指定目錄運行php腳本
nginx更簡單,直接通過location條件匹配定位后進行權(quán)限禁止,可在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生效。