网站的服务器很多恶意爬虫导致MySQL宕机网站打不来怎么办?

我在宝塔面板上看到网站的日流量很高,然后看很多DuckAssistBot、Meta-ExternalFetcher、TokenFoundryBot、ChatGLM-Spider、KimiBot这些垃圾爬虫,就类似这样的:

104.22.24.226 - - [11/Oct/2026:18:38:31 +0800] "GET /@fs/.env?raw?? HTTP/2.0" 404 - "-" "Mozilla/5.0 (compatible; MoonshotBot/1.0; +https://kimi.ai/)"

一大堆访问根本不存在的文件,非常多不同的IP,还有的IP会伪装UA,这种要怎么屏蔽呢?

请先 登录 后评论

1 个回答

一、专门针对UA屏蔽IP返回403

Nginx设置

server {
if ($http_user_agent ~* (sqlmap|nikto|wpscan|nmap|masscan|dirbuster|gobuster|acunetix|nessus|semrush|ahrefs|mj12bot|dotbot|petalbot|gptbot|chatgpt-user|openai-search|ai2bot|claudebot|anthropic-ai|amazonbot|meta-webindexer|facebookexternalhit|externalfetcher|meta-externalagent|sitechecker|seoscout|seranking|Applebot|Reflectionbot)) {
return 403;
}
}

Apache设置

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (sqlmap|nikto|wpscan|nmap|masscan|dirbuster|gobuster|acunetix|nessus|semrush|ahrefs|mj12bot|dotbot|petalbot|gptbot|chatgpt-user|openai-search|ai2bot|claudebot|anthropic-ai|amazonbot|meta-webindexer|facebookexternalhit|externalfetcher|meta-externalagent|sitechecker|seoscout|seranking|Applebot|Reflectionbot) [NC]
RewriteRule ^ - [F,L]

把你要添加的恶意AI爬虫UA填到里面去。

二、使用Fail2ban屏蔽高频IP

你用的是宝塔面板,直接在软件商店下载Fail2ban安装,然后添加设置,屏蔽时间设置长一点,时间到了会自动解除封禁的IP,如果再次高频访问,又会被封禁,自动解除主要是为了防止把Bing、百度的爬虫给屏蔽了。

更多网站防御,可以看这篇文章,非常全面的干货:https://www.shoushai.com/p/2031

请先 登录 后评论