Apache2.4系でのアクセス制限方法をメモ。
2.2系のConfigをそのまま使ってサーバーを移行しようとすると
ここの記述で起動しない事が多いです。
特定のIPブロックのみを許可する場合
基本拒否として、特定のIPのみ許可する場合は簡単です。
#Apache 2.2(特定のIPのみ許可) <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Order Deny,Allow Deny from all Allow from 192.168.0.0/24 </Directory>
#Apache 2.4(特定のIPのみ許可) <Directory “/var/www/html”> Options Indexes FollowSymLinks AllowOverride None Require all denied Require ip 192.168.0.0/24 </Directory>
「Allow from」が「Require ip」に変わったのか、ぐらいに見えますよねぇ。
特定のIPブロックのみを拒否する場合
許可の時と同じパターンであれば、以下のようになるはずですが・・・
#Apache2.2(特定のIPブロックのみを拒否) <Directory “/var/www/html”> Options Indexes FollowSymLinks AllowOverride None Order Allow,Deny Allow from all Deny from 192.168.0.0/24 </Directory>
#Apache2.4(特定のIPブロックのみを拒否)※NGパターン <Directory “/var/www/html”> Options Indexes FollowSymLinks AllowOverride None Require all granted Require not ip 192.168.0.0/24 </Directory>
このConfigで再起動すると、下記のようなログを吐いてApacheが上がりません。
negative Require directive has no effect in <RequireAny> directive
正解はこちらです。
#Apache2.4(特定のIPブロックのみを拒否) <Directory “/var/www/html”> Options Indexes FollowSymLinks AllowOverride None <RequireAll> Require all granted <RequireNone> Require ip 192.168.0.0/24 </RequireNone> </RequireAll> </Directory>
Apache2.4には<RequireAll>、<RequireAny>、<RequireNone>という3つのタグ(ディレクティブ)指定があります。
<RequireAll>..</RequireAll> | 指定した接続元を許可、あるいは拒否(どちらでも指定可能) |
<RequireAny>..</RequireAny> | 指定した接続元を許可 |
<RequireNone>..</RequireNone> | 指定した接続元を拒否 |
基本は全体を<RequireAll>..</RequireAll>で囲んで、その中に許可であれば<RequireAny>..</RequireAny>、拒否であれば<RequireNone>..</RequireNone>でIPブロックを指定します。
上記のタグをすべて省略して条件のみ記述した場合、暗黙の<RequireAny>..</RequireAny>が適用されるため、先ほどのNGパターンは下記記述と同意になります。
<Directory “/var/www/html”> Options Indexes FollowSymLinks AllowOverride None <RequireAny> Require all granted Require not ip 192.168.0.0/24 </RequireAny> </Directory>
これでは「指定した接続元(192.168.0.0/24)の許可(RequireAny)を拒否(not)」
してしまうことになるので、この”条件の二重指定”がまずいらしい。
もし、許可の時となるべく同じパターンで記述するとすれば、
どちらの指定も可能な<RequireAll>を使用して
<Directory “/var/www/html”> Options Indexes FollowSymLinks AllowOverride None <RequireAll> Require all granted Require not ip 192.168.0.0/24 </RequireAll> </Directory>
とすればOKです。
ちなみに「Require all granted」はおまじないのごとく、毎回記述が必要みたいです。
わかりにくかったらすいません(^-^;
コメント