使用 Nginx 过滤网络爬虫的方法和配置

8 min read

使用 Nginx 过滤网络爬虫的方法和配置 的 ngx_http_rewrite_module 模块和 ngx_http_user_agent_module 模块进行配置。

以下是一些可用的方法和配置:

  1. 使用 ngx_http_user_agent_module 模块过滤爬虫
  • 在 Nginx 配置文件中,添加以下内容:
http {
    # ...
    map $http_user_agent $is_spider {
        default 0;
        ~bot 1;
        ~spider 1;
        ~crawler 1;
        ~search 1;
        ~detective 1;
        ~lighthouse 1;
        # 添加其他爬虫的正则表达式
    }

    # 拒绝所有爬虫
    if ($is_spider) {
        return 403;
    }
}
  • 上面的配置定义了一个变量 $is_spider,该变量的值为 0 或 1,根据 $http_user_agent 中是否包含爬虫相关的字符串来决定。
  • map 块中,添加了一些常见的爬虫的正则表达式,例如以 "bot"、"spider"、"crawler"、"search" 等开头的字符串。
  • 最后,使用 if 语句检查 $is_spider 的值,如果为 1,则返回 403 错误码,否则允许请求继续处理。
  1. 使用 ngx_http_rewrite_module 模块重定向爬虫
  • 在 Nginx 配置文件中,添加以下内容:
http {
    # ...
    if ($http_user_agent ~* (bot|spider|crawler|search|detective|lighthouse)) {
        return 301 https://www.example.com/bot.html;
    }
}
  • 上面的配置使用 if 语句匹配 $http_user_agent 中是否包含爬虫相关的字符串,如果匹配成功,则重定向到 https://www.example.com/bot.html 页面。
  • 注意,使用 if 语句需要谨慎,因为它可能会引起性能问题和安全隐患,建议使用 mapif 的组合方式。

以上是两种常见的过滤网络爬虫的方法和配置,但是需要注意的是,这些方法并不能完全防止所有的爬虫,因为一些恶意爬虫可能会伪装为普通浏览器或其他用户代理。因此,建议使用更多的安全措施来保护您的网站。