bot-verifier: NGINX 的搜索索引机器人验证模块
安装
您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:
- RedHat Enterprise Linux 7, 8, 9 和 10
- CentOS 7, 8, 9
- AlmaLinux 8, 9
- Rocky Linux 8, 9
- Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-bot-verifier
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-bot-verifier
通过在 /etc/nginx/nginx.conf 的顶部添加以下内容来启用模块:
load_module modules/ngx_http_bot_verifier_module.so;
本文档描述了 nginx-module-bot-verifier v0.0.14,于 2026 年 2 月 2 日发布。
概述
http {
# 必需:配置 realip 模块以信任您的上游代理
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.16.0.0/12;
set_real_ip_from 192.168.0.0/16;
real_ip_header X-Forwarded-For;
server {
location / {
bot_verifier on;
bot_verifier_redis_host localhost;
bot_verifier_redis_port 6379;
bot_verifier_redis_connection_timeout 10;
bot_verifier_redis_read_timeout 10;
bot_verifier_redis_expiry 3600;
bot_verifier_repsheet_enabled on;
}
}
}
描述
这是一个 NGINX 模块,旨在验证声称是搜索引擎索引器的行为者。为了确保您的控制不会干扰任何搜索提供商的页面排名,合理地禁用有效搜索引擎机器人的安全机制。问题在于 User Agent 头部不能被信任。为了确保您只允许有效的搜索引擎索引器,您必须根据他们发布的标准进行验证。此模块执行该验证并缓存结果,以确保您不会在每个请求上支付验证罚款。
指令
以下指令仅用于模块配置。
bot_verifier
语法: bot_verifier [on|off]
默认: off
上下文: location
阶段: access
启用或禁用模块。模块只有在设置为 on 时才会生效。
bot_verifier_redis_host
语法: bot_verifier_redis_host <string>
默认: localhost
上下文: location
阶段: access
设置 Redis 主机。此设置用于连接到用于缓存查找结果的 Redis 数据库。
bot_verifier_redis_port
语法: bot_verifier_redis_port <int>
默认: 6379
上下文: location
阶段: access
设置 Redis 端口。此设置用于连接到用于缓存查找结果的 Redis 数据库。
bot_verifier_redis_connection_timeout
语法: bot_verifier_redis_connection_timeout <int>
默认: 10
上下文: location
阶段: access
设置连接到 Redis 时的超时。此设置用于连接到用于缓存查找结果的 Redis 数据库。
bot_verifier_redis_read_timeout
语法: bot_verifier_redis_read_timeout <int>
默认: 10
上下文: location
阶段: access
设置查询 Redis 时的超时。此设置用于连接到用于缓存查找结果的 Redis 数据库。
bot_verifier_redis_expiry
语法: bot_verifier_redis_expiry <seconds>
默认: 3600
上下文: location
阶段: access
设置查询 Redis 时的超时。此设置用于连接到用于缓存查找结果的 Redis 数据库。
bot_verifier_repsheet_enabled
语法: bot_verifier_repsheet_enabled [on|off]
默认: off
上下文: location
阶段: access
启用在 Repsheet 中对失败行为者的黑名单。假设 Repsheet 缓存存在于已配置的 Redis 服务器上。
验证功能
为了确保模块正常工作,您需要发出一个查询以触发失败和成功的情况。
确保首先配置 realip:
http {
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
# ... 其余配置
}
要触发失败情况,请发出以下请求:
curl -A "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html" localhost:8888
这将发出一个自我标识为 Google 机器人的查询。反向和正向查找例程将失败,您将收到 403 响应。
为了确保在识别到机器人时验证工作,请发出以下请求:
curl -H "X-Forwarded-For: 66.249.66.1" -A "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html" localhost:8888
当 realip 模块配置为信任 127.0.0.1(localhost)时,它将使用 X-Forwarded-For 头部值作为客户端 IP。请求应该成功并返回正常响应,因为 66.249.66.1 是一个有效的 Google IP。
开发者设置
此模块包含一个完整的自包含开发环境。这是为了确保模块的工作不会干扰其他 NGINX 安装。要设置环境,请运行 script/bootstrap 命令。这将创建以下目录:
vendor - NGINX 安装将位于此处
build - NGINX 安装将位于此处
此存储库根目录中的 nginx.conf 文件将被符号链接到 build/nginx/conf/nginx.conf,以便更轻松地进行配置更改。您可以使用以下命令启动 NGINX:
build/nginx/sbin/nginx
日志文件位于 build/nginx/logs。您可以通过运行以下命令停止服务器:
build/nginx/sbin/nginx -s stop
如果您正在对模块进行更改,可以通过运行 make compile 重新编译它们。请记得在此成功完成后重启 NGINX。
运行测试套件
此存储库附带一个使用 Test::Nginx 库的测试套件。要运行测试,您需要安装以下库:
cpanm -S install Test::Nginx Test::Nginx::Socket
安装库后,只需运行 make,测试套件将运行。如果您正在提交对该模块的更改,请确保在提交之前运行测试套件。任何破坏测试套件的更改将不被接受。
GitHub
您可以在 nginx-module-bot-verifier 的 GitHub 存储库 中找到此模块的其他配置提示和文档。