✧ 君と集まって星座になれたら ✦ ROKUTOSEI
547 字
3 分钟
个人博客放行谷歌爬虫 IP
更新于 2026-07-19
2026-07-18

防火墙使用 deploy_firewall.sh 脚本管理,其中 bad_asn_v4 ipset 封禁了大量 ASN 的 IP 段

Google 搜索爬虫(Googlebot)的 IP 恰好落在被封禁的范围内,导致 Google Search Console 无法抓取 robots.txt 和 sitemap-index.xml,提示 Robots.txt 无法访问

本文记录解决方案


排查过程#

在 VPS 上执行 iptables -L INPUT -v -n 发现有两层 DROP 规则:

Chain INPUT (policy DROP)
num pkts target prot in out source destination
3 14778 DROP 0 -- * * 45.0.0.0/8 0.0.0.0/0
4 35911 DROP 0 -- * * 0.0.0.0/0 0.0.0.0/0 match-set bad_asn_v4 src

Googlebot 的 IP 属于 bad_asn_v4 匹配的 Google ASN(15169),在规则 4 被直接丢弃

虽然 VPS 本地 wget 能正常返回(绕过 iptables),但外部 Googlebot 始终无法连接


解决方案#

在 bad_asn_v4 DROP 规则之前插入一条白名单,仅放行 Google 爬虫(Googlebot)的 80/443 端口流量

注意白名单规则必须插在 bad_asn_v4 DROP 之前,否则不会生效

第一步:安装 jq#

Terminal window
sudo apt install jq -y

第二步:创建 ipset 并拉取 Googlebot IP 列表#

只放行 Google 搜索爬虫,使用官方发布的专用 IP 列表:

https://developers.google.com/static/crawling/ipranges/common-crawlers.json
Terminal window
sudo ipset create google_whitelist hash:net 2>/dev/null
curl -s https://developers.google.com/static/crawling/ipranges/common-crawlers.json | \
jq -r '.prefixes[] | .ipv4Prefix // .ipv6Prefix // empty' | \
while read cidr; do
sudo ipset add google_whitelist "$cidr" 2>/dev/null
done

第三步:插入 iptables 白名单规则#

放在 bad_asn_v4 DROP 规则之前(45.0.0.0/8 DROP 之后,即第 4 条位置):

Terminal window
sudo iptables -I INPUT 4 -p tcp -m multiport --dports 80,443 \
-m set --match-set google_whitelist src -j ACCEPT

Googlebot 请求到 80/443 端口时,先在规则 4 匹配白名单直接 ACCEPT,不会被后续的 bad_asn_v4 DROP

第四步:持久化#

Terminal window
sudo netfilter-persistent save

第五步:设置定时更新(可选)#

Google 的 IP 段会动态变化,每周自动同步一次:

Terminal window
sudo crontab -e

添加一行:

0 3 * * 1 curl -s https://developers.google.com/static/crawling/ipranges/common-crawlers.json | jq -r '.prefixes[] | .ipv4Prefix // .ipv6Prefix // empty' | while read cidr; do ipset add google_whitelist "$cidr" 2>/dev/null; done >/dev/null 2>&1

每周一凌晨 3 点自动更新


验证#

Terminal window
# 检查 iptables 规则顺序
sudo iptables -L INPUT -v -n --line-numbers
# 期望输出:白名单规则(第4条)在 bad_asn 规则(第5条)之前
# 4 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 80,443 match-set google_whitelist src
# 5 DROP 0 -- * * 0.0.0.0/0 0.0.0.0/0 match-set bad_asn_v4 src
# 检查 ipset 条目数
sudo ipset list google_whitelist | wc -l

然后在 Google Search Console 重新测试抓取

Rokutosei
Rokutosei ✦ 六等星
“君と集まって星座になれたら”
个人博客放行谷歌爬虫 IP
https://blog.rokutosei.com/posts/others/googlebot/
作者
Rokutosei
发布于
2026-07-18
修改于
2026-07-19
许可协议
CC BY-NC-SA 4.0
✧ 文章目录 ✦
1
排查过程
2
解决方案
第一步:安装 jq
第二步:创建 ipset 并拉取 Googlebot IP 列表
第三步:插入 iptables 白名单规则
第四步:持久化
第五步:设置定时更新(可选)
3
验证