43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
4a56677aad9b66a36f3dc9fddbca8dc1230ad471rbowen<!-- $LastChangedRevision$ -->
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding Licensed to the Apache Software Foundation (ASF) under one or more
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding contributor license agreements. See the NOTICE file distributed with
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding this work for additional information regarding copyright ownership.
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding The ASF licenses this file to You under the Apache License, Version 2.0
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding (the "License"); you may not use this file except in compliance with
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding the License. You may obtain a copy of the License at
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd Unless required by applicable law or agreed to in writing, software
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd distributed under the License is distributed on an "AS IS" BASIS,
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd See the License for the specific language governing permissions and
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd limitations under the License.
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki<parentdocument href="./">Virtual Hosts</parentdocument>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki <p>This document describes when and how to use name-based virtual hosts.</p>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki<seealso><a href="ip-based.html">IP-based Virtual Host Support</a></seealso>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki<seealso><a href="details.html">An In-Depth Discussion of Virtual Host Matching</a></seealso>
208651a016b098f4fa1f6279559f104d70f1632dtakashi<seealso><a href="mass.html">Dynamically configured mass virtual hosting</a></seealso>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki<seealso><a href="examples.html">Virtual Host examples for common setups</a></seealso>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki<section id="namevip"><title>Name-based vs. IP-based Virtual Hosts</title>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <p><a href="ip-based.html">IP-based virtual hosts</a> use the IP address of the connection to
208651a016b098f4fa1f6279559f104d70f1632dtakashi determine the correct virtual host to serve. Therefore you need to
51518469f1207f11cd9a00d303838b86fe27b55cyoshiki have a separate IP address for each host.</p>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki <p>With name-based virtual hosting, the server relies on the client to
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki report the hostname as part of the HTTP headers. Using this technique,
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki many different hosts can share the same IP address.</p>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <p>Name-based virtual hosting is usually simpler, since you need
c68796f705286dc3fd9c0d23a57fe459b04de864takashi only configure your DNS server to map each hostname to the correct
f099908f1df377e6f6ee90c5732fcd30f253012eyoshiki IP address and then configure the Apache HTTP Server to recognize
f099908f1df377e6f6ee90c5732fcd30f253012eyoshiki the different hostnames. Name-based virtual hosting also eases
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki the demand for scarce IP addresses. Therefore you should use
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki name-based virtual hosting unless you are using equipment
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki that explicitly demands IP-based hosting. Historical reasons for
208651a016b098f4fa1f6279559f104d70f1632dtakashi IP-based virtual hosting based on client support are no longer
208651a016b098f4fa1f6279559f104d70f1632dtakashi applicable to a general-purpose web server.</p>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <p> Name-based virtual hosting builds off of the IP-based virtual host
208651a016b098f4fa1f6279559f104d70f1632dtakashi selection algorithm, meaning that searches for the proper server name
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki occur only between virtual hosts that have the best IP-based address.</p>
208651a016b098f4fa1f6279559f104d70f1632dtakashi<section id="alg"><title>How the server selects the proper name-based virtual host</title>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <p>It is important to recognize that the first step in name-based virtual
208651a016b098f4fa1f6279559f104d70f1632dtakashi host resolution is IP-based resolution. Name-based virtual host
208651a016b098f4fa1f6279559f104d70f1632dtakashi resolution only chooses the most appropriate name-based virtual host
208651a016b098f4fa1f6279559f104d70f1632dtakashi after narrowing down the candidates to the best IP-based match. Using a wildcard (*)
208651a016b098f4fa1f6279559f104d70f1632dtakashi for the IP address in all of the VirtualHost directives makes this
208651a016b098f4fa1f6279559f104d70f1632dtakashi IP-based mapping irrelevant.</p>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <p>When a request arrives, the server will find the best (most specific) matching
208651a016b098f4fa1f6279559f104d70f1632dtakashi <directive type="section" module="core">VirtualHost</directive> argument based on
0e1bb310657b9d1540737c542e337abe37c8453bkawai the IP address and port used by the request. If there is more than one virtual host
10506784730138cf41a16c81c0b3d78224c2a2dekawai containing this best-match address and port combination, Apache will further
208651a016b098f4fa1f6279559f104d70f1632dtakashi compare the <directive module="core" >ServerName</directive> and <directive
208651a016b098f4fa1f6279559f104d70f1632dtakashi module="core">ServerAlias</directive> directives to the server name
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki present in the request.</p>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki <p>If you omit the <directive module="core">ServerName</directive>
208651a016b098f4fa1f6279559f104d70f1632dtakashi directive from any name-based virtual host, the server will default
208651a016b098f4fa1f6279559f104d70f1632dtakashi to a fully qualified domain name (FQDN) derived from the system hostname.
208651a016b098f4fa1f6279559f104d70f1632dtakashi This implicitly set server name can lead to counter-intuitive virtual host
208651a016b098f4fa1f6279559f104d70f1632dtakashi matching and is discouraged.</p>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <section id="defaultvhost"><title>The default name-based vhost for an IP and port combination </title>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki <p> If no matching ServerName or ServerAlias is found in the set of
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki virtual hosts containing the most specific matching IP address and port
208651a016b098f4fa1f6279559f104d70f1632dtakashi combination, then <strong>the first listed virtual host</strong> that
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki<section id="using"><title>Using Name-based Virtual Hosts</title>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <modulelist>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki </modulelist>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <directivelist>
c68796f705286dc3fd9c0d23a57fe459b04de864takashi <directive module="core" type="section">VirtualHost</directive>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki </directivelist>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki <p>The first step is to create a <directive type="section"
208651a016b098f4fa1f6279559f104d70f1632dtakashi each different host that you would like to serve. Inside each <directive type="section"
208651a016b098f4fa1f6279559f104d70f1632dtakashi module="core">VirtualHost</directive> block, you will need at minimum a
208651a016b098f4fa1f6279559f104d70f1632dtakashi <directive module="core">ServerName</directive> directive to designate
208651a016b098f4fa1f6279559f104d70f1632dtakashi which host is served and a <directive module="core">DocumentRoot</directive>
208651a016b098f4fa1f6279559f104d70f1632dtakashi directive to show where in the filesystem the content for that host
208651a016b098f4fa1f6279559f104d70f1632dtakashi <p> Any request that doesn't match an existing <directive type="section"
208651a016b098f4fa1f6279559f104d70f1632dtakashi module="core">VirtualHost</directive> is handled by the global
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki server configuration, regardless of the hostname or ServerName.</p>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <p> When you add a name-based virtual host to an existing server, and
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki the virtual host arguments match preexisting IP and port combinations,
208651a016b098f4fa1f6279559f104d70f1632dtakashi requests will now be handled by an explicit virtual host. In this case,
208651a016b098f4fa1f6279559f104d70f1632dtakashi it's usually wise to create a <a href="#defaultvhost">default virtual host</a>
208651a016b098f4fa1f6279559f104d70f1632dtakashi with a <directive module="core">ServerName</directive> matching that of
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki the base server. New domains on the same interface and port, but
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki requiring separate configurations, can then be added as subsequent (non-default)
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki virtual hosts.</p>
208651a016b098f4fa1f6279559f104d70f1632dtakashi <p> It is best to always explicitly list a <directive module="core"
208651a016b098f4fa1f6279559f104d70f1632dtakashi >ServerName</directive> in every name-based virtual host.</p>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki <p>If a <directive module="core">VirtualHost</directive> doesn't specify
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki a <directive module="core">ServerName</directive>, a server name will be
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki inherited from the base server configuration. If no server name was
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki specified globally, one is detected at startup through reverse DNS resolution
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki of the first listening address. In either case, this inherited server name
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki will influence name-based virtual host resolution, so it is best to always
208651a016b098f4fa1f6279559f104d70f1632dtakashi explicitly list a <directive module="core">ServerName</directive> in every
208651a016b098f4fa1f6279559f104d70f1632dtakashi name-based virtual host.</p>
c68796f705286dc3fd9c0d23a57fe459b04de864takashi <p>For example, suppose that you are serving the domain
f099908f1df377e6f6ee90c5732fcd30f253012eyoshiki <code>www.example.com</code> and you wish to add the virtual host
f099908f1df377e6f6ee90c5732fcd30f253012eyoshiki <code>other.example.com</code>, which points at the same IP address.
c68796f705286dc3fd9c0d23a57fe459b04de864takashi Then you simply add the following to <code>httpd.conf</code>:</p>
208651a016b098f4fa1f6279559f104d70f1632dtakashi<VirtualHost *:80>
f099908f1df377e6f6ee90c5732fcd30f253012eyoshiki # This first-listed virtual host is also the default for *:80
208651a016b098f4fa1f6279559f104d70f1632dtakashi</VirtualHost>
208651a016b098f4fa1f6279559f104d70f1632dtakashi<VirtualHost *:80>
208651a016b098f4fa1f6279559f104d70f1632dtakashi</VirtualHost>
5f3c6037eb417c884d8e6b1de96d88d2b156d749kawai </highlight>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki <p>You can alternatively specify an explicit IP address in place of the
51518469f1207f11cd9a00d303838b86fe27b55cyoshiki <code>*</code> in <directive type="section" module="core"
208651a016b098f4fa1f6279559f104d70f1632dtakashi >VirtualHost</directive> directives. For example, you might want to do this
51518469f1207f11cd9a00d303838b86fe27b55cyoshiki in order to run some name-based virtual hosts on one IP address, and either
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki IP-based, or another set of name-based virtual hosts on another address.</p>
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki <p>Many servers want to be accessible by more than one name. This is
43b8f5cebbf28df342fcdda8fa1992c28eaa6309yoshiki possible with the <directive module="core">ServerAlias</directive>