代碼如下:
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生效。