nginx – 出于安全原因拒绝除index.php之外的所有* .php请求

操作系统:CentOS 7
nginx:1.6.2
httpd:apache 2.4.6
cms:Drupal 7

在我的服务器被盗用后,我从服务器中删除了所有,重新安装了操作系统和软件,并从备份中恢复数据。 现在我configuration所有的服务在最大的安全风格。

在详细研究访问日志之后 – 我决定拒绝任何除了index.php之外的任何php文件请求,这是为了提高安全性而在站点文档根目录中。

Nginx的访问日志内容很多logging如:

azenv2.php az.php 

 /*/wp-login.php /administrator/index.php /MyAdmin/index.php 

第一类 – 后门(其中一个黑客攻击我的网站,有人从我的服务器发送大量的垃圾邮件)。

其次 – 有人想findstream行的CMS和实用程序,并尝试一些login@密码,如admin @ 123456

我的理由阻止这两个类别的nginx通过拒绝请求到PHP文件是:

  1. 即使有人会上传php-shell ,也不可能使用它

  2. 所有这些请求都是“不好”的一个priory – 并通过nginx拒绝它们将保护drupal(httpd + php + mysql)的工作和耗电。

我目前的configuration为一个虚拟主机:

 server { listen <server-ip>; server_name <site-name>; location ~* /sites/default/files/styles/ { try_files $uri @imagestyles; } location @imagestyles { proxy_pass http://127.0.0.1:<port>; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; access_log off; } location ~* \.(jpg|jpeg|gif|png|ico|css|bmp|swf|js|pdf|zip|rar|mp3|flv|doc|xls)$ { root <site-documents-root>; access_log off; } location ~ (^|/)\. { deny all; } location / { proxy_pass http://127.0.0.1:<port>; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; access_log <path-to-log-folder>/nginx_access.log main; } } 

nginx.conf – 安装后没有改变。


UPDATE
最后,我为拒绝创build这个configuration:

 location ~ \.php$ { access_log /path/to/log/nginx_deny.log name_log; deny all; } 

和这个configuration代理:

 location =/index.php { proxy_pass http://127.0.0.1:<port>; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location =/cron.php { proxy_pass http://127.0.0.1:<port>; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location / { proxy_pass http://127.0.0.1:<port>; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } 

1)。 所以关于攻击尝试的完整信息是在日志中收集的。
2)。 服务器不会为坏请求做额外的工作。
3)。 Drupal的cron可能会工作。

您可以通过多种方式实现此目的。

与您的configuration文件直接集成,您可能希望简单地包含以下部分;

 location ~ \.php$ { try_files index.php @error; fastcgi_pass ...; fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; ... } location @error { [config of however you want to handle errors] } 

在允许访问/执行之前,将检查所请求文件的存在。

除上述之外,实际上我个人推荐使用fail2ban ,如果configuration正确,将为您提供更全面的安全性。 您可以将其configuration为实时监控您的访问日志,并禁止IP访问您的服务器(s),通过自动创build新的iptables规则,并指定禁止时间。

就我个人而言,我的服务器configuration为根据本文使用nginx的fail2ban(或者至less基于这一点 – 您可以根据需要进行修改)。