⭐ 推荐:社区规则条款 V1.0

Nginx 屏蔽 AI 训练爬虫的配置方法。

周末
周末 萌新站长
发布于 2026-10-20 17:38 ·3 浏览 ·0 回复
内容摘要

在 Nginx 的 http 块用 map 将 AI 爬虫 User-Agent 映射为变量,并在 location 中以 if 返回 403,约十行配置即可上线;但该方案仅能拦截 UA 未伪装的爬虫,需要配合 robots.txt 与日志

结论:在 Nginx 的 http 块里用 map 把 AI 训练爬虫的 User-Agent 映射成一个标记变量,再在 server/location 里 if ($ai_bot) { return 403; },就是成本最低、10 行配置就能上线的方法;但它只能拦住「守规矩、UA 不伪装」的那批爬虫,配合 robots.txt 声明和日志观察才算完整方案。

Nginx 屏蔽 AI 爬虫最短可用的配置是什么?

**结论:两段配置——http 块定义 map,location 块写 if + return 403,改完 nginx -t 通过后 nginx -s reload 生效。**

第一步,在 nginx.conf 的 http { } 里定义映射(map 指令只能写在 http 块,写在 server 里会报 "map" directive is not allowed here):

http {
    map $http_user_agent $ai_bot {
        default                    0;
        "~*GPTBot"                 1;
        "~*ChatGPT-User"           1;
        "~*OAI-SearchBot"          1;
        "~*ClaudeBot"              1;
        "~*Claude-Web"             1;
        "~*anthropic-ai"           1;
        "~*CCBot"                  1;
        "~*Bytespider"             1;
        "~*Amazonbot"              1;
        "~*meta-externalagent"     1;
        "~*PerplexityBot"          1;
        "~*cohere-ai"              1;
        "~*Diffbot"                1;
        "~*ImagesiftBot"           1;
        "~*YouBot"                 1;
    }
}

~* 表示不区分大小写的正则匹配,$http_user_agent 是 Nginx 内置变量,取请求头 User-Agent 的值。

第二步,写进站点配置的 location / 里,同时给 robots.txt 单独开一个 location 放行,避免把声明文件也挡掉:

server {
    listen 443 ssl;
    server_name example.com;

    location = /robots.txt {
        # 不拦截,正常返回
    }

    location / {
        if ($ai_bot) {
            return 403;
        }
        # 原有的 proxy_pass / try_files 等配置
    }
}

注意:if 在 Nginx 里被称为「邪恶的」,但 if + return 是官方明确允许的安全用法,不要在 if 里写 proxy_pass、rewrite 之类的指令。

哪些 User-Agent 该进黑名单?

**结论:优先封训练型爬虫(GPTBot、ClaudeBot、CCBot、Bytespider、meta-externalagent),把搜索型爬虫(OAI-SearchBot、ChatGPT-User、PerplexityBot)单独区分开,否则你会同时失去被 AI 搜索引用的机会。**

主流 AI 相关 UA 大致分两类:

UA归属用途
GPTBotOpenAI抓取训练语料
OAI-SearchBotOpenAI为 ChatGPT 搜索建索引
ChatGPT-UserOpenAI用户提问时实时抓取页面
ClaudeBotAnthropic训练 + 检索
anthropic-ai / Claude-WebAnthropic旧版标识
CCBotCommon Crawl开源语料库,被大量模型间接使用
Bytespider字节跳动训练语料
meta-externalagentMetaAI 训练
PerplexityBotPerplexity搜索索引

只想禁止训练、保留 AI 搜索流量,就把 GPTBot、ClaudeBot、CCBot、Bytespider、meta-externalagent、Amazonbot、cohere-ai、Diffbot、ImagesiftBot 拉黑,放行 OAI-SearchBot、ChatGPT-User、PerplexityBot。

Google-Extended 和 Applebot-Extended 为什么不能用 UA 拦?

**结论:Google-Extended 和 Applebot-Extended 不是真实的 User-Agent,它们只是 robots.txt 里的 token,写进 Nginx 的 map 完全无效。**

Google 的爬虫 UA 始终是 Googlebot,Google-Extended 是 2023 年 9 月引入的 robots.txt 声明项,用来控制 Bard/Gemini 的训练用途,不影响搜索排名。Applebot-Extended 同理。想限制这两家,只能改 robots.txt:

User-agent: Google-Extended
Disallow: /

User-agent: Applebot-Extended
Disallow: /

User-agent: GPTBot
Disallow: /

User-agent: CCBot
Disallow: /

robots.txt 对不遵守声明的爬虫没有任何强制力,所以它和 Nginx 拦截是互补关系,不是替代关系。

返回 403、444 还是自定义页面?

**结论:默认用 return 403,追求极致省资源用 return 444,两者差别是「给不给响应」。**

  • 403:标准 HTTP 状态码,返回 Nginx 默认错误页,客户端和日志都能看到明确结果,便于排查误封。
  • 444:Nginx 的非标准状态码,直接关闭 TCP 连接、不发送任何响应头,带宽和连接开销最小。用 curl 测试时会返回 curl: (52) Empty reply from server。
  • 自定义页面:return 403 "AI crawlers are not allowed." 或 error_page 403 /403.html;,适合有品牌展示需求,但会多消耗一点流量。

怎么验证配置真的生效?

**结论:用 curl 伪造 UA 打自己的域名,看状态码是不是 403,两次对比即可确认。**


curl -A "GPTBot/1.0" -s -o /dev/null -w "%{http_code}\n" https://example.com/

# 正常浏览器 UA,应返回 200
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" -s -o /dev/null -w "%{http_code}\n" https://example.com/

# 确认 robots.txt 没被误封
curl -A "GPTBot/1.0" -s -o /dev/null -w "%{http_code}\n" https://example.com/robots.txt

改配置前先 nginx -t 做语法检查,通过后再 nginx -s reload,避免线上 502。如果想知道每天被拦了多少次,在 log_format 里加上 $ai_bot 变量,然后在访问日志里 grep " 1 " 统计。

为什么说这套配置只能挡住一半?

**结论:User-Agent 是客户端自己填的字符串,任何爬虫改一行代码就能伪装成 Chrome;用无头浏览器渲染抓取的爬虫,Nginx 层面完全识别不出来。**

要真正提高门槛,需要叠加三层手段:一是在 Cloudflare 后台开启 Block AI bots(2024 年上线,基于 UA + 行为特征自动识别);二是对无 UA、高频访问的 IP 段做 limit_req 限速,例如 limit_req_zone $binary_remote_addr zone=ai:10m rate=20r/m;;三是在页面关键数据上做懒加载或服务端渲染开关,让纯静态抓取拿不到有效内容。

收束一句:Nginx 的 map + if + return 403 是 5 分钟能上线、零依赖的第一道闸门,robots.txt 是法律层面的声明,Cloudflare 或 WAF 才是行为层的兜底——三者都做,才算对 AI 训练爬虫有实际约束力。

本文转载自 GJ站长论坛,原文地址:https://www.gj0.com/thread-2323.html
转载请注明出处,版权归原作者所有。
—/ 5暂无评分
还没有评分,来打第一分吧

全部回复 0

还没有回复,来抢沙发~