制作中のサイトは外部に開放していないポートを使ってバーチャルホストを作り、SSHポートフォーワードで動作確認。運用開始時にはリバースプロキシを使って公開するという方法を使っています。
公開後も外部からアクセスしてほしくないコンテンツがある場合に、リバースプロキシにアクセス制限をかける方法を試してみました。
- 参考にしたサイト:
- mod_proxy - Apache HTTP Server Version 2.4のドキュメント(英語)
- https://www.ubuntu-perl-web.com/blog/20200306174004.html
リバースプロキシのアクセス制御
<Proxy>ディレクティブを使ってProxyPass側にアクセス制限をかけることができます。
+---- Frontend ----+ Request | | Client ---------|--> [ProxyPass] ----|------> Backend Deny | | | <--------|---------+ | Response | | <--------| [ProxyPassReverse] |<---- +--------------------+
設定例
/etc/httpd/conf/httpd.confを編集して、mod_proxyモジュール、mod_proxy_httpモジュールを有効にします。https通信を行う場合は、mod_rewriteモジュール、mod_sslモジュール、mod_headersモジュールも有効にします。
プロキシを行うバーチャルホストの設定ファイルを作成
例:/etc/httpd/conf/vhosts/frontend.conf
<VirtualHost "*:80"> ServerName test.example.com ServerAdmin webmaster@test.example.com DocumentRoot "/srv/http/sites/frontend" ErrorLog "/var/log/httpd/frontend-errot_log" CustomLog "/var/log/httpd/frontend-access_log" common <Proxy "/want-to-deny"> Require ip 127.0.0.1 127.0.1.1 </Proxy> ProxyPreserveHost On ProxyPass "/" "http://localhost:8080/" ProxyPassReverse "/" "http://localhost:8080/" </VirtualHost>
転送先(http://localhost:8080/)の末尾の/を忘れないように!
プロキシされる(バックエンド)バーチャルホストの設定ファイルを作成
例:/etc/httpd/conf/vhosts/backend.conf
Listen 8080 <VirtualHost "*:8080"> ServerName test.example.com ServerAdmin webmaster@test.example.com DocumentRoot "/srv/http/sites/backend" ErrorLog "/var/log/httpd/backend-error_log" CustomLog "/var/log/httpd/backend-access_log" common <Directory "/srv/http/sites/backend"> AllowOverride all Require all granted </Directory> </Directory>
/etc/httpd/conf/httpd.confを編集して、/etc/httpd/conf/vhosts/frontend.confと/etc/httpd/conf/vhosts/backend.confをインクルードするようにします。
# Virtual hosts #Include conf/extra/httpd-vhosts.conf Include conf/vhosts/frontend.conf Include conf/vhosts/backend.conf
コメント