银河SEO 知识库

知识库 / 阶段 7 · 站点配置 · 风险与合规 / 搜索引擎优化(SEO) ↗ / 第 093 节

网站 Nginx 配置机器人爬取规则

完整 Nginx 配置(可直接用)#

一、Nginx 核心思路#

Nginx 限速模块 limit_req 本身不能直接识别 User-Agent,所以我们用 map 标记爬虫limit_req_zone 限速location 启动限速策略

最终逻辑:

  • Google/Bing/Yandex → is_searchbot = 1 → 放行

  • AI 爬虫、普通爬虫 → is_badbot = 1 → 限速 5r/s

  • 空 UA / 可疑行为 → 限速 & 可阻断

重要:请把下面所有配置放进 http {} #

二、识别搜索引擎(放行)#

map $http_user_agent $is_searchbot {

default 0;

"~*Googlebot" 1;

"~*Bingbot" 1;

"~*YandexBot" 1;

}

SEO 不会受影响。

三、识别 AI 爬虫 & 一般爬虫(你的限速对象)#

map $http_user_agent $is_badbot {

default 0;



# AI 爬虫

"~*GPTBot" 1;

"~*ChatGPT" 1;

"~*ClaudeBot" 1;

"~*anthropic" 1;

"~*Perplexity" 1;



# 常规爬虫

"~*bot" 1;

"~*crawler" 1;

"~*spider" 1;

"~*scrapy" 1;

"~*python" 1;

"~*curl" 1;

"~*wget" 1;



# 伪装爬虫

"" 1; # 空 UA

}

SEO 搜索引擎不受影响
AI 爬虫 & 普通爬虫都能正确识别

四、设置限速 Zone(5 次每秒)#

limit_req_zone $is_badbot zone=botlimit:10m rate=5r/s;

含义:#

  • 每秒最多 5 请求(你要求的)

  • 超速会被 Nginx 拒绝(503 或自定义 429)

五、在 server/location 中启用限制#

放在你所有业务 location 之前:

server {

...



# 限速 AI & 普通爬虫

if ($is_badbot = 1) {

 limit_req zone=botlimit burst=5 nodelay;

}



# 搜索引擎不受限

if ($is_searchbot = 1) {

 return 200;

}



...

}

burst=5#

允许偶尔瞬时多 5 个请求
避免误伤工具类爬虫

nodelay#

超过速率立即阻断(不会排队)

六、可选:返回 429 替代 503(更符合行业标准)#

limit_req_status 429;

七、完整可用 Nginx 配置(复制即用)#

http {

# 放行 Google/Bing/Yandex

map $http_user_agent $is_searchbot {

 default 0;

 "~*Googlebot" 1;

 "~*Bingbot" 1;

 "~*YandexBot" 1;

}



# 限制 AI 与一般爬虫

map $http_user_agent $is_badbot {

 default 0;



 # AI 爬虫

 "~*GPTBot" 1;

 "~*ChatGPT" 1;

 "~*ClaudeBot" 1;

 "~*anthropic" 1;

 "~*Perplexity" 1;



 # 常规爬虫

 "~*bot" 1;

 "~*crawler" 1;

 "~*spider" 1;

 "~*scrapy" 1;

 "~*python" 1;

 "~*curl" 1;

 "~*wget" 1;



 # UA 为空 = 可疑

 "" 1;

}



# 限速区:每秒 5 次

limit_req_zone $is_badbot zone=botlimit:10m rate=5r/s;

limit_req_status 429;



server {

 listen 80;

 server_name example.com;



 # 搜索引擎直接放行

 if ($is_searchbot = 1) {

 return 200;

 }



 # 限速爬虫

 if ($is_badbot = 1) {

 limit_req zone=botlimit burst=5 nodelay;

 }



 location / {

 try_files $uri $uri/ =404;

 }

}

}

八、效果总结(满足你全部要求)#

类型 行为 是否限速
Googlebot 完全放行 不限速
Bingbot 完全放行 不限速
YandexBot 完全放行 不限速
GPTBot / ClaudeBot / ChatGPT 限速 5 次/秒
普通 bot 限速 5 次/秒
伪装 bot(空 UA / 工具爬虫) 限速或阻断 5 次/秒
人类用户 不受影响

这是目前 NGINX 最健壮 + SEO 完全友好 + 不误伤 + 最容易维护 的方案。