首页 > 解决方案 > 如何使用 fail2ban 处理 Nginx 错误“主脚本未知”

问题描述

我使用 Ubuntu 20.04 和 Fail2ban,但不会过滤此类错误。此错误出现在 /var/log/nginx/error.log 中。如何在 Fail2ban 中进行设置以阻止这样的机器人?

2020/12/31 18:02:34 [错误] 674#674: *1003 FastCGI 在标准错误中发送:“主要脚本未知”同时从上游读取响应标头,客户端:xxx.xxx.xxx.xxx,服务器:mydomain。 com,请求:“GET /wp-content/plugins/eshop-magic/download.php?file=../../../../wp-config.php HTTP/1.1”,上游:“fastcgi: //unix:/var/run/php/php7.4-fpm.sock:",主机:" www.mydomain.com "

注意:我想阻止所有 IP 地址尝试访问不存在的网页,使用 fail2ban。我无法在 filter.d 上构建正确的正则表达式

标签: nginxfail2banufw

解决方案


[nginx-badbots]在您的jail.local

[nginx-badbots]

enabled  = true
port     = http,https
filter   = nginx-badbots
logpath  = /var/log/nginx/error.log
maxretry = 2

创建一个名为nginx-badbots.confinside的过滤器文件filter.d

[Definition]

failregex = FastCGI sent in stderr: "Primary script unknown" .*, client: <HOST>

ignoreregex =

重启fail2ban

service fail2ban restart

检查你的nginx-badbots监狱

fail2ban-client status nginx-badbots

推荐阅读