LoginSignup
0

More than 1 year has passed since last update.

Apache 2.2系とApache2.4系での海外IPのアクセス制限方法 ( AH01797: client denied by server configurationとエラーが出る場合)

Posted at

実行環境

Apache/2.2.15 (Unix)
→ Apache/2.4.37 (centos)

概要

.htaccess ファイルで国外IP制限をしていたところ、サーバーのリプレイスで書き換えが発生しました。
以下は同じ内容をApacheのバージョンが 2.2系 から 2.4系 に書き換えた場合と、そうしなかった場合に起こりうるエラーの説明です。

Apache 2.2系

### 検索エンジンをまとめて許可
SetEnvIf User-Agent "Googlebot" allowbot
SetEnvIf User-Agent "msnbot" allowbot
SetEnvIf User-Agent "bingbot" allowbot
SetEnvIf User-Agent "Slurp" allowbot
## 一旦すべて拒否して
order deny,allow
deny from all
## 改めて、検索エンジンからのアクセスを許可
allow from env=allowbot

## SearchConsoleのプロパティ確認用の対策
allow from googlebot.com
allow from google.com
## 国外IPからのアクセスを拒否 
allow from 1.0.16.0/20
allow from 1.0.64.0/18
~

Apache 2.4系

## 検索エンジンをまとめて許可
SetEnvIf User-Agent "Googlebot" allowbot
SetEnvIf User-Agent "msnbot" allowbot
SetEnvIf User-Agent "bingbot" allowbot
SetEnvIf User-Agent "Slurp" allowbot

## 一旦すべて拒否

<RequireAny>
    Require all denied
    ### 検索エンジンからのアクセスを許可
    Require env allowbot
    ## SearchConsoleのプロパティ確認用の対策
    Require host googlebot.com
    Require host google.com

    ### 日本国内のIPを許可(国外IPからのアクセスを拒否)
    Require ip 1.0.16.0/20
    Require ip 1.0.64.0/18
    ~
</RequireAny>

書き換えた.htaccessファイルのデバグ方法

こちらのサイトを開き、検証したいURLを入力。適当な国を選び「Start Test」をクリックでテスト開始です。
結果ページで「Screenshot」か「View Test Log」をみて、無事403エラーとなっていれば国外IP制限できていると確認できます。
ちなみに、結果ページのURLは個別に発行されるため容易にシェアも可能、かなりお気に入りです。

書き換えをしてなかった場合

Apache 2.4系の環境にて、上記の書き換えを行わない場合は以下のサーバーエラーがログ出力されると思います。

Error    12.345.678.9012        AH01797: client denied by server configuration: /var/www/vhosts/qwerty.com/httpdocs/            Apache エラー

参考

検索エンジンbot一覧
https://www.casis-iss.org/ex1911/
検索エンジンのプロパティ
https://support.google.com/webmasters/answer/80553?hl=ja
日本国内IPの一覧
http://www.cgis.biz/tools/access/

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
0