Apache のバーチャルホストが上手く機能しなかった話
こんな理由で上手く動作しない事があるのかって事で思い知った話。
内向け用のサーバーとして「wsgi.magic-object.com」を開設した。
以下はバーチャルホストの設定
<VirtualHost *:80>
# General setup for the virtual host, inherited from global configuration
ServerName wsgi.magic-object.com
DocumentRoot "/var/www/wsgi"
<Proxy *>
Order Deny,Allow
Deny from all
Allow from localhost
Allow from 127
Allow from ::1
Allow from 192.168.3
</Proxy>
Timeout 600
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:8880/
ProxyPassReverse / http://localhost:8880/
#RequestHeader set X-Forwarded-Proto "https"
</VirtualHost>
<VirtualHost *:443>
# General setup for the virtual host, inherited from global configuration
ServerName wsgi.magic-object.com
DocumentRoot "/var/www/wsgi"
RewriteEngine off
Include /etc/letsencrypt/options-ssl-apache.conf
Include /etc/httpd/conf.d/ssl.setting
SSLCertificateFile /etc/letsencrypt/live/wsgi.magic-object.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/wsgi.magic-object.com/privkey.pem
<Proxy *>
Order Deny,Allow
Deny from all
Allow from localhost
Allow from 127
Allow from ::1
Allow from 192.168.3
</Proxy>
Timeout 600
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:8880/
ProxyPassReverse / http://localhost:8880/
#RequestHeader set X-Forwarded-Proto "https"
</VirtualHost>
ポートの「80番」と「443番」で、リバースプロキシで「http://localhost:8880/」に飛ばししているだけである。しかし、「80番」は正常に動作したのだが、「443番」は、何故か「https://www.magic-object.com」に飛ばされてしまう。
原因が分からずに悩んでいると、どうやら次の設定が動作しているらしい事が判明した。
<VirtualHost *:443>
# General setup for the virtual host, inherited from global configuration
ServerName www.magic-object.com
DocumentRoot "/var/www/html"
Include /etc/letsencrypt/options-ssl-apache.conf
Include /etc/httpd/conf.d/ssl.setting
SSLCertificateFile /etc/letsencrypt/live/www.magic-object.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.magic-object.com/privkey.pem
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.magic-object.com
RewriteRule ^ https://www.magic-object.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
はぁ? なぜ?
結論
実は DNS で「wsgi.magic-object.com」は「www.magic-object.com」の「CNAME」ですと宣言していた。「https」接続では証明書の関係から、当然、名前解決を行い証明書が有効なモノかどうかを判断する。
ここで、「wsgi.magic-object.com」は「www.magic-object.com」の「CNAME」なので、それじゃぁ「www.magic-object.com」アドレスって事だよね、とばかりに「wsgi.magic-object.com」を「www.magic-object.com」扱いにしていたのである。
「https」接続を行うバーチャルホストの場合には、DNS 関連の設定も関係してくるのである。
