バーチャルホスト設定(Apache)
備忘録として。
バーチャルのWebサーバーを立てる際、本来のサーバーネームを叩いた際に、その他のバーチャルWebが誤表示されないために。
以下の仮定
・メインWebサイトは、http://centossrv.com/
・追加するWebサイトは、http://virtual.com/
・メインWebサイトのドキュメントルートは/var/www/html
・追加するWebサイトのドキュメントルートは/var/www/html/virtual
・メインWebサイトへのアクセスログは/var/log/httpd/access_log、/var/log/httpd/error_logに記録する
・追加するWebサイトへのアクセスログは/var/log/httpd/virtual-access_log、/var/log/httpd/virtual-error_logに記録する
(1)バーチャルホスト設定
[root@centos ~]# mkdir /var/www/html/virtual ← バーチャルホスト用ドキュメントルートディレクトリ作成
[root@centos ~]# vi /etc/httpd/conf/httpd.conf ← Apache設定ファイル編集
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If this is not set to valid DNS name for your host, server-generated
# redirections will not work. See also the UseCanonicalName directive.
#
# If your host doesn’t have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make
# redirections work in a sensible way.
#
#ServerName centossrv.com:80 ← 行頭に#を追加してコメントアウト
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80 ← コメント解除(バーチャルホスト有効化)
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#
[root@centos ~]# vi /etc/httpd/conf.d/virtualhost-00.conf ← 未定義ホスト用バーチャルホスト設定ファイル作成
※バーチャルホスト未定義ホスト名でアクセス時にアクセスを拒否する
<VirtualHost *:80>
ServerName any
<Location />
Order deny,allow
Deny from all
</Location>
</VirtualHost>
[root@centos ~]# vi /etc/httpd/conf.d/virtualhost-centossrv.com.conf ← メインホスト用バーチャルホスト設定ファイル作成
<VirtualHost *:80>
ServerName centossrv.com
DocumentRoot /var/www/html
</VirtualHost>
[root@centos ~]# vi /etc/httpd/conf.d/virtualhost-virtual.com.conf ← 追加ホスト用バーチャルホスト設定ファイル作成
<VirtualHost *:80>
ServerName virtual.com
DocumentRoot /var/www/html/virtual
ErrorLog logs/virtual-error_log
CustomLog logs/virtual-access_log combined env=!no_log
</VirtualHost>
(2)WebサーバーSSL設定※Webサーバー間通信内容暗号化を導入している場合のみ
名前ベースバーチャルホストの場合(グローバルIPアドレス1個で複数のWebサイトを運営する場合)、メインホストがhttps://centossrv.com/、バーチャルホストがhttps://virtual.com/とした場合、https://centossrv.com/でアクセスしてもhttps://virtual.com/でアクセスしても、https://centossrv.com/のWebページが表示されてしまうので、 https://centossrv.com/でアクセスした場合はそのままで、https://virtual.com/でアクセスした場合はhttp://virtual.com/へリダイレクトするようにする。
[root@centos ~]# vi /etc/httpd/conf.d/ssl.conf ← WebサーバーSSL設定ファイル編集
# Per-Server Logging:
# The home of a custom SSL log file. Use this when you want a
# compact non-error SSL logfile on a virtual host basis.
CustomLog logs/ssl_request_log \
“%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \”%r\” %b”
--追加(ここから)--
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteLog “logs/rewrite_log”
RewriteLogLevel 0
RewriteCond %{HTTP_HOST} !^centossrv.com$ ← メインホスト名を指定
RewriteRule ^/(.*)?$ http://%{HTTP_HOST}/$1 [L,R]
</IfModule>
--追加(ここまで)--
</VirtualHost>
■Webサーバー再起動
[root@centos ~]# /etc/rc.d/init.d/httpd restart ← Webサーバー再起移動
httpd を停止中: [ OK ]
httpd を起動中: [ OK ]



