1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nginx (geo) と Apache 2.4 (mod_authz_core) でクラウド発のスクレイピングボット (AS132203 / AS45102) を高速遮断し Googlebot を保護する方法

1
Posted at

本番環境の Next.js、WordPress、Laravel サーバーを Nginx または Apache 2.4 / LiteSpeed で運用している場合、以下のようなボットトラフィックの急増に直面することがあります。

  1. Google Analytics 4 (GA4) にシンガポール、香港、アッシュバーンなどのデータセンターから直帰率 100%・滞在時間 0秒 の不審なセッションが大量に記録される。
  2. Nginx / Apache の access.log に、数千個の異なる IPv4 アドレスからモバイル User-Agent を偽装したリクエストが継続的に到達する。

クラウドデータセンターの広大な CIDR ブロック(AS132203、AS45102、AS136907)全体に IP が分散しているため、従来の IP 単位のレート制限(limit_req_zone や mod_evasive)では防ぎきれません。本記事では、検索エンジンのクローラー(Googlebot、Bingbot、Applebot)を 100% 安全に許可しながら、エッジ層で 0.08 ミリ秒以内に遮断する設定手順を解説します。


1. Nginx: ngx_http_geo_module による基数木(Radix Tree)フィルタリング

if ($remote_addr ~* ...) による正規表現マッチングはリクエストごとに線形探索が発生するため、代わりに Nginx 標準の geo モジュールを使用して起動時にメモリ上の基数木(O(1) 検索)を構築します。

geo $blocked_cloud_cidr {
    default 0;
    43.128.0.0/10     1; # AS132203
    101.32.0.0/15     1; # AS132203
    47.74.0.0/15      1; # AS45102
    47.88.0.0/15      1; # AS45102
    8.208.0.0/12      1; # AS45102
    114.119.128.0/18  1; # AS136907
}

map $http_user_agent $safe_search_bot {
    default 0;
    ~*(Googlebot|Google-InspectionTool|AdsBot-Google|bingbot|Applebot|DuckDuckBot) 1;
}

2. Apache 2.4 (mod_authz_core + mod_setenvif) および .htaccess

Apache HTTP Server 2.4 環境では、非推奨の Deny from の代わりに mod_authz_core の Require not ip と mod_setenvif を組み合わせることで、SEO クローラーを確実に除外しつつ CIDR サブネットを高速に遮断できます。

<IfModule mod_setenvif.c>
    SetEnvIfNoCase User-Agent "(Googlebot|Google-InspectionTool|AdsBot-Google|bingbot|Applebot|DuckDuckBot|Slurp|YandexBot)" seo_safe_bot=1
    SetEnvIfNoCase User-Agent "(HeadlessChrome|PhantomJS|Puppeteer|Playwright|Bytespider|PetalBot)" bad_bot_ua=1
</IfModule>

<IfModule mod_authz_core.c>
    <Location "/">
        <RequireAny>
            Require env seo_safe_bot
            <RequireAll>
                Require all granted
                Require not env bad_bot_ua
                Require not ip 43.128.0.0/10 101.32.0.0/15 119.28.0.0/16 124.156.0.0/16
                Require not ip 43.119.0.0/16 47.74.0.0/15 47.82.0.0/16 47.88.0.0/15 8.208.0.0/12
                Require not ip 149.232.0.0/16 159.138.0.0/16 114.119.0.0/16 46.250.0.0/16
            </RequireAll>
        </RequireAny>
    </Location>
</IfModule>

共有ホスティング / .htaccess (mod_rewrite) の場合

httpd.conf の編集権限がない環境では、.htaccess で同様のホワイトリスト判定とサブネット遮断を実装できます。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} !(Googlebot|Google-InspectionTool|AdsBot-Google|bingbot|Applebot|DuckDuckBot) [NC]
    RewriteCond %{HTTP_USER_AGENT} (HeadlessChrome|PhantomJS|Puppeteer|Playwright|Bytespider|PetalBot) [NC,OR]
    RewriteCond %{REMOTE_ADDR} ^(43\.(1[2-8][0-9]|19[0-1])\.|101\.3[2-3]\.|47\.(7[4-5]|8[289])\.|114\.119\.)
    RewriteRule ^.*$ - [F,L]
</IfModule>

3. ヘッドレス Chromium (SwiftShader / llvmpipe) による GA4 汚染の防止

プロキシ経由で CDN を通過したヘッドレスブラウザは、WebGL レンダラー情報に Google SwiftShader や llvmpipe を露出します。gtag.js の初期化前に WebGL レンダラーを検証することで、GA4 への架空セッション送信を防ぐことができます。


参考リンク・設定生成ツール

Nginx / Apache 2.4 / .htaccess 用の CIDR ルール生成および access.log 解析ツールは Utilvo EdgeShield で公開しています。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?