Image

ナレッジベース → nginxサーバーのフラッディング(頻繁な定期的なページクロール)からの保護

[仮想サーバー]
公開日: 10.09.2025

どのサーバーも遅かれ早かれフラッディング(flooding)に晒され、それがサービスへの負荷増加とリソース使用量の増大を引き起こします。インターネット上の多数のボットは、脆弱性の有無やデータ収集のためにウェブサイトをスキャンしています。

このようなボットは悪意のある振る舞いをするため、サーバーリソースを浪費しないようブロックすべきです。

本記事では、フラッディングに対処する特定の方法の一つについて考察します。DDoS攻撃に対処するためには、別のサービスや方法が使用されます。

そのようなリクエストの例:

100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /aa.php HTTP/1.1" 404 27 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /file.php HTTP/1.1" 301 162 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /file.php HTTP/1.1" 404 27 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /wp-file.php HTTP/1.1" 301 162 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /wp-file.php HTTP/1.1" 404 27 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /k.php HTTP/1.1" 301 162 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /k.php HTTP/1.1" 404 27 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /al.php HTTP/1.1" 301 162 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /al.php HTTP/1.1" 404 27 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:57:59 +0000] "GET /wp- HTTP/1.1" 301 162 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:58:00 +0000] "GET /wp- HTTP/1.1" 404 48273 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:58:00 +0000] "GET /admin.php HTTP/1.1" 301 162 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:58:00 +0000] "GET /admin.php HTTP/1.1" 404 27 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:58:00 +0000] "GET /file2.php HTTP/1.1" 301 162 "-" "-"
100.xx.188.xx - - [10/Sep/2025:00:58:00 +0000] "GET /file2.php HTTP/1.1" 404 27 "-" "-"

ログからわかるように、このボットは1秒間に約9回ものリクエストをサーバーに行っています。特に、異なるIPアドレスを持つこのようなボットが複数いる場合、仮想サーバーにかなりの負荷がかかる可能性があります。

私たちの課題は、静的ファイルを除外し、非常に多くの定期的なリクエストを行うIPアドレスをブロックすることです。負荷を生み出すのは動的なリクエストであるため、動的なリクエストのみをカウントします。

1. Fail2banのインストール

apt install fail2ban

2. 設定

nano /etc/fail2ban/jail.local
[DEFAULT]
# 主要設定
ignoreip = 127.0.0.1/8 ::1
bantime = 3600
findtime = 600
maxretry = 200

# メール設定
destemail = corp@domain.tld
sender = fail2ban@domain.tld
sendername = Fail2Ban
mta = sendmail

# デフォルトのアクション (メール通知付き)
action = %(action_mwl)s

[nginx-flood]
enabled = true
port = http,https
filter = nginx-flood
logpath = /var/log/nginx/*access*.log
maxretry = 200
findtime = 60
bantime = 3600
# actionはDEFAULTから継承されるため、重複させる必要はありません

2.1 フィルター本体

nano /etc/fail2ban/filter.d/nginx-flood.conf
[Definition]
# 静的ファイルをカウントから除外
failregex = ^<HOST> -.*"(GET|POST|HEAD) (?!.*\.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|eot|svg|pdf|txt|xml)(\?.*)?$).* HTTP/\d\.\d".*
ignoreregex = 
datepattern = ^[^\[]*\[({DATE})\s+({TIME})\s+[^\]]*\]

2.2 修正

メールアドレスを自分のものに変更するのを忘れないでください:

destemail = corp@domain.tld
sender = fail2ban@domain.tld

2.3 トリガー閾値

必要に応じて、誤検知を避けるためにパラメータを調整してください:

maxretry = 200
findtime = 60
bantime = 3600

3. サービスの起動

systemctl restart fail2ban
systemctl enable fail2ban

4. 表示と管理

4.1 ブロックされたIPの表示

iptablesの場合

iptables -L -n

fail2banの場合

fail2ban-client status nginx-flood

4.2 テスト

テスト用のIPを一時的にBANリストに追加

fail2ban-client set nginx-flood banip 1.2.3.4

ブロックされているか、およびメールが届いたかを確認

fail2ban-client set nginx-flood unbanip 1.2.3.4




No Comments Yet