proxy_set_header User-Agent 用于透传或修改客户端 UA,需配合 map 指令识别设备类型,并通过 if 或 internal location 实现按 $device_type 路由至不同后端,同时必须显式设置 proxy_set_header User-Agent $http_user_agent 以避免被覆盖为“nginx”。

可以通过 proxy_set_header User-Agent 保留或修改请求头中的 User-Agent,再结合 Nginx 的 map 指令识别设备类型,最终在 location 或 upstream 中按条件转发到不同后端。核心不是靠 proxy_set_header 直接实现转发,而是用它配合其他指令完成设备识别与路由决策。
1. 用 map 指令提取设备类型
Nginx 本身不解析 User-Agent,但可通过 map 模块基于正则匹配做轻量级识别。建议在 http 块中定义:
map $http_user_agent $device_type {
~*android|mobile|blackberry|webos|iphone|ipod|ios|opera\ mini|iemobile|wpdesktop "mobile";
~*tablet|ipad|kindle|silk|nexus\ 7|nexus\ 10 "tablet";
default "desktop";
}说明:
– $http_user_agent 自动获取请求头中的 User-Agent;
– 匹配顺序从上到下,第一个命中即生效;
– 移动优先(含常见手机 UA 关键词),再判平板,其余归桌面;
– 注意转义空格(如 nexus\ 7)和特殊字符。
2. 在 proxy_pass 前根据 $device_type 路由
在 server 或 location 块中,用 if 或更推荐的 try_files + named location 方式跳转,或直接指定不同 upstream:
阿里百炼大模型平台多人共享账号用量统计代理服务。解决公司共用账号时无法区分个人用量的问题。适用场景:1)部署用量统计代理;2)统计多人Token用量和调用次数;3)管理大模型API调用配额。
- 方式一:用 if(简单场景可用,注意 if 在 location 外不支持 proxy_pass)
location / {
if ($device_type = "mobile") {
proxy_pass http://backend_mobile;
proxy_set_header User-Agent $http_user_agent;
break;
}
if ($device_type = "tablet") {
proxy_pass http://backend_tablet;
proxy_set_header User-Agent $http_user_agent;
break;
}
proxy_pass http://backend_desktop;
proxy_set_header User-Agent $http_user_agent;
}- 方式二(更健壮):用多个 location + 正则或前缀匹配,配合
internal重定向
location / {
set $target "desktop";
if ($device_type = "mobile") { set $target "mobile"; }
if ($device_type = "tablet") { set $target "tablet"; }
rewrite ^(.*)$ /_route_$target$1 last;
}
<p>location /_route_mobile/ {
internal;
proxy_pass <a href="https://www.php.cn/link/91d054b242704a7a9196a568fb02974a">https://www.php.cn/link/91d054b242704a7a9196a568fb02974a</a>;
proxy_set_header User-Agent $http_user_agent;
}</p><p>location /_route_tablet/ {
internal;
proxy_pass <a href="https://www.php.cn/link/5adc3ad31cc0aa43aaab7f4e75a9f00d">https://www.php.cn/link/5adc3ad31cc0aa43aaab7f4e75a9f00d</a>;
proxy_set_header User-Agent $http_user_agent;
}</p><p>location /_route_desktop/ {
internal;
proxy_pass <a href="https://www.php.cn/link/e9631bea70bc3ae4018bff0eac2c0986">https://www.php.cn/link/e9631bea70bc3ae4018bff0eac2c0986</a>;
proxy_set_header User-Agent $http_user_agent;
}</p>3. 确保后端能收到原始 User-Agent
proxy_set_header User-Agent $http_user_agent; 这行很重要 —— 默认 Nginx 会把 User-Agent 改成 “nginx”,不显式设置就丢失原始值。如果你希望后端也做二次判断(比如 SSR 渲染),必须透传:
- 不要写成
proxy_set_header User-Agent "custom-string"(覆盖掉原始 UA) - 若需添加标记(如注入 Nginx 转发标识),可用拼接:
proxy_set_header User-Agent "$http_user_agent; via-nginx" - 确保客户端没通过 CDN 或中间代理篡改 UA,否则识别不准
4. 验证与调试技巧
快速验证是否生效:
- 用 curl 模拟不同设备:
curl -H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)..." http://your-domain/ - 在后端日志或 Nginx access_log 中加
$device_type变量:log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$device_type"'; - 临时返回设备类型用于调试:
return 200 "Device: $device_type\n";










