一、专门针对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