名前ベースのバーチャルホスト

|
1.NameVirtualHostや<VirtualHost >の引数の部分のIPアドレスとポート
2.ServerName "バーチャルホスト名"
の組み合わせで応答が決定します。

ブラウザが、「1」で設定されているIPアドレス、「2」で設定されているバーチャルホスト名でアクセスしたときに、Apache側は該当するServerNameが書かれている<VirtualHost >ディレクティブの設定で動作します。

 NameVirtualHostや<VirtualHost >ディレクティブの引数の部分の「*:80」は、「サーバーの持っているすべてのIPアドレス:ポート番号」を意味します。
 
 ここの引数の部分ですが、「*」のほかにもサーバーが持っている具体的なIPアドレスを指定してもいいし、本当のホスト名やバーチャルホスト名を指定しても大丈夫ですが、「*」か、IPアドレスをお勧めします

 また、ポート番号の部分「:80」も、httpd.confの上の方でListen 80を指定してあるので省略可能な場合がありますが、やはり明示的にポート番号を指定したほうがよいでしょう。※理由は後述

■プライマリ仮想サーバー
定義しているバーチャルホスト以外へのリクエストは、設定ファイル内で最初に定義してあるバーチャルホストに送られます。

 なので、一番最初の<VirtualHost *:80>は、http://"IPアドレス"や、http://"本当のサーバー名"などでアクセスされた場合を想定して記述しておくことになります。

※アドレスベースのバーチャルホストで、<VirtualHost _default_:80>を設定してしまうのも有効です。

NameVirtualHost *:80

<VirtualHost *:80> ServerAdmin webmaster@example.local ServerName www.example.local DocumentRoot /var/www/html DirectoryIndex index.html </VirtualHost>

<VirtualHost *:80> ServerAdmin webmaster@hoge.local ServerName www.hoge.local DocumentRoot /home/www.hoge.local/htdocs DirectoryIndex index.html ErrorLog /home/www.hoge.local/logs/error_log CustomLog /home/www.hoge.local/logs/access_log common </VirtualHost>

<VirtualHost *:80> ServerAdmin webmaster@domain.local ServerName www.domain.local DocumentRoot /home/www.domain.local/htdocs DirectoryIndex index.html ErrorLog /home/www.domain.local/logs/error_log CustomLog /home/www.domain.local/logs/access_log common </VirtualHost>

■NameVirtualHostや<VirtualHost >ディレクティブでサーバー名を指定しないほうがよい理由

下記のような書き方もできます。
わかりやすいといえばわかりやすいです。

しかし、サーバー名に依存した記述をすると、DNSに依存することになります。
サーバーの起動時にバーチャルホスト名の名前引きができなかった場合、その名前ベースバーチャルホスト設定は効き目がなくなって、一番上のバーチャルホストの設定で応答することになります。

NameVirtualHost www.example.local:80

<VirtualHost www.example.local:80> ServerAdmin webmaster@example.local ServerName www.example.local DocumentRoot /var/www/html DirectoryIndex index.html </VirtualHost>

<VirtualHost www.hoge.local:80> ServerAdmin webmaster@hoge.local ServerName www.hoge.local DocumentRoot /home/www.hoge.local/htdocs DirectoryIndex index.html ErrorLog /home/www.hoge.local/logs/error_log CustomLog /home/www.hoge.local/logs/access_log common </VirtualHost>

<VirtualHost www.domain.local:80> ServerAdmin webmaster@domain.local ServerName www.domain.local DocumentRoot /home/www.domain.local/htdocs DirectoryIndex index.html ErrorLog /home/www.domain.local/logs/error_log CustomLog /home/www.domain.local/logs/access_log common </VirtualHost>


■NameVirtualHostや<VirtualHost >ディレクティブで、ポート番号を明示的に指定したほうがよい理由

 RedHat系のLinuxなどでmod_sslをインストールしている場合、/etc/httpd/conf.d/ssl.confにdefaultのIPアドレスベースのバーチャルホストで、ポート443で待ち受ける設定があるので、通常のhttpアクセスのバーチャルホスト設定のほうでも80番を明示的に指定しないとapacheの起動時に警告を受けます。

○/etc/httpd/conf.d/ssl.confのdefault設定

LoadModule ssl_module modules/mod_ssl.so
Listen 443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl    .crl
SSLPassPhraseDialog  builtin
SSLSessionCache         shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout  300
SSLMutex default
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
##
## SSL Virtual Host Context
##
<VirtualHost _default_:443>
  ErrorLog logs/ssl_error_log
  TransferLog logs/ssl_access_log
  LogLevel warn

SSLEngine on SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key

<Files ~ "\.(cgi|shtml|phtml|php3?)$"> SSLOptions +StdEnvVars </Files> <Directory "/var/www/cgi-bin"> SSLOptions +StdEnvVars </Directory>

SetEnvIf User-Agent ".*MSIE.*" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0

CustomLog logs/ssl_request_log \ "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" </VirtualHost>

このブログ記事について

このページは、くわぞうが2006年5月25日 01:32に書いたブログ記事です。

ひとつ前のブログ記事は「bindのバージョンを見る」です。

次のブログ記事は「バーチャルホストの設定を確認する方法」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

Powered by Movable Type 4.1