この文書ではネームベースのバーチャルホストをどんなとき、 またどうやって使うのかということを説明します。
参考: 標準的なセットアップでの バーチャルホストの例、 IP ベースの バーチャルホストのサポート、 バーチャルホストのマッチングの具体的な詳細 と 大量のバーチャルホストを動的に設定する
初期のバージョンの HTTP では (他の多くのプロトコル、例えば FTP と同様) 一つのサーバ上でのバーチャルホストごとに異なった IP アドレスが必要でした。いくつかのプラットフォームでは、このことが 実行可能なバーチャルホストの数を限定してしまうことになりました。 また、IP アドレスの数には限りがあるということについての懸念から、 レジストラ (ARIN, RIPE, APNIC) はバーチャルホストごとに IP を割り当てるようなことは避けるよう強く要請しています。
名前ベースのバーチャルホストは通常単純で、それぞれのホスト名と それに対応する正確な IP アドレスを DNS で設定し、異なる ホスト名を区別出来るように Apache HTTP サーバを設定するだけです。 さらに、名前ベースのバーチャルホストは不足する IP アドレスの需要を緩和します。したがって、IP ベースのバーチャルホストを 選択すべき特定の理由がなければ名前ベースのバーチャルホストを使うべきです。 IP ベースのバーチャルホストを使用することを考慮する理由として、
Related Directives DocumentRoot NameVirtualHost ServerAlias ServerName ServerPath VirtualHost |
To use name-based virtual hosting,
you must designate the IP address (and possibly port) on the server
that will be accepting requests for the hosts.
これは NameVirtualHost
ディレクティブで設定します。通常、NameVirtualHost
で
*
の属性を使ってサーバの全ての IP アドレスを使います。
Note that mentioning an IP address in a NameVirtualHost
directive does not automatically make the server listen to that IP address.
See Setting which addresses and ports Apache uses for more details.
In addition, any IP address specified here must be associated
with a network interface on the server.
The next step is to create a <VirtualHost>
block for each different host that you would like to serve.
The argument to the <VirtualHost>
directive
should be the same as the argument to the NameVirtualHost
directive (ie, an IP address, or *
for all addresses).
Inside each <VirtualHost>
block,
you will need at minimum a ServerName directive
to designate which host is served and a DocumentRoot
directive to show where in the filesystem the content for that host lives.
たとえば、www.domain.tld と
www.otherdomain.tld の両方が 1つの IP
アドレスを指しているとしましょう。そのような場合は、
httpd.conf
に以下のようなコードを追加するだけです
NameVirtualHost * <VirtualHost *> ServerName www.domain.tld DocumentRoot /www/domain </VirtualHost> <VirtualHost *> ServerName www.otherdomain.tld DocumentRoot /www/otherdomain </VirtualHost>
NameVirtualHost
及び
<VirtualHost>
のどちらの場合も、
* の部分には明示的にIPアドレスを指定することができます。
複数の名前でサーバアクセスができるようにしたいことも多いでしょう。
このようなことは、ServerAlias
ディレクティブを <VirtualHost>
セクションに記述することで実現できます。
例えばこれを最初の <VirtualHost> ブロックに書き加えると、
ServerAlias domain.tld *.domain.tld
domain.tld
ドメインへの全てのホストへのリクエストは
www.domain.tld
のバーチャルホストが処理します。
* や ? をワイルドカード文字として使用できます。
Of course,you can't just make up names and place them in ServerName
or ServerAlias
.
第一に DNS のホスト名と IP アドレスをサーバにあわせて適切に設定する事が必要です
Finally, you can fine-tune the configuration of the virtual hosts
by placing other directives inside the <VirtualHost>
containers.
Most directives can be placed in these containers and
will then change the configuration only of the relevant virtual host.
To find out if a particular directive is allowed,
check the Context of the directive.
Configuration directives set in the main server context
(outside any <VirtualHost>
container)
will be used only if they are not overriden by the virtual host settings.
リクエストが来ると、サーバはまず最初に NameVirtualHost
にマッチする IP アドレスかどうかをチェックします。マッチすれば
マッチした IP アドレスの <VirtualHost>
のそれぞれのセクションの中から ServerName
か
ServerAlias
に要求されたホスト名があるか探します。
見つかればそのサーバ用の設定を使います。マッチするバーチャルホスト
が見つからなければ、マッチした IP アドレスの
リストの最初にあるバーチャルホスト が使われます。
結果として、リストの最初のバーチャルホストが デフォルト の
バーチャルホストになります。IP アドレスが NameVirtualHost
ディレクティブにマッチした場合は、メインのサーバ の
DocumentRoot
使われ ません
どのバーチャルホストにもマッチしないリクエストに対して、
特別な設定をしたいのであれば、設定ファイル中の最初の
<VirtualHost>
にそれを記述してください。
上で書いたように、ネームベースのバーチャルホストが正しく動作する ために必要な情報を送ってこないクライアントが依然として存在しています。 そのようなクライアントに対しては、該当する IP アドレスについて、 一番最初に設定されているバーチャルホスト (プライマリのネームベースのバーチャルホスト) からページが送り返されます。
ServerPath
ディレクティブで対処が可能です。ちょっと不格好ですけれども。
設定例
NameVirtualHost 111.22.33.44 <VirtualHost 111.22.33.44> ServerName www.domain.tld ServerPath /domain DocumentRoot /web/domain </VirtualHost>
この例にはどういう意味があるでしょうか? これは
"/domain" で始まる URI へのリクエストはすべて、
バーチャルホスト www.domain.tld で処理される、
という意味です。つまり、すべてのクライアントで
http://www.domain.tld/domain/
でアクセスできるページが、
Host: ヘッダを送ってくるクライアントであれば
http://www.domain.tld/
としてもアクセスできる、
という意味です。
これを実用的なものとするためには、 プライマリのバーチャルホストのページに http://www.domain.tld/domain/ へのリンクを設置します。 そして、バーチャルホストのページでは、純粋な相対リンク (例: "file.html" や "../icons/image.gif")、 あるいは /domain/ で始まるリンク (例: "http://www.domain.tld/domain/misc/file.html" や "/domain/misc/file.html") だけを設置します。
これには、幾分かの規律が必要となりますが、 このようなガイドラインを忠実に守ることにより、たいていの場合、 すべてのブラウザで ― 新しいブラウザでも古いものでも ― 作成したページが見えるということを保証します。
参考: ServerPath 設定例