Apache、Nginx 下禁止 PHP 执行的办法

为了安全,我们会禁止静态文件目录执行 PHP 的权限,下面是 Apache 和 Nginx 下禁止 PHP 执行的办法。

通过修改 .htaccess 文件禁止 Apache 执行 PHP

Apache 下修改 .htaccess 文件禁止 PHP 执行的办法

如果你使用的是 Apache,创建一个.htaccess文件,放到要禁止执行 PHP 的文件夹内,内容如下:

<Files ~ ".php">
    Order allow,deny 
    Deny from all 
</Files>

如果要禁止多个目录,可以使用上面的方法,在多个目录里放置 .htaccess 文件,也可以在根目录下的 .htaccess 文件里使用 Rewrite 进行设置。

RewriteEngine on RewriteCond % !^$
RewriteRule uploads/(.*).(php)$ – [F]
RewriteRule img/(.*).(php)$ – [F]
RewriteRule css/(.*).(php)$ –[F]

Nginx 下修改配置文件禁止 PHP 执行的办法

Nginx 下禁止 PHP 执行需要修改 Nginx 的配置文件,如果针对单个目录:

location ~* ^/uploads/.*\.(php|php5)$
{
    deny all;
}

如果针对多个目录:

location ~* ^/(css|uploads)/.*\.(php|php5)$
{
    deny all;
}

修改完成之后,重启 Nginx 使配置文件生效。

现在很多的控制面板支持增加 Nginx 的配置文件,比如 Plesk,修改完成,保存的时候会自动重启 Nginx 服务,还是很方便的。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>