【Apache備忘録⑦】アクセス制限の設定
Webサーバーで情報を公開する際に、特定のユーザーのみで利用する場合、アクセス制限が必要となる。
Apacheの設定でアクセス制限を実装できる。
※ AWSで運用する場合は、セキュリティグループやACLなどで制限した方が管理しやすいのでAWS側で制御した方が良い。
特定のアクセス元に対してアクセスを禁止する
特定のグローバルIPアドレス(パブリックIPアドレス)からのアクセスを制限することができる。
ホワイトリストでのアクセス制限
設定ファイルにアクセス制限を追加する。
$ sudo vi /etc/httpd/conf/httpd.conf
特定のアクセス元からのアクセスのみを許可する場合は 131〜157行目の設定を変更する。
「Require all granted」をコメントアウトして、全てのアクセス許可を無効化する。
「Require host 対象とするホスト名」を追加する。
以下の設定では、「www.kento.co.jp」からのアクセスのみ許可する設定となっている。
<Directory "/var/www/html"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # # ↓ コメントアウトする #Require all granted # ↓ 追加する Require host www.kento.co.jp </Directory>
ブラックリストでのアクセス制御
設定ファイルにアクセス制限を追加する。
$ sudo vi /etc/httpd/conf/httpd.conf
特定のアクセス元からのアクセスのみを禁止する場合は 131〜157行目の設定を変更する。
「Require all granted」をコメントアウトして、全てのアクセス許可を無効化する。
「RequireAll」、「Require not ip IPアドレス」を追加する。
以下の設定では、「192.168.1.20」からのアクセスのみ禁止する設定となっている。
<Directory "/var/www/html"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # # ↓ コメントアウトする #Require all granted # ↓ 追加する <RequireAll> Require all granted Require not ip 192.168.1.20 </RequireAll> </Directory>
Require ディレクティブ設定値一覧
Apache2.4では、以下の設定値をRequireディレクティブに設定することでアクセス制御ができる。
種別 | 詳細 | 使用例 |
---|---|---|
all | 全てのアクセスを許可または禁止 | 許可:Require all granted 禁止:Require all denied |
ip | IPアドレスからのアクセスを許可または禁止 IPアドレス/CIDRも指定できる |
許可:Require ip 192.168.1.20 禁止:Require not ip 192.168.1.20 許可(CIDR付き): Require ip 192.168.1.0/255.255.2550 |
host | ホスト名で許可または禁止 | 許可:Require host hoge.co.jp 禁止:Require not host hoge.co.jp |
local | ローカルホストからのアクセスの許可または禁止 | 許可:Require local 禁止:Require not local |