1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
4 <!-- English Revision: 659902:1746754 (outdated) -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="examples.xml.meta">
24 <parentdocument href="./">バーチャルホスト</parentdocument>
25 <title>バーチャルホストの例</title>
29 <p>この文書は、バーチャルホストの設定の際に
30 よくある質問に答えるものです。想定している対象は <a
31 href="name-based.html">名前ベース</a> や <a
32 href="ip-based.html">IP ベース</a> のバーチャルホストを使って
33 一つのサーバで複数のウェブサイトを運用している状況です。
38 <section id="purename"><title>一つの IP アドレスでいくつかの名前ベースの
41 <p>サーバは IP アドレスを一つ割り当てられていて、DNS でマシンに
42 複数の名前 (CNAME) が指定されています。このマシンで
43 <code>www.example.com</code> と <code>www.example.org</code>
44 のためのウェブサーバを実行させたいとします。</p>
46 <note><title>注</title><p>
47 Apache サーバの設定でバーチャルホストの設定をしただけで、
48 知らない間にそのホスト名に対応する DNS のエントリが
49 作成されたりはしません。そのサーバの IP アドレスに解決される
50 ように DNS に名前を登録しなければ<em>なりません</em>。
51 そうでないと誰もあなたのウェブサイトを見ることはできません。
52 ローカルでのテストのために <code>hosts</code> ファイルに
53 エントリを追加することもできますが、この場合はその
54 hosts エントリのあるマシンからしか動作しません。</p>
60 # Ensure that Apache listens on port 80<br />
63 # Listen for virtual host requests on all IP addresses<br />
64 NameVirtualHost *:80<br />
66 <VirtualHost *:80><br />
68 DocumentRoot /www/example1<br />
69 ServerName www.example.com<br />
71 # Other directives here<br />
74 </VirtualHost><br />
76 <VirtualHost *:80><br />
78 DocumentRoot /www/example2<br />
79 ServerName www.example.org<br />
81 # Other directives here<br />
87 <p>アスタリスクはすべてのアドレスにマッチしますので、主サーバは
88 リクエストを扱いません。<code>www.example.com</code> は
89 最初にあるため、優先順位は一番高くなり、<cite>default</cite> もしくは
90 <cite>primary</cite> のサーバと考えることができます。つまり、リクエストが
91 どの <code>ServerName</code> ディレクティブにもマッチしない場合、
92 一番最初の <code>VirtualHost</code> により扱われます。</p>
94 <note><title>注</title>
96 <p><code>*</code> をシステムの実際の IP アドレスに置き換える
97 こともできます。その場合は <code>VirtualHost</code> の引数は
98 <code>NameVirtualHost</code> の引数と同じに<em>しなければなりません
102 NameVirtualHost 172.20.30.40<br />
104 <VirtualHost 172.20.30.40><br />
108 <p>しかし、IP アドレスが予測不可能なシステム
109 ――例えばプロバイダから動的に IP アドレスを取得して何らかの
110 ダイナミック DNS を使っている場合など――においては、<code>*</code>
111 指定はさらに便利です。<code>*</code> はすべての IP アドレスに
112 マッチしますので、この設定にしておけば IP アドレスが変更されても
116 <p>名前ベースのバーチャルホスティングではほぼすべての状況で、
117 上記の設定で希望の設定になっていることでしょう。
118 実際この設定が動作しないのは、IP アドレスやポートの違いによって
119 違うコンテンツを送るときだけです。</p>
123 <section id="twoips"><title>複数の IP アドレスのあるホストで名前ベースの
127 <title>注</title><p>ここで説明されている方法は IP アドレスが
131 <p>サーバには二つ IP アドレスがついています。一つ目
132 (<code>172.20.30.40</code>) では主サーバ
133 <code>server.domain.com</code> を扱い、もう一方
134 (<code>172.20.30.50</code>) では二つかそれ以上の数の
138 <title>サーバの設定</title>
142 # This is the "main" server running on 172.20.30.40<br />
143 ServerName server.domain.com<br />
144 DocumentRoot /www/mainserver<br />
146 # This is the other address<br />
147 NameVirtualHost 172.20.30.50<br />
149 <VirtualHost 172.20.30.50><br />
151 DocumentRoot /www/example1<br />
152 ServerName www.example.com<br />
154 # Other directives here ...<br />
157 </VirtualHost><br />
159 <VirtualHost 172.20.30.50><br />
161 DocumentRoot /www/example2<br />
162 ServerName www.example.org<br />
164 # Other directives here ...<br />
170 <p><code>172.20.30.50</code> 以外のアドレスへのリクエストは主サーバ
171 が扱います。<code>172.20.30.50</code> への、未知のホスト名または
172 <code>Host:</code> ヘッダなしのリクエストは <code>www.example.com</code>
177 <section id="intraextra"><title>違う IP アドレス (例えば、内部と外部アドレス)
180 <p>サーバマシンは IP アドレスを二つ (<code>192.168.1.1</code>
181 と <code>172.20.30.40</code>) 持っています。このマシンは内部
182 (イントラネット) と 外部 (インターネット) のネットワークの間に
183 あります。<code>server.example.com</code> はネットワークの外からは
184 外部アドレス (<code>172.20.30.40</code>) として解決されますが、
185 ネットワークの中からは内部アドレス (<code>192.168.1.1</code>)
188 <p><code>VirtualHost</code> 一つだけでサーバが内部のリクエストと
189 外部のリクエストの両方に同じコンテンツで応答するようにできます。</p>
192 <title>サーバの設定</title>
194 NameVirtualHost 192.168.1.1<br />
195 NameVirtualHost 172.20.30.40<br />
197 <VirtualHost 192.168.1.1 172.20.30.40><br />
199 DocumentRoot /www/server1<br />
200 ServerName server.example.com<br />
201 ServerAlias server<br />
206 <p>これでどちらのネットワークからのリクエストも同じ <code>VirtualHost</code>
209 <note><title>注:</title><p>内部ネットワークでは完全なホスト名の
210 <code>server.example.com</code> の代わりに、単に <code>server</code>
213 <p>上の例では、IP アドレスのリストを、すべてのアドレスに
214 同じコンテンツで応答する <code>*</code> に置き換えられます。</p>
219 <section id="port"><title>違うポートで違うサイトを運営する</title>
221 <p>同じ IP に複数のドメインがあり、さらに複数のポートを使って
222 リクエストを扱いたいときがあります。"NameVirtualHost" タグの中で
223 ポートを定義することで、これを動作させられます。
224 NameVirtualHost name:port 無しや Listen ディレクティブで
225 <VirtualHost name:port> を使おうとしても、その設定は動作しません。</p>
228 <title>サーバの設定</title>
233 NameVirtualHost 172.20.30.40:80<br />
234 NameVirtualHost 172.20.30.40:8080<br />
236 <VirtualHost 172.20.30.40:80><br />
238 ServerName www.example.com<br />
239 DocumentRoot /www/domain-80<br />
241 </VirtualHost><br />
243 <VirtualHost 172.20.30.40:8080><br />
245 ServerName www.example.com<br />
246 DocumentRoot /www/domain-8080<br />
248 </VirtualHost><br />
250 <VirtualHost 172.20.30.40:80><br />
252 ServerName www.example.org<br />
253 DocumentRoot /www/otherdomain-80<br />
255 </VirtualHost><br />
257 <VirtualHost 172.20.30.40:8080><br />
259 ServerName www.example.org<br />
260 DocumentRoot /www/otherdomain-8080<br />
267 <section id="ip"><title>IP ベースのバーチャルホスティング</title>
269 <p>サーバは <code>www.example.com</code> と <code>www.example.org</code>
270 にそれぞれ解決される、二つの IP アドレス (<code>172.20.30.40</code> と
271 <code>172.20.30.50</code>) があります。</p>
274 <title>サーバの設定</title>
278 <VirtualHost 172.20.30.40><br />
280 DocumentRoot /www/example1<br />
281 ServerName www.example.com<br />
283 </VirtualHost><br />
285 <VirtualHost 172.20.30.50><br />
287 DocumentRoot /www/example2<br />
288 ServerName www.example.org<br />
293 <p><code><VirtualHost></code> ディレクティブのどれでも
294 指定されていないアドレス (例えば <code>localhost</code>) は、
299 <section id="ipport"><title>ポートベースと IP ベースの混ざった
302 <p>サーバマシンはそれぞれ <code>www.example.com</code> と
303 <code>www.example.org</code> にそれぞれ解決される、IP アドレスを二つ
304 (<code>172.20.30.40</code> と <code>172.20.30.50</code>) 持っています。
305 どちらもポート 80 と 8080 でホストを走らせます。</p>
308 <title>サーバの設定</title>
310 Listen 172.20.30.40:80<br />
311 Listen 172.20.30.40:8080<br />
312 Listen 172.20.30.50:80<br />
313 Listen 172.20.30.50:8080<br />
315 <VirtualHost 172.20.30.40:80><br />
317 DocumentRoot /www/example1-80<br />
318 ServerName www.example.com<br />
320 </VirtualHost><br />
322 <VirtualHost 172.20.30.40:8080><br />
324 DocumentRoot /www/example1-8080<br />
325 ServerName www.example.com<br />
327 </VirtualHost><br />
329 <VirtualHost 172.20.30.50:80><br />
331 DocumentRoot /www/example2-80<br />
332 ServerName www.example.org<br />
334 </VirtualHost><br />
336 <VirtualHost 172.20.30.50:8080><br />
338 DocumentRoot /www/example2-8080<br />
339 ServerName www.example.org<br />
346 <section id="mixed"><title>名前ベースと IP ベースを混ぜた
349 <p>いくつかのマシンでは名前ベースの、その他では IP ベースのバーチャル
353 <title>サーバの設定</title>
357 NameVirtualHost 172.20.30.40<br />
359 <VirtualHost 172.20.30.40><br />
361 DocumentRoot /www/example1<br />
362 ServerName www.example.com<br />
364 </VirtualHost><br />
366 <VirtualHost 172.20.30.40><br />
368 DocumentRoot /www/example2<br />
369 ServerName www.example.org<br />
371 </VirtualHost><br />
373 <VirtualHost 172.20.30.40><br />
375 DocumentRoot /www/example3<br />
376 ServerName www.example3.net<br />
378 </VirtualHost><br />
381 <VirtualHost 172.20.30.50><br />
383 DocumentRoot /www/example4<br />
384 ServerName www.example4.edu<br />
386 </VirtualHost><br />
388 <VirtualHost 172.20.30.60><br />
390 DocumentRoot /www/example5<br />
391 ServerName www.example5.gov<br />
398 <section id="proxy"><title><code>Virtual_host</code> と
399 mod_proxy を併用する</title>
401 <p>次の例は、フロント側のバーチャルホストで他のマシンへプロクシします。
402 例では <code>192.168.111.2</code> のマシンではバーチャルホスト名は
403 同じ名前で設定されています。複数のホスト名を一台のマシンにプロクシする
404 場合は、<directive module="mod_proxy">ProxyPreserveHost On</directive>
405 ディレクティブを使って、希望のホスト名を渡せるようになります。
409 <VirtualHost *:*><br />
410 ProxyPreserveHost On<br />
411 ProxyPass / http://192.168.111.2/<br />
412 ProxyPassReverse / http://192.168.111.2/<br />
413 ServerName hostname.example.com<br />
419 <section id="default"><title><code>_default_</code> のバーチャルホストを
422 <section id="defaultallports"><title>すべてのポートに対する
423 <code>_default_</code> バーチャルホスト</title>
425 <p>未指定の IP アドレスとポート、<em>つまり</em>他のバーチャルホストに
426 使われていないアドレスとポートの組み合わせ、への<em>すべての</em>リクエストを
430 <title>サーバの設定</title>
432 <VirtualHost _default_:*><br />
434 DocumentRoot /www/default<br />
439 <p>このようにワイルドカードのポートでデフォルトのバーチャルホストを
440 指定すると、主サーバにリクエストが行くのを防げます。</p>
442 <p>デフォルトのバーチャルホストは名前ベースのバーチャルホストに
443 使われているアドレスとポートの組に送られたリクエストを扱うことは
444 ありません。リクエストが不明な <code>Host:</code> ヘッダやその
445 ヘッダがなかったりする場合は基本名前ベースバーチャルホスト (その
446 アドレスとポートで設定ファイル中で最初のバーチャルホスト) により
449 <p>どんなリクエストでも <directive module="mod_alias">AliasMatch</directive>
450 や <directive module="mod_rewrite">RewriteRule</directive> を使って
451 単一の情報ページ (やスクリプト) に書き換えることができます。</p>
454 <section id="defaultdifferentports"><title>違うポートのための
455 <code>_default_</code> バーチャルホスト</title>
457 <p>一つめの設定とほぼ同じですが、サーバは複数のポートを listen しており、
458 80 番ポートに対して二つめの <code>_default_</code> バーチャルホストを
462 <title>サーバの設定</title>
464 <VirtualHost _default_:80><br />
466 DocumentRoot /www/default80<br />
469 </VirtualHost><br />
471 <VirtualHost _default_:*><br />
473 DocumentRoot /www/default<br />
479 <p>80 番ポートのデフォルトバーチャルホスト (ワイルドカードポートの
480 デフォルトバーチャルホストよりも前に書かれていなければ<em>なりません</em>) は
481 未指定の IP アドレスに送られたすべてのリクエストを扱います。
482 主サーバはリクエストを扱いません。</p>
485 <section id="defaultoneport"><title>一つのポートに対してだけの
486 <code>_default_</code> バーチャルホスト</title>
488 <p>80 番ポートにはデフォルトのバーチャルホストが必要で、他の
489 バーチャルホストはデフォルトが必要ない場合です。</p>
492 <title>サーバの設定</title>
494 <VirtualHost _default_:80><br />
495 DocumentRoot /www/default<br />
500 <p>80 番ポートへのアドレス未指定のリクエストはデフォルトのバーチャル
501 ホストから送られます。他の未指定のアドレスとポートへのリクエストは
507 <section id="migrate"><title>名前ベースのバーチャルホストから IP ベースの
508 バーチャルホストに移行する</title>
510 <p>ホスト名が名前 <code>www.example.org</code> のバーチャルホスト
511 (<a href="#name">名前ベース</a>の例の 2 番目の設定) が専用の IP アドレスを
512 得たとします。名前ベースのバーチャルホストの古い IP アドレスを
513 キャッシュしているネームサーバやプロキシのために移行期間中は両方の
514 バーチャルホストを提供したいとします。</p>
516 <p>答は簡単です。単に新しい IP アドレス (<code>172.20.30.50</code>)
517 を <code>VirtualHost</code> ディレクティブに追加することで
524 ServerName www.example.com<br />
525 DocumentRoot /www/example1<br />
527 NameVirtualHost 172.20.30.40<br />
529 <VirtualHost 172.20.30.40 172.20.30.50><br />
531 DocumentRoot /www/example2<br />
532 ServerName www.example.org<br />
535 </VirtualHost><br />
537 <VirtualHost 172.20.30.40><br />
539 DocumentRoot /www/example3<br />
540 ServerName www.example.net<br />
541 ServerAlias *.example.net<br />
547 <p>このバーチャルホストは新しいアドレス (IP ベースのバーチャルホストとして)
548 と古いアドレス(名前ベースのバーチャルホストとして) の両方から
553 <section id="serverpath"><title><code>ServerPath</code> ディレクティブを
556 <p>名前ベースのバーチャルホストが二つあるサーバがあるとします。
557 正しいバーチャルホストを得るためにはクライアントは正しい
558 <code>Host:</code> ヘッダを送らなければなりません。
559 古い HTTP/1.0 はそのようなヘッダを送らないので、Apache はクライアントが
560 どのバーチャルホストを意図したのかさっぱりわかりません
561 (なので、主バーチャルホストでリクエストを扱います)。
562 可能な限りの下位互換性を得るため、名前ベースのバーチャルホストの
563 URL 接頭辞へのリンクの書かれたページを返す、
564 主バーチャルホストが作成されます。</p>
567 <title>サーバの設定</title>
569 NameVirtualHost 172.20.30.40<br />
571 <VirtualHost 172.20.30.40><br />
573 # primary vhost<br />
574 DocumentRoot /www/subdomain<br />
575 RewriteEngine On<br />
576 RewriteRule ^/.* /www/subdomain/index.html<br />
579 </VirtualHost><br />
581 <VirtualHost 172.20.30.40><br />
582 DocumentRoot /www/subdomain/sub1<br />
584 ServerName www.sub1.domain.tld<br />
585 ServerPath /sub1/<br />
586 RewriteEngine On<br />
587 RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
590 </VirtualHost><br />
592 <VirtualHost 172.20.30.40><br />
594 DocumentRoot /www/subdomain/sub2<br />
595 ServerName www.sub2.domain.tld<br />
596 ServerPath /sub2/<br />
597 RewriteEngine On<br />
598 RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
604 <p><directive module="core">ServerPath</directive> ディレクティブの設定に
605 より、URL <code>http://www.sub1.domain.tld/sub1/</code> は
606 <em>常に</em> sub1-vhost により扱われます。URL
607 <code>http://www.sub1.domain.tld/</code> へのリクエストは
608 クライアントが正しい <code>Host:</code> ヘッダを送ったときにのみ
609 sub1-vhost から送られます。<code>Host:</code> ヘッダがなければ
610 クライアントは主ホストの情報ページを得ます。</p>
612 <p>一つ奇妙な動作をする点があることは覚えておいてください。
613 <code>http://www.sub2.domain.tld/sub1/</code> へのリクエストも
614 <code>Host:</code> ヘッダがなければ sub1-vhost により扱われます。</p>
616 <p>正しい <code>Host:</code> ヘッダを送ったクライアントはどちらの
617 URL、<em>つまり</em>接頭辞がある方も無い方も使えるように
618 <directive module="mod_rewrite">RewriteRule</directive> ディレクティブが