1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Reverse Proxyを使用したコンテナへのアクセスにIP制限を設定(Apache)

Last updated at Posted at 2021-07-21

■条件

  • Reverse Proxyを使用し、各種コンテナに振り分けている
  • IP制限は各コンテナで行う
  • WEBサーバはApache(2.4)を使用している
  • 各コンテナのIPアドレスは「172.16.0.xxx」と仮定
  • ホスト側のIPアドレスは「192.168.10.xxx」と仮定

Reverse Proxyを気にせずIP制限をした場合

<VirtualHost *:80>
    ServerName www.local.com
    ServerAlias www.local.com *.local.com
    DocumentRoot /var/html

    <Directory "/var/html">
        Require all denied
        Require ip 192.168.10.100
        Require ip 192.168.10.101
    </Directory>
</VirtualHost>

上記の設定は「192.168.10.100」と「192.168.10.101」からのアクセスのみを許可したい場合の設定です。
ですが、「192.168.10.100」からアクセスを行ってもエラーとなります。

Reverse Proxyを使用している場合、IPアドレスが「172.16.0.xxx」となる為、「Require ip」と条件が一致しません。
元のIPアドレスはX-Forwarded-Forなどに設定されています。

では、どのように設定すればX-Forwarded-Forで制限を指定することが出来るのか?

  mod_remoteip

で解決することができます。

■mod_remoteipを使用してIP制限をした場合

<VirtualHost *:80>
    ServerName www.local.com
    ServerAlias www.local.com *.local.com
    DocumentRoot /var/html

    RemoteIPHeader X-Forwarded-For
    <Directory "/var/html">
        Require all denied
        Require ip 192.168.10.100
        Require ip 192.168.10.101
    </Directory>
</VirtualHost>

RemoteIPHeader を記載します。
Directoryの中には記載できません

上記のようにRemoteIPHeaderを設定した後、Require ipで比較されるIPアドレスはX-Forwarded-Forになります。

■Debianで「mod_remoteip」を有効にする

a2enmod remoteip

■Alpineで「mod_remoteip」を有効にする

sed -i -e 's/#\(LoadModule remoteip_module\)/\1/' /usr/local/apache2/conf/httpd.conf

■関連

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?