name-based.xml revision 4fdfee72da9a4a290d2fb41a548e343801a2635d
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<?xml version='1.0' encoding='UTF-8' ?>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<!DOCTYPE manualpage SYSTEM "/style/manualpage.dtd">
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<!-- $LastChangedRevision$ -->
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<!--
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna Licensed to the Apache Software Foundation (ASF) under one or more
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna contributor license agreements. See the NOTICE file distributed with
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna this work for additional information regarding copyright ownership.
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna The ASF licenses this file to You under the Apache License, Version 2.0
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna (the "License"); you may not use this file except in compliance with
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna the License. You may obtain a copy of the License at
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna http://www.apache.org/licenses/LICENSE-2.0
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna Unless required by applicable law or agreed to in writing, software
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna distributed under the License is distributed on an "AS IS" BASIS,
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna See the License for the specific language governing permissions and
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna limitations under the License.
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna-->
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<manualpage metafile="name-based.xml.meta">
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<parentdocument href="./">Virtual Hosts</parentdocument>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<title>Name-based Virtual Host Support</title>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<summary>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <p>This document describes when and how to use name-based virtual hosts.</p>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna</summary>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<seealso><a href="ip-based.html">IP-based Virtual Host Support</a></seealso>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna<seealso><a href="details.html">An In-Depth Discussion of Virtual Host Matching</a></seealso>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive<seealso><a href="mass.html">Dynamically configured mass virtual hosting</a></seealso>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive<seealso><a href="examples.html">Virtual Host examples for common setups</a></seealso>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive<section id="namevip"><title>Name-based vs. IP-based Virtual Hosts</title>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <p><a href="ip-based.html">IP-based virtual hosts</a> use the IP address of the connection to
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive determine the correct virtual host to serve. Therefore you need to
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive have a separate IP address for each host.</p>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <p>With name-based virtual hosting, the server relies on the client to
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive report the hostname as part of the HTTP headers. Using this technique,
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive many different hosts can share the same IP address.</p>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <p>Name-based virtual hosting is usually simpler, since you need
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna only configure your DNS server to map each hostname to the correct
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna IP address and then configure the Apache HTTP Server to recognize
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive the different hostnames. Name-based virtual hosting also eases
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive the demand for scarce IP addresses. Therefore you should use
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive name-based virtual hosting unless you are using equipment
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive that explicitly demands IP-based hosting. Historical reasons for
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive IP-based virtual hosting based on client support are no longer
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive applicable to a general-purpose web server.</p>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <p> Name-based virtual hosting builds off of the IP-based virtual host
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive selection algorithm, meaning that searches for the proper server name
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive occur only between virtual hosts that have the best IP-based address.</p>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive</section>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive<section id="alg"><title>How the server selects the proper name-based virtual host</title>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <p>It is important to recognize that the first step in name-based virtual
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive host resolution is IP-based resolution. Name-based virtual host
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive resolution only chooses the most appropriate name-based virtual host
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive after narrowing down the candidates to the best IP-based match. Using a wildcard (*)
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive for the IP address in all of the VirtualHost directives makes this
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive IP-based mapping irrelevant.</p>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <p>When a request arrives, the server will find the best (most specific) matching
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <directive type="section" module="core">VirtualHost</directive> argument based on
1d9308ed0075062953a246d16bcda888a1be1adeslive the IP address and port used by the request. If there is more than one virtual host
1d9308ed0075062953a246d16bcda888a1be1adeslive containing this best-match address and port combination, Apache will further
1d9308ed0075062953a246d16bcda888a1be1adeslive compare the <directive module="core" >ServerName</directive> and <directive
1d9308ed0075062953a246d16bcda888a1be1adeslive module="core">ServerAlias</directive> directives to the server name
1d9308ed0075062953a246d16bcda888a1be1adeslive present in the request.</p>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <section id="defaultvhost"><title>The default name-based vhost for an IP and port combination </title>
1d9308ed0075062953a246d16bcda888a1be1adeslive <p> If no matching ServerName or ServerAlias is found in the set of
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive virtual hosts containing the most specific matching IP address and port
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive combination, then <strong>the first listed virtual host</strong> that
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive matches that will be used.</p></section>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive</section>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive<section id="using"><title>Using Name-based Virtual Hosts</title>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive<related>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <modulelist>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <module>core</module>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive </modulelist>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <directivelist>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <directive module="core">DocumentRoot</directive>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <directive module="core">ServerAlias</directive>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <directive module="core">ServerName</directive>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <directive module="core" type="section">VirtualHost</directive>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive </directivelist>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive</related>
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <p>The first step is to create a <directive type="section"
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive module="core">VirtualHost</directive> block for
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive each different host that you would like to serve. Inside each <directive type="section"
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive module="core">VirtualHost</directive> block, you will need at minimum a
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive <directive module="core">ServerName</directive> directive to designate
57758b7ebacbd303cb1c004dd67dd8e5cc1055dbslive which host is served and a <directive module="core">DocumentRoot</directive>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna directive to show where in the filesystem the content for that host
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna lives.</p>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <note><title>Main host goes away</title>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <p> Any request that doesn't match an existing <directive type="section"
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna module="core">VirtualHost</directive> is handled by the global
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna server configuration, regardless of the hostname or ServerName.</p>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <p> When you add a name-based virtual host to an existing server, and
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna the virtual host arguments match preexisting IP and port combinations,
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna requests will now be handled by an explicit virtual host. In this case,
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna it's usually wise to create a <a href="#defaultvhost">default virtual host</a>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna with a <directive module="core">ServerName</directive> matching that of
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna the base server. New domains on the same interface and port, but
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna requiring separate configurations, can then be added as subsequent (non-default)
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna virtual hosts.</p>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna </note>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <p>For example, suppose that you are serving the domain
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <code>www.example.com</code> and you wish to add the virtual host
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <code>other.example.com</code>, which points at the same IP address.
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna Then you simply add the following to <code>httpd.conf</code>:</p>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <example>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna &lt;VirtualHost *:80&gt;<br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <indent>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna # This first-listed virtual host is also the default for *:80
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna ServerName www.example.com<br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna ServerAlias example.com <br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna DocumentRoot /www/domain<br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna </indent>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna &lt;/VirtualHost&gt;<br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna &lt;VirtualHost *:80&gt;<br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <indent>ServerName other.example.com<br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna DocumentRoot /www/otherdomain<br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna </indent>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna &lt;/VirtualHost&gt;<br />
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna </example>
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <p>You can alternatively specify an explicit IP address in place of the
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna <code>*</code> in <directive type="section" module="core"
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna >VirtualHost</directive> directives. For example, you might want to do this
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna in order to run some name-based virtual hosts on one IP address, and either
ea8572f0df1c69b398e9d677648a0fa5f31b2d60pquerna IP-based, or another set of name-based virtual hosts on another address.</p>
<p>Many servers want to be accessible by more than one name. This is
possible with the <directive module="core">ServerAlias</directive>
directive, placed inside the <directive type="section" module="core"
>VirtualHost</directive> section. For example in the first <directive
type="section" module="core">VirtualHost</directive> block above, the
<directive module="core">ServerAlias</directive> directive indicates that
the listed names are other names which people can use to see that same
web site:</p>
<example>
ServerAlias example.com *.example.com
</example>
<p>then requests for all hosts in the <code>example.com</code> domain will
be served by the <code>www.example.com</code> virtual host. The wildcard
characters <code>*</code> and <code>?</code> can be used to match names.
Of course, you can't just make up names and place them in <directive
module="core">ServerName</directive> or <code>ServerAlias</code>. You must
first have your DNS server properly configured to map those names to an IP
address associated with your server.</p>
<p>Name-based virtual hosts for the best-matching set of <directive
type="section" module="core">virtualhost</directive>s are processsed
in the order they appear in the configuration. The first matching <directive
module="core">ServerName</directive> or <directive module="core"
>ServerAlias</directive> is used, with no different precedence for wildcards
(nor for ServerName vs. ServerAlias). </p>
<p>Finally, you can fine-tune the configuration of the virtual hosts
by placing other directives inside the <directive type="section"
module="core">VirtualHost</directive> 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 <a href="/mod/directive-dict.html#Context">Context</a> of the
directive. Configuration directives set in the <em>main server context</em>
(outside any <directive type="section" module="core">VirtualHost</directive>
container) will be used only if they are not overridden by the virtual host
settings.</p>
</section>
</manualpage>