这几天,我发现我的一两个服务器过载(高于平常的CPU使用率),我查看了 Apache 日志,发现 ChatGPT Bot(也称为 GPTBot/1.0)和字节跳动 Bots(也称为 Bytespider)的访问记录。
您可以通过以下 BASH 命令检查访问您服务器的前 10 个 IP:
1 2 3 | #!/bin/bash awk '{a[$1]++}END{for(v in a)print v, a[v]}' /var/log/apache2/*.log* | sort -k2 -nr | head -10 |
#!/bin/bash awk '{a[$1]++}END{for(v in a)print v, a[v]}' /var/log/apache2/*.log* | sort -k2 -nr | head -10
为什么应该阻止 ChatGPT 和字节跳动 Bots 抓取您的页面?
ChatGPT还有字节跳动都有自己的大模型,他们就是通过抓取你的数据来喂他们的LLMs(大型语言模型)。这些 bots 免费使用您的材料(信息或数据)。它们给您的服务器增加了额外的负担,这是可以避免的。
我不喜欢它们从我的网站获取信息,白撸我的羊毛,但如果您觉得无所谓,可以将它们列入白名单。
如何阻止ChatGPT和字节跳动的爬虫抓取您的页面?
使用 robots.txt 阻止
一种比较软性的阻止方式是在网站根目录的 robots.txt 文件中添加以下内容:
User-agent: GPTBot Disallow: / User-agent: Bytespider Disallow: /
然而,这些爬虫可能选择不遵守这些规则。比如百度爬虫就不遵守。
使用 CloudFlare 的 WAF 规则阻止
另一种更强硬的方法是通过添加一些防火墙规则来阻止它们,例如,您可以添加一个 CloudFlare WAF 规则来阻止它们:
比如还可以在表达式编译器(Expression Editor)里加入其它限制:
1 2 3 4 5 6 | (http.user_agent contains "GPTBot") or (http.user_agent contains "Bytespider") or // 可以根据需求加入其它限制,比如限制 Amazonbot (http.user_agent contains "Amazonbot") or // 访问 WordPress 博客访问评论链接 (http.request.uri contains "?replytocom=") |
(http.user_agent contains "GPTBot") or (http.user_agent contains "Bytespider") or // 可以根据需求加入其它限制,比如限制 Amazonbot (http.user_agent contains "Amazonbot") or // 访问 WordPress 博客访问评论链接 (http.request.uri contains "?replytocom=")
使用 HTTP 头阻止
您可以通过在服务器配置中设置适当的 HTTP 头来阻止特定的用户代理。以下是如何在 htaccess)加速网站”>Apache 和 Nginx 服务器上实现这一点:
对于 Apache,在您的 .htaccess 文件中添加以下内容:
1 2 3 4 5 6 | <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_USER_AGENT} GPTBot [NC,OR] RewriteCond %{HTTP_USER_AGENT} Bytespider [NC] RewriteRule .* - [F,L] </IfModule> |
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_USER_AGENT} GPTBot [NC,OR] RewriteCond %{HTTP_USER_AGENT} Bytespider [NC] RewriteRule .* - [F,L] </IfModule>
对于 Nginx 服务器,在您的 Nginx 配置文件中添加以下内容:
1 2 3 | if ($http_user_agent ~* (GPTBot|Bytespider)) { return 403; } |
if ($http_user_agent ~* (GPTBot|Bytespider)) { return 403; }
使用自定义中间件阻止
如果您对应用程序的服务器端代码有控制权,您可以编写中间件来阻止这些用户代理。
在 Express(Node.js)中的示例:
1 2 3 4 5 6 7 8 | app.use((req, res, next) => { const userAgent = req.headers['user-agent']; if (/GPTBot|Bytespider/i.test(userAgent)) { res.status(403).send('Forbidden'); } else { next(); } }); |
app.use((req, res, next) => { const userAgent = req.headers['user-agent']; if (/GPTBot|Bytespider/i.test(userAgent)) { res.status(403).send('Forbidden'); } else { next(); } });
在 Django(Python)中的示例:
1 2 3 4 5 6 7 8 9 10 11 | from django.http import HttpResponseForbidden class BlockBotsMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): user_agent = request.META.get('HTTP_USER_AGENT', '') if 'GPTBot' in user_agent or 'Bytespider' in user_agent: return HttpResponseForbidden('Forbidden') return self.get_response(request) |
from django.http import HttpResponseForbidden class BlockBotsMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): user_agent = request.META.get('HTTP_USER_AGENT', '') if 'GPTBot' in user_agent or 'Bytespider' in user_agent: return HttpResponseForbidden('Forbidden') return self.get_response(request)
使用这些方法的组合可以有效地阻止 GPT-4 和 ByteSpider bots 访问您的网站。在服务器级别的阻止(通过 HTTP 头、防火墙规则或 WAF)与 robots.txt 指令结合使用可以提供更强大的解决方案。
CloudFlare Bots 安全:阻止AI机器人
CloudFlare最近推出了一键阻止这些用于AI人工智能训练的机器人,只需要在 Bots/安全里一键开启。需要注意的是,当该功能开启后,已经被验证的AI机器人也会被阻止,所以需要谨慎使用。
阻止人工智能机器人(阻止机器人抓取您的内容用于模型训练等人工智能应用程序。阻止人工智能机器人也会阻止经过验证的人工智能机器人。
英文:Why and How You Should Stop the ChatGPT and Bytedance Bots Crawling Your Pages?
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK