Skip to content

一个域名两个版本:nginx 的 UA 分流是怎么做的 ​

工具箱有电脑版和手机版,但只有 emphant.com 一个主域。手机用户访问 emphant.com/text/word-counter/ 时,希望他直接落到手机版的对应工具,而不是先看到电脑版再手动切。

这件事 nginx 全包了,前端不需要知道自己是哪个版本。

分流矩阵 ​

先把目标写清楚,再谈实现:

访问者//{分类}/{工具}/桌面专属工具
手机302 → /m/302 → /m/#/t/{分类}/{工具}留在电脑版
平板200 电脑版200 电脑版200 电脑版
桌面200 电脑版200 电脑版200 电脑版
搜索引擎爬虫200 电脑版200 电脑版200 电脑版

关键判断:爬虫不能跳。如果 Googlebot 用手机 UA 抓取,被 302 到 /m/,而 /m/ 又整段 noindex,那电脑版的工具页就永远进不了索引。

第一个 map:是不是手机 ​

nginx
map $http_user_agent $is_mobile_ua {
    default 0;
    "~*(googlebot|bingbot|baiduspider|yisouspider|sogou web spider|360spider|bytespider|petalbot)" 0;
    "~*(ipad|tablet|kindle|silk|playbook)" 0;
    "~*android(?!.*mobile)" 0;
    "~*(mobile|iphone|ipod|windows phone|blackberry|opera mini|harmonyos)" 1;
}

几个细节:

  • 爬虫规则必须在手机规则之前匹配。nginx 的 map 是按顺序的正则,~* 不区分大小写,第一条命中就停。
  • Android 平板 UA 里通常没有 Mobile,所以用负向断言 android(?!.*mobile) 归为 0。
  • iPad 单独列出,平板屏幕够大,看电脑版体验更好。
  • 微信内置浏览器 UA 含 Mobile,会命中手机规则,符合预期。

手机用户有时就是想看电脑版(比如需要大表格)。给一个开关:?view=pc。

nginx
location = / {
    if ($arg_view = pc) { rewrite ^ /__view-pc last; }
    if ($arg_view = mobile) { rewrite ^ /__view-mobile last; }
    if ($is_mobile_ua) { set $to_mobile 1; }
    if ($cookie_view_pref = pc) { set $to_mobile 0; }
    if ($to_mobile) { return 302 /m/; }
    try_files /index.html =404;
}

这里有个 nginx 的经典坑:add_header 在 if 块里不生效。不能在 if ($arg_view = pc) 里直接发 Set-Cookie,只能内部 rewrite 到一个真实 location:

nginx
location = /__view-pc {
    add_header Set-Cookie "view_pref=pc; Path=/; Max-Age=2592000; SameSite=Lax" always;
    add_header Cache-Control "no-cache" always;
    rewrite ^ /index.html break;
}

rewrite ... break 让 URL 对用户仍然是 /?view=pc,但内容来自 index.html。cookie 有效期 30 天,之后工具页也不再跳转。

反向操作 ?view=mobile 清掉 cookie 并回到 /m/。

工具页:保持路径对应 ​

用户从搜索结果点进 /text/word-counter/,手机上应该直接打开同一个工具:

nginx
location ~ ^/(?<tool_cat>text|image|calc|dev|study)/(?<tool_slug>[a-z0-9-]+)/?$ {
    if ($is_mobile_ua) { set $to_tool "/m/#/t/$tool_cat/$tool_slug"; }
    if ($is_desktop_only_page) { set $to_tool ""; }
    if ($arg_view = pc) { set $to_tool ""; }
    if ($cookie_view_pref = pc) { set $to_tool ""; }
    if ($arg_view = mobile) { set $to_tool "/m/#/t/$tool_cat/$tool_slug"; }
    if ($to_tool) { return 302 $to_tool; }
    try_files $uri $uri/ =404;
}

两个覆盖条件:

  • $is_desktop_only_page:少数工具(PDF 多页预览、代码截图)在手机上没法用,跳过去只会看到空页,所以留在电脑版;
  • view_pref=pc:用户已经明确选了电脑版,就不要再跳。

变量覆盖的顺序很关键,后面的 set 会覆盖前面的。把「留在电脑版」的条件放在手机判断之后,才能把 $to_tool 清空。

桌面专属工具的 map ​

nginx
map $uri $is_desktop_only_page {
    default 0;
    "~*^/dev/pdf-merge/(.*)" 1;
    "~*^/dev/code-screenshot/(.*)" 1;
}

这是一个可扩展的名单:以后新增必须在桌面用的工具,加一行正则即可,不用改 location 逻辑。

手机版整段 noindex ​

nginx
location /m/ {
    add_header Cache-Control "no-cache" always;
    add_header X-Robots-Tag "noindex" always;
    try_files $uri $uri/ /m/index.html;
}

手机版内容与电脑版重复,收录它只会稀释权重。noindex 也保证了 302 过去的内容不会被当成「另一个页面」。

怎么验证 ​

bash
# 模拟 iPhone
curl -A "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) Mobile/15E148" \
  -I https://emphant.com/text/word-counter/
# 期望 302 + Location: /m/#/t/text/word-counter

# 模拟 Googlebot
curl -A "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" \
  -I https://emphant.com/text/word-counter/
# 期望 200

# 模拟 iPad
curl -A "Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) Mobile/15E148" \
  -I https://emphant.com/
# 期望 200

# 强制电脑版
curl -A "iPhone" -I "https://emphant.com/?view=pc"
# 期望 200 + Set-Cookie: view_pref=pc

如果重来 ​

这套方案能工作,但复杂度不低:三个 map、两个内部 location、五条覆盖判断。更简单的替代是不做 UA 跳转,让首页和工具页都返回电脑版,在页面上用一个明显的按钮「用手机版打开」。

  • 优点:没有 302、没有 cookie、爬虫完全无感;
  • 缺点:手机用户要多点一次,首屏体验差。

选择 UA 分流,是因为这个站点的手机访问占比不低,而工具页的「点了就能用」比「多一次选择」重要。代价是每次改 nginx 都要把这套规则重新理解一遍——所以它被写进了项目文档。

记录方案,而不是记录结论。