Linuxめも

通常のHTTPアクセスとは思えない悪意のあるものは拒否しましょう。
接続元がイケてない場合はわかりやすいUSER_AGENTである場合があります。

.htaccess ファイルに以下のように記述することで実現することができます。

USER_AGENT が Wget ではじまるものを拒否する場合。

SetEnvIf User-Agent "^Wget" DENY_AGENT
order allow,deny
allow from all
deny from env=DENY_AGENT

VirtualHostで共通の設定を使用したい場合

deny-list.conf ファイルを作成する

  • User-Agentに特定の文字列を含む場合拒否
  • 特定のIPアドレス 192.168.0.1 を拒否
  • 特定のIPアドレス 192.168.1.0 から 192.168.1.255 を拒否
SetEnvIf User-Agent "SemrushBot" deny_bot
SetEnvIf User-Agent "BLEXBot" deny_bot
SetEnvIf User-Agent "DomainCrawler" deny_bot

<RequireAll>
    Require all granted
    Require not env deny_bot
    Require not ip 192.168.0.1
    Require not ip 192.168.1.0/24
</RequireAll>

VirtualHostの設定から deny-list.conf を Include する

<VirtualHost *:443>
    ServerName sv1.example.com:443
    DocumentRoot "/home/sv1/public_html"

    SSLEngine on
    SSLHonorCipherOrder on
    Header set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    SSLProtocol -All +TLSv1 +TLSv1.1 +TLSv1.2
    SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5

    SSLCertificateFile /etc/letsencrypt/live/sv1.example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/sv1.example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/sv1.example.com/chain.pem

    <Directory "/home/sv1/public_html">
        Options FollowSymLinks
        AllowOverride ALL
#        Require all granted
        Include /usr/local/apache2/conf/extra/deny-list.conf
    </Directory>

    ErrorLog "logs/sv1_error_log"
    CustomLog "logs/sv1_access_log" combined
</VirtualHost>

設定が有効になっているか確認

curl -A "test" -I https://sv1.example.com/
HTTP/1.1 200 OK
curl -A "SemrushBot" -I https://sv1.example.com/
HTTP/1.1 403 Forbidden

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2025-01-22 (水) 19:26:55