ヘルプ:リバースプロキシの通信内容暗号化にCertbotを導入(CentOS8)
メインページ > Help:目次 > Help:自宅サーバー構築(CentOS8) > リバースプロキシの通信内容暗号化にCertbotを導入(CentOS8)
- 動作確認しました。
- 偶にしか行わないので、忘れないように書いておく。
- Webサーバー間通信内容暗号化 (自己署名)(CentOS8)で、バックエンド、Webサーバーを構築してください。
Let's Encrypt から 発行料 無料の SSL/TLS サーバー証明書を取得します。
Let's Encrypt は Linux Foundation の協業プロジェクトで、Web 全体の安全性を改善することをミッションに掲げているとのことです。
発行料 無料 とはいえ、あやしいものではありません。
Let's Encrypt の詳細は公式サイトを参照ください。
Let's Encrypt では 一般的な ドメイン認証 (DV) の証明書を無料で発行しています。
無料ですが Let's Encrypt の中間証明書は、大手認証局 (CA) のルート証明書によってクロス署名されているため、多くの主要ブラウザ等々で信頼済みとして扱われます。
なお、一回の作業で得られる証明書の有効期限は 90日です。よって、90日以内に更新作業を再度実施する必要があります。
バーチャルホスト設定
バーチャルホスト用ドキュメントルートディレクトリ作成
[root@host3 ~]# mkdir /var/www/html/sudachi.xyz [root@host3 ~]# mkdir /var/www/html/sudachi.xyz/awstatsreport
ディレクトリの所有者変更
[root@host3 ~]# chown sysop. -R /var/www/html/sudach.xyz
バーチャルホスト設定ファイル作成
ここで, ProxyPass*の設定で最後に”/”をつけておかないと、そこからのリンクのパスがおかしくなり移動できないので注意。
[root@host3 ~]# cat /etc/httpd/conf.d/virtualhost-sudachi.xyz.conf
<IfModule mod_proxy.c>
#フォワードプロキシ機能の無効化
ProxyRequests Off
#リバースプロキシなので制限をしない
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
#プロキシ経由であることをヘッダに記述しない
ProxyVia Off
# プロキシリクエストに、受け付けた Host HTTP ヘッダを使う
ProxyPreserveHost On
<VirtualHost *:80>
DocumentRoot "/var/www/html/sudachi.xyz"
ServerName sudachi.xyz:80
ErrorLog logs/sudachi.xyz-error_log
CustomLog logs/sudachi.xyz-access_log combined env=!no_log
# awstats awstatsicons awstatsreport .well-known へのリクエストはプロキシしない
ProxyPass /awstats/ !
ProxyPass /awstatsicons/ !
ProxyPass /awstatsreport/ !
ProxyPass /.well-known/ !
ProxyPass / http://192.168.1.2:80/
ProxyPassReverse / http://sudachi.xyz:80/
ProxyPassReverseCookieDomain 192.168.1.2 sudachi.xyz
ProxyPassReverseCookiePath / /
</VirtualHost>
</IfModule>
「httpd.conf」の文法チェック
[root@host3 ~]# apachectl configtest Syntax OK
Webサーバー再起動
[root@host3 ~]# systemctl restart httpd
動作確認
[root@host3 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/httpd.service.d
└─php-fpm.conf
Active: active (running) since Tue 2020-04-14 21:10:24 JST; 2min 32s ago
Docs: man:httpd.service(8)
Process: 16441 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 21858 (httpd)
Status: "Total requests: 49; Idle/Busy workers 99/1;Requests/sec: 0.329; Bytes served/sec: 5.8KB/sec"
Tasks: 278 (limit: 26213)
Memory: 60.9M
CGroup: /system.slice/httpd.service
├─21858 /usr/sbin/httpd -DFOREGROUND
├─21861 /usr/sbin/httpd -DFOREGROUND
├─21862 /usr/sbin/httpd -DFOREGROUND
├─21863 /usr/sbin/httpd -DFOREGROUND
├─21864 /usr/sbin/httpd -DFOREGROUND
└─22077 /usr/sbin/httpd -DFOREGROUND
4月 14 21:10:24 host3.sudachi.jp systemd[1]: Starting The Apache HTTP Server...
4月 14 21:10:24 host3.sudachi.jp systemd[1]: Started The Apache HTTP Server.
4月 14 21:10:24 host3.sudachi.jp httpd[21858]: Server configured, listening on: port 443, port 80
ドメイン名に対するサーバー証明書取得
Certbotではサーバー名の認証をWeb経由で行うため、サーバー証明書に指定するサーバー名で外部からWebアクセスできるようにしておく必要がある。
ドメイン名に対する SSL/TLS サーバ証明書を取得する場合。
Certbotクライアントインストール
証明書を取得するためのツール Certbot クライアントをインストールします。
[root@host3 ~]# dnf --enablerepo=epel -y install certbot
テスト用のサーバ証明書取得
Let's Encrypt 認証局では、ドメイン名あたりの SSL/TLS サーバ証明書の発行枚数について、制限を設けています。そのため、試験段階においては --test-cert オプションを付けて、テスト用のサーバで使ってみることをお勧めします。
※テスト用のサーバで発行したテスト用の証明書は、ブラウザにおいてセキュリティ警告が表示され、信頼された証明書として扱われません。
[root@host3 ~]# certbot certonly --webroot -w /var/www/html/sudachi.xyz -d sudachi.xyz -m *****@sudachi.xyz --test-cert
サーバー証明書取得
[root@host3 ~]# certbot certonly --webroot -w /var/www/html/sudachi.xyz -d sudachi.xyz -m *****@sudachi.xyz --agree-tos ・ ・ ・ What would you like to do? ------------------------------------------------------------------------------- 1: Keep the existing certificate for now 2: Renew & replace the cert (limit ~5 per 7 days) ------------------------------------------------------------------------------- Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 ← 2を選択(証明書を更新し、交換する)
追加ホスト用リバースプロキシ設定
ファイル編集
[root@host3 ~]# vi /etc/httpd/conf.d/virtualhost-sudachi.xyz.conf
<IfModule mod_proxy.c>
#フォワードプロキシ機能の無効化
ProxyRequests Off
#リバースプロキシなので制限をしない。
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
#プロキシ経由であることをヘッダに記述しない
ProxyVia Off
# プロキシリクエストに、受け付けた Host HTTP ヘッダを使う
ProxyPreserveHost On
<VirtualHost *:80>
DocumentRoot "/var/www/html/sudachi.xyz"
ServerName sudachi.xyz:80
ErrorLog logs/sudachi.xyz-error_log
CustomLog logs/sudachi.xyz-access_log combined env=!no_log
Alias /awstatsreport /var/www/html/sudachi.xyz/awstatsreport
# APC INFO アクセス制限
<Location "/apc">
Require all denied
Require ip 127.0.0.1
Require ip 192.168.1.0/24
</Location>
# OpCache 状態確認アクセス制限
<Location "/opcache-status">
Require all denied
Require ip 127.0.0.1
Require ip 192.168.1.0/24
</Location>
# awstats awstatsicons awstatsreport .well-known へのリクエストはプロキシしない
ProxyPass /awstats/ !
ProxyPass /awstatsicons/ !
ProxyPass /awstatsreport/ !
ProxyPass /.well-known/ !
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/var/www/html/sudachi.xyz"
ServerName sudachi.xyz:443
ErrorLog logs/sudachi.xyz-error_log
CustomLog logs/sudachi.xyz-access_log combined env=!no_log
Alias /awstatsreport /var/www/html/sudachi.xyz/awstatsreport
# APC INFO アクセス制限
<Location "/apc">
Require all denied
Require ip 127.0.0.1
Require ip 192.168.1.0/24
</Location>
# OpCache 状態確認アクセス制限
<Location "/opcache-status">
Require all denied
Require ip 127.0.0.1
Require ip 192.168.1.0/24
</Location>
## SSL Engine Switch:
## Enable/Disable SSL for this virtual host.
SSLEngine on
SSLProxyEngine on
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
# リバースプロキシHTTPSオンリーにする
RequestHeader set X_FORWARDED_PROTO 'https'
# awstats awstatsicons awstatsreport .well-known へのリクエストはプロキシしない
ProxyPass /awstats/ !
ProxyPass /awstatsicons/ !
ProxyPass /awstatsreport/ !
ProxyPass /.well-known/ !
ProxyPass / https://192.168.1.2:443/
ProxyPassReverse / https://sudachi.xyz:443/
ProxyPassReverseCookieDomain 192.168.1.2 sudachi.xyz
ProxyPassReverseCookiePath / /
## SSL Protocol support:
# SSLv2、SSLv3を無効化する
SSLProtocol All -SSLv2 -SSLv3
## SSL Cipher Suite:
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
## Speed-optimized SSL Cipher configuration:
SSLHonorCipherOrder on
SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
## 公開鍵指定
SSLCertificateFile /etc/letsencrypt/live/sudachi.xyz/cert.pem
## 秘密鍵指定
SSLCertificateKeyFile /etc/letsencrypt/live/sudachi.xyz/privkey.pem
## 中間証明書指定
SSLCertificateChainFile /etc/letsencrypt/live/sudachi.xyz/chain.pem
## SSL Protocol Adjustments:
BrowserMatch "MSIE [2-5]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
Header always set Strict-Transport-Security "max-age=15768000"
</VirtualHost>
</IfModule>
「httpd.conf」の文法チェック
[root@host3 ~]# apachectl configtest Syntax OK
Webサーバー再起動
[root@host3 ~]# systemctl restart httpd
動作確認
[root@host3 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 土 2017-05-06 13:45:58 JST; 46s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 29305 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 29311 (httpd)
Status: "Total requests: 9; Current requests/sec: 0.111; Current traffic: 1.3KB/sec"
CGroup: /system.slice/httpd.service
├─29311 /usr/sbin/httpd -DFOREGROUND
├─29312 /usr/sbin/httpd -DFOREGROUND
├─29313 /usr/sbin/httpd -DFOREGROUND
├─29314 /usr/sbin/httpd -DFOREGROUND
├─29315 /usr/sbin/httpd -DFOREGROUND
├─29316 /usr/sbin/httpd -DFOREGROUND
├─29317 /usr/sbin/httpd -DFOREGROUND
├─29318 /usr/sbin/httpd -DFOREGROUND
├─29322 /usr/sbin/httpd -DFOREGROUND
├─29323 /usr/sbin/httpd -DFOREGROUND
└─29324 /usr/sbin/httpd -DFOREGROUND
5月 06 13:45:57 host3.sudachi.jp systemd[1]: Starting The Apache HTTP Server...
5月 06 13:45:58 host3.sudachi.jp systemd[1]: Started The Apache HTTP Server.
