网站的目录结构:
2 directories, 7 files
需求:
1.网站根目录下,只允许访问index.php,其他任何php文件不得访问。即不能访问1.php和test1.php
2.子目录下test中,只允许/test/index.php和/test/test1.php 2个php文件被访问
3.子目录下的/uploads的所有php文件不可以访问,即,不能访问/uploads/1.php
nginx的配置文件,如下:
#安全防护,防止文件包含漏洞,比如请求中有http://a.com/plus/../../../test.php这种
if ( $fastcgi_script_name ~ \..*\/.*php ) {
return 403;
}
#对于/uploads下所有php文件都不能被解析
location ~* ^/uploads/.*\.(php|php5)?$
{
deny all;
}
#对于所有一下后缀的文件返回403
location ~ .*\.(htaccess|htpasswd|asp|aspx|jsp|asa|mdb|zip|sql|tar.gz|sh|py|pl)?$
{
deny all;
}
#允许访问/index.php
location ~ /index\.(php|php5)?$
{
fastcgi_pass unix:/usr/local/php/var/run/php-fpm-99bbs.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/sites/bbs.99.com.cn$fastcgi_script_name;
}
#允许访问/test/test1.php
location ~ /test/test1\.(php|php5)?$
{
fastcgi_pass unix:/usr/local/php/var/run/php-fpm-99bbs.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/sites/bbs.99.com.cn$fastcgi_script_name;
}
#拒绝所有的php请求
location ~ /.*\.(php|php5)?$
{
deny all;
}
看效果:
#对于/uploads下所有php文件都不能被解析
#允许访问/index.php
#不允许访问根目录下的其他任何php文件
#允许访问指定目录下的php文件即/test/test1.php
而/test/2.php文件是不能被php解析的,假设它是木马文件的话
总结:大部分的使用nginx的架设的web的配置文件中,都是使用的允许所有的php文件可以被解析,这种配置方案有些过于简单的了,安全性不高。我们的做运维的,有个原则,就是最小化使用原则,即需要就用,不需要的就不用,再这里,就是只允许我们网站程序中需要php解析的文件运行。其他的文件一律deny.
nginx的安全配置