sheep 发表于 2026-7-28 20:23

《Nginx跳转代码》根据爬虫、设备、来源判断,实现nginx屏蔽/跳转与展示控制

Nginx 多场景跳转模板,内置完整蜘蛛 UA 库,支持三种常用访问控制,配置区域配置对应模块即可,自带防循环重定向,可跳转域名或目录。

《爬虫/蜘蛛/设备标识全量识别清单》
http://www.sheepyc.com/thread-703-1-1.html
本文中的爬虫/蜘蛛/设备标识更新/补充,均会在上述文章内同步,复制代码后注意核对
# ==============================================
# Nginx 跳转控制通用模板
# 功能:爬虫识别 | 设备识别 | 来源识别 | 防循环重定向
# 支持:域名前缀跳转 / 完整域名跳转 / 指定目录跳转
# ==============================================

# ====================== 【配置区】 按需修改即可 ======================
# 1. 通用配置
set $redirect_code 302;          # 重定向状态码:301=永久重定向302=临时重定向

# 2. 爬虫配置
set $spider_redirect "";         # 爬虫跳转目标,空=放行不跳转;示例:/spider.html 或 http://xxx.com

# 3. 设备跳转配置(留空=该设备不执行跳转)
set $pc_redirect_target "";      # PC端跳转目标;示例:www1.$base_domain 或 /pc/
set $tablet_redirect_target "";# 平板端(iPad等)跳转目标;示例:www1.$base_domain 或 /pad/
set $mobile_redirect_target "";# 手机端跳转目标;示例:m.$base_domain 或 /mobile/

# 4. 来源跳转配置
set $non_search_redirect "";   # 非搜索引擎&非AI来源跳转目标,空=不启用;示例:/error.html
set $ai_referer_redirect "";   # AI平台来源单独跳转目标,空=不单独处理

# 5. 防循环重定向规则(匹配该规则则取消跳转,避免死循环)
# 域名前缀跳转填:^www1\. ;目录跳转填:^/mobile/
set $loop_guard_pattern "";
# ====================== 【配置区结束】 ======================


# ---------------------- 1. 基础域名提取 ----------------------
# 自动移除 www. / www1. / m. / wap. 前缀,提取裸域名
set $base_domain "$host";
if ($host ~* "^(www|www1|m|wap)\.(.+)$") {
    set $base_domain $2;
}


# ---------------------- 2. 设备类型精准识别 ----------------------
# 初始化标记
set $is_pc 0;
set $is_tablet 0;
set $is_mobile 0;

# 优先级1:识别手机端
if ($http_user_agent ~* "(mobile|iphone|ipod|android.*mobile|windows\s*phone|blackberry|opera\s*mini|ucweb|meego|silk|kindle|playbook|nintendo|playstation|wii)") {
    set $is_mobile 1;
}

# 优先级2:识别平板端(iPad/安卓平板,避免误判为PC)
if ($is_mobile = 0) {
    if ($http_user_agent ~* "(ipad|android.*tablet|tablet)") {
      set $is_tablet 1;
    }
}

# 优先级3:剩余识别为PC桌面端
if ($is_mobile = 0) {
    if ($is_tablet = 0) {
      if ($http_user_agent ~* "(windows\s*nt|macintosh|mac\s*os\s*x|x11|cros|linux\s*(x86|amd64|i686))") {
            set $is_pc 1;
      }
    }
}


# ---------------------- 3. 爬虫/蜘蛛全量识别 ----------------------
# 覆盖范围:通用泛匹配 + 全球主流AI蜘蛛 + 中国主流AI蜘蛛 + 全球搜索引擎 + 中国搜索引擎 + 其他平台蜘蛛
set $is_spider 0;
if ($http_user_agent ~* "(bot|spider|crawl|crawler|index|archive|gptbot|oai-searchbot|chatgpt-user|claudebot|claude-searchbot|claude-user|xai-searchbot|xai-bot|xai-grok|meta-externalagent|meta-externalfetcher|perplexitybot|perplexity-user|huggingfacebot|amazonbot|coherebot|inflectionbot|google-extended|ccbot|doubaobot|yuanbaobot|hunyuanbot|qwenbot|tongyibot|wenxinbot|moonshotbot|deepseekbot|kimibot|zhipubot|youbot|google|bing|yahoo|yandex|duckduckbot|slurp|applebot|baidu|sogou|bytespider|shenma|youdao|360spider|soso|toutiao|semrush|ahrefs|petalbot|exabot|facebot|facebook|twitter|linkedin|telegram|ia_archiver)") {
    set $is_spider 1;
}


# ---------------------- 4. 访问来源(Referrer)识别 ----------------------
set $is_search_referer 0;
set $is_ai_referer 0;

# 识别搜索引擎来源(支持多级域名,如google.co.jp、baidu.com.cn)
if ($http_referer ~* "\.(google|bing|yahoo|yandex|duckduckgo|apple|baidu|sogou|shenma|sm|youdao|360|soso|toutiao)(\.+){0,2}\/") {
    set $is_search_referer 1;
}

# 识别AI平台来源
if ($http_referer ~* "\.(openai|anthropic|claude|xai|perplexity|huggingface|cohere|inflection|meta|gemini|doubao|wenxin|tongyi|qianwen|hunyuan|moonshot|deepseek)(\.+){0,2}\/") {
    set $is_ai_referer 1;
}


# ---------------------- 5. 跳转目标计算(优先级从高到低) ----------------------
set $final_target "";

# 优先级1:爬虫专属逻辑
if ($is_spider = 1) {
    set $final_target $spider_redirect;
}

# 优先级2:AI平台来源单独跳转(非爬虫时生效)
if ($is_spider = 0) {
    if ($is_ai_referer = 1) {
      set $final_target $ai_referer_redirect;
    }
}

# 优先级3:非搜索&非AI来源跳转(非爬虫、未触发AI跳转时生效)
if ($is_spider = 0) {
    if ($is_search_referer = 0) {
      if ($is_ai_referer = 0) {
            set $final_target $non_search_redirect;
      }
    }
}

# 优先级4:设备维度跳转(非爬虫、未触发来源跳转时生效)
if ($is_spider = 0) {
    if ($final_target = "") {
      if ($is_pc = 1) {
            set $final_target $pc_redirect_target;
      }
      if ($is_tablet = 1) {
            set $final_target $tablet_redirect_target;
      }
      if ($is_mobile = 1) {
            set $final_target $mobile_redirect_target;
      }
    }
}


# ---------------------- 6. 防循环重定向检测 ----------------------
if ($loop_guard_pattern != "") {
    # 域名前缀场景防护
    if ($host ~* $loop_guard_pattern) {
      set $final_target "";
    }
    # 目录跳转场景防护
    if ($request_uri ~* $loop_guard_pattern) {
      set $final_target "";
    }
}


# ---------------------- 7. 执行跳转 ----------------------
if ($final_target != "") {
    # 目标包含http/https → 直接跳转到完整域名
    if ($final_target ~* "^https?://") {
      return $redirect_code $final_target;
    }
    # 目标以/开头 → 站内目录跳转
    if ($final_target ~* "^/") {
      return $redirect_code $final_target;
    }
    # 其他情况 → 视为域名前缀,拼接完整域名+原请求路径
    return $redirect_code http://$final_target$request_uri;
}











页: [1]
查看完整版本: 《Nginx跳转代码》根据爬虫、设备、来源判断,实现nginx屏蔽/跳转与展示控制