device-type: NGINX 设备检测模块
需要 GetPageSpeed NGINX Extras 订阅的专业计划(或更高版本)。
安装
您可以在任何基于 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-device-type
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-device-type
通过在 /etc/nginx/nginx.conf 的顶部添加以下内容来启用该模块:
load_module modules/ngx_http_device_type_module.so;
本文档描述了 nginx-module-device-type v1.0.0,于 2026 年 2 月 15 日发布。
高性能设备检测,直接在边缘进行。
为什么选择这个模块?
设备检测对于以下方面至关重要:
- 自适应服务 — 提供移动优化的资源而无需重定向
- 缓存分段 — 按设备类型变化缓存内容
- 机器人管理 — 阻止抓取程序,限制爬虫速率,允许搜索引擎
- AI 爬虫控制 — 识别和管理 GPTBot、ClaudeBot 和其他 AI 训练爬虫
- 分析丰富 — 将设备上下文传递给后端
传统解决方案需要外部 API 调用,增加延迟和成本。该模块使用预编译的正则表达式模式在 进程内 进行检测 — 无需网络往返,无需按请求收费。
特性
| 特性 | 描述 |
|---|---|
| 22 个 NGINX 变量 | 设备类型、浏览器、操作系统、机器人分类 — 全部作为原生变量 |
| 4,000+ 检测模式 | 由 GetPageSpeed 维护的全面覆盖 |
| 客户端提示支持 | 与现代浏览器的高精度检测 |
| AI 爬虫检测 | 识别 GPTBot、ClaudeBot、Anthropic-AI、CCBot 和 50+ AI 爬虫 |
| 机器人分类 | 分类为 search_engine、ai_crawler、crawler、monitoring、scraper |
| JSON 输出 | 完整的检测结果作为单个 JSON 变量 |
| 零配置 | 加载模块,使用变量 — 无需指令 |
| 每请求缓存 | 每个请求运行一次检测,结果缓存于模块上下文 |
模式统计
- 800+ 机器人模式,带有类别、生产者和 AI 爬虫标志
- 500+ 浏览器模式,带有版本提取和引擎检测
- 300+ 操作系统模式,带有版本和家族分类
- 2,000+ 设备模式,用于品牌和型号检测
快速开始
1. 加载模块
load_module modules/ngx_http_device_type_module.so;
2. 使用变量
server {
# 按设备提供不同内容
location / {
if ($is_mobile) {
rewrite ^(.*)$ /m$1 last;
}
}
# 阻止 AI 爬虫
location /content/ {
if ($is_ai_crawler) {
return 403;
}
proxy_pass http://backend;
}
# 调试端点
location = /device {
default_type application/json;
return 200 $device_json;
}
}
变量参考
设备类型
| 变量 | 值 | 描述 |
|---|---|---|
$device_type |
mobile、tablet、desktop、tv、console、car、wearable、camera、peripheral、bot |
主要分类 |
$is_mobile |
1 / 0 |
手机或仅移动浏览器 |
$is_tablet |
1 / 0 |
平板设备 |
$is_desktop |
1 / 0 |
台式机或笔记本 |
$is_bot |
1 / 0 |
机器人或爬虫 |
$is_tv |
1 / 0 |
智能电视 |
$is_console |
1 / 0 |
游戏机 |
$is_wearable |
1 / 0 |
可穿戴设备 |
设备详情
| 变量 | 示例 | 描述 |
|---|---|---|
$device_brand |
Apple、Samsung、Google |
设备制造商 |
$device_model |
iPhone 15、Galaxy S24、Pixel 8 |
设备型号 |
浏览器
| 变量 | 示例 | 描述 |
|---|---|---|
$browser_name |
Chrome、Firefox、Safari |
浏览器名称 |
$browser_version |
120.0.0.0 |
完整版本字符串 |
$browser_family |
Chrome、Firefox、Safari |
浏览器家族 |
$browser_engine |
Blink、Gecko、WebKit |
渲染引擎 |
操作系统
| 变量 | 示例 | 描述 |
|---|---|---|
$os_name |
Windows、Android、iOS |
操作系统名称 |
$os_version |
14、17.2、11 |
操作系统版本 |
$os_family |
Windows、Android、Unix、iOS、macOS |
操作系统家族 |
机器人分类
| 变量 | 示例 | 描述 |
|---|---|---|
$bot_name |
Googlebot、GPTBot、ClaudeBot |
机器人标识符 |
$bot_category |
search_engine、ai_crawler、crawler |
机器人类别 |
$bot_producer |
Google、OpenAI、Anthropic |
机器人运营商 |
$is_ai_crawler |
1 / 0 |
AI 训练/搜索爬虫 |
JSON 输出
| 变量 | 描述 |
|---|---|
$device_json |
完整的检测结果以 JSON 格式输出 |
示例 JSON:
{
"type": "mobile",
"browser": {"name": "Chrome", "version": "120.0", "engine": "Blink"},
"os": {"name": "Android", "version": "14", "family": "Android"},
"device": {"brand": "Google", "model": "Pixel 8"},
"bot": null
}
机器人检测 JSON:
{
"type": "bot",
"browser": {"name": "", "version": "", "engine": ""},
"os": {"name": "", "version": "", "family": ""},
"device": {"brand": "", "model": ""},
"bot": {"name": "GPTBot", "category": "ai_crawler", "producer": "OpenAI", "is_ai": true}
}
用例
自适应内容服务
# 提供移动优化的页面
location / {
set $variant "desktop";
if ($is_mobile) { set $variant "mobile"; }
if ($is_tablet) { set $variant "tablet"; }
proxy_pass http://backend;
proxy_set_header X-Device-Variant $variant;
}
缓存键变化
# 按设备类型变化缓存
proxy_cache_key "$scheme$request_uri|$device_type";
# 或仅按移动与桌面
proxy_cache_key "$scheme$request_uri|$is_mobile";
机器人管理
# 阻止 AI 训练爬虫
location / {
if ($is_ai_crawler) {
return 403 "不允许 AI 爬虫";
}
}
# 限制抓取程序速率,允许搜索引擎
limit_req_zone $binary_remote_addr zone=scraper:10m rate=1r/s;
location / {
if ($bot_category = "scraper") {
limit_req zone=scraper;
}
proxy_pass http://backend;
}
分析头部
location /api/ {
proxy_set_header X-Device-Type $device_type;
proxy_set_header X-Device-Brand $device_brand;
proxy_set_header X-Browser $browser_name;
proxy_set_header X-OS $os_name;
proxy_set_header X-Is-Bot $is_bot;
proxy_pass http://backend;
}
调试端点
location = /debug/device {
default_type text/plain;
return 200 "类型: $device_type
移动: $is_mobile
平板: $is_tablet
桌面: $is_desktop
机器人: $is_bot ($bot_name)
AI 爬虫: $is_ai_crawler
浏览器: $browser_name $browser_version
引擎: $browser_engine
操作系统: $os_name $os_version ($os_family)
设备: $device_brand $device_model
";
}
客户端提示
为了提高与基于 Chromium 的浏览器的检测准确性,广告客户端提示:
add_header Accept-CH "Sec-CH-UA, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version";
add_header Critical-CH "Sec-CH-UA-Mobile";
该模块处理以下客户端提示:
| 头部 | 用途 |
|---|---|
Sec-CH-UA |
浏览器品牌和版本 |
Sec-CH-UA-Mobile |
移动指示符 (?1 = 移动) |
Sec-CH-UA-Model |
设备型号(平板/手机提示) |
Sec-CH-UA-Platform |
操作系统名称 |
Sec-CH-UA-Platform-Version |
操作系统版本 |
客户端提示提供的信息比 User-Agent 解析更准确,是设备检测的未来。
检测优先级
该模块按以下顺序评估信号:
- 机器人检测 — 与 800+ 机器人模式进行正则匹配
- 客户端提示 — 如果存在,用于移动/平板/浏览器/操作系统检测
- 快速 UA 检查 — 对 iPad、iPhone、PlayStation 等进行快速子字符串匹配
- 完整设备模式 — 2,000+ 正则模式用于品牌/型号
- Android 平板启发式 — Android 没有 "Mobile" 标记 → 平板
- 移动回退 — "Mobile" 标记 → 移动
- 浏览器检测 — 500+ 模式带有版本捕获
- 引擎检测 — Blink、Gecko、WebKit 等
- 操作系统检测 — 300+ 模式带有家族分类
结果在模块上下文中按请求缓存。
与替代方案的比较
| 特性 | 本模块 | 51Degrees | WURFL | DeviceAtlas |
|---|---|---|---|---|
| 进程内检测 | ✅ | ✅ | ✅ | ❌ (云) |
| 无每请求成本 | ✅ | ❌ | ❌ | ❌ |
| AI 爬虫检测 | ✅ | ❌ | ❌ | ❌ |
| 机器人分类 | ✅ | 有限 | 有限 | 有限 |
| 客户端提示 | ✅ | ✅ | ✅ | ✅ |
| JSON 输出 | ✅ | ❌ | ❌ | ❌ |
| 动态模块 | ✅ | ✅ | ✅ | N/A |