1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5              This file is generated from xml source: DO NOT EDIT
6        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7      -->
8<title>Name-based Virtual Host Support - Apache HTTP Server</title>
9<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="/style/css/prettify.css" />
12<script src="/style/scripts/prettify.min.js" type="text/javascript">
13</script>
14
15<link href="/images/favicon.ico" rel="shortcut icon" /></head>
16<body id="manual-page"><div id="page-header">
17<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p>
18<p class="apache">Apache HTTP Server Version 2.4</p>
19<img alt="" src="/images/feather.gif" /></div>
20<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
21<div id="path">
22<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.4</a> &gt; <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>Name-based Virtual Host Support</h1>
23<div class="toplang">
24<p><span>Available Languages: </span><a href="/de/vhosts/name-based.html" hreflang="de" rel="alternate" title="Deutsch">&nbsp;de&nbsp;</a> |
25<a href="/en/vhosts/name-based.html" title="English">&nbsp;en&nbsp;</a> |
26<a href="/fr/vhosts/name-based.html" hreflang="fr" rel="alternate" title="Fran�ais">&nbsp;fr&nbsp;</a> |
27<a href="/ja/vhosts/name-based.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
28<a href="/ko/vhosts/name-based.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
29<a href="/tr/vhosts/name-based.html" hreflang="tr" rel="alternate" title="T�rk�e">&nbsp;tr&nbsp;</a></p>
30</div>
31
32    <p>This document describes when and how to use name-based virtual hosts.</p>
33</div>
34<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#namevip">Name-based vs. IP-based Virtual Hosts</a></li>
35<li><img alt="" src="/images/down.gif" /> <a href="#alg">How the server selects the proper name-based virtual host</a></li>
36<li><img alt="" src="/images/down.gif" /> <a href="#using">Using Name-based Virtual Hosts</a></li>
37</ul><h3>See also</h3><ul class="seealso"><li><a href="ip-based.html">IP-based Virtual Host Support</a></li><li><a href="details.html">An In-Depth Discussion of Virtual Host Matching</a></li><li><a href="mass.html">Dynamically configured mass virtual hosting</a></li><li><a href="examples.html">Virtual Host examples for common setups</a></li></ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
38<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
39<div class="section">
40<h2><a name="namevip" id="namevip">Name-based vs. IP-based Virtual Hosts</a></h2>
41
42    <p><a href="ip-based.html">IP-based virtual hosts</a> use the IP address of the connection to
43    determine the correct virtual host to serve.  Therefore you need to
44    have a separate IP address for each host.</p>
45
46    <p>With name-based virtual hosting, the server relies on the client to
47    report the hostname as part of the HTTP headers.  Using this technique,
48    many different hosts can share the same IP address.</p>
49
50    <p>Name-based virtual hosting is usually simpler, since you need
51    only configure your DNS server to map each hostname to the correct
52    IP address and then configure the Apache HTTP Server to recognize
53    the different hostnames. Name-based virtual hosting also eases
54    the demand for scarce IP addresses. Therefore you should use
55    name-based virtual hosting unless you are using equipment
56    that explicitly demands IP-based hosting.  Historical reasons for
57    IP-based virtual hosting based on client support are no longer
58    applicable to a general-purpose web server.</p>
59
60    <p> Name-based virtual hosting builds off of the IP-based virtual host
61    selection algorithm, meaning that searches for the proper server name
62    occur only between virtual hosts that have the best IP-based address.</p>
63
64</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
65<div class="section">
66<h2><a name="alg" id="alg">How the server selects the proper name-based virtual host</a></h2>
67
68    <p>It is important to recognize that the first step in name-based virtual
69    host resolution is IP-based resolution.  Name-based virtual host
70    resolution only chooses the most appropriate name-based virtual host
71    after narrowing down the candidates to the best IP-based match.  Using a wildcard (*)
72    for the IP address in all of the VirtualHost directives makes this
73    IP-based mapping irrelevant.</p>
74
75    <p>When a request arrives, the server will find the best (most specific) matching
76    <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> argument based on
77    the IP address and port used by the request.  If there is more than one virtual host
78    containing this best-match address and port combination, Apache will further
79    compare the <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code> and <code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code> directives to the server name
80    present in the request.</p>
81
82    <h3><a name="defaultvhost" id="defaultvhost">The default name-based vhost for an IP and port combination </a></h3>
83    <p> If no matching ServerName or ServerAlias is found in the set of
84    virtual hosts containing the most specific matching IP address and port
85    combination, then <strong>the first listed virtual host</strong> that
86    matches that will be used.</p>
87</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
88<div class="section">
89<h2><a name="using" id="using">Using Name-based Virtual Hosts</a></h2>
90
91<table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="/mod/core.html">core</a></code></li></ul></td><td><ul><li><code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code></li><li><code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code></li><li><code class="directive"><a href="/mod/core.html#servername">ServerName</a></code></li><li><code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code></li></ul></td></tr></table>
92
93    <p>The first step is to create a <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> block for
94    each different host that you would like to serve.  Inside each <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> block, you will need at minimum a
95    <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code> directive to designate
96    which host is served and a <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code>
97    directive to show where in the filesystem the content for that host
98    lives.</p>
99
100    <div class="note"><h3>Main host goes away</h3>
101        <p> Any request that doesn't match an existing <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> is handled by the global
102        server configuration, regardless of the hostname or ServerName.</p>
103
104        <p> When you add a name-based virtual host to an existing server, and
105        the virtual host arguments match preexisting IP and port combinations,
106        requests will now be handled by an explicit virtual host.  In this case,
107        it's usually wise to create a <a href="#defaultvhost">default virtual host</a>
108        with a <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code> matching that of
109        the base server.  New domains on the same interface and port, but
110        requiring separate configurations,  can then be added as subsequent (non-default)
111        virtual hosts.</p>
112    </div>
113
114    <div class="note"><h3>ServerName inheritance</h3>
115       <p> It is best to always explicitly list a  <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code> in every name-based virtual host.</p>
116       <p>If a <code class="directive"><a href="/mod/core.html#virtualhost">VirtualHost</a></code> doesn't specify 
117       a <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code>, a server name will be 
118       inherited from the base server configuration.  If no server name was 
119       specified globally, one is detected at startup through reverse DNS resolution
120       of the first listening address.  In either case, this inherited server name
121       will influenced name-based virtual host resolution, so it is best to always
122       explicitly list a  <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code> in every
123       name-based virtual host.</p>
124    </div>
125
126    <p>For example, suppose that you are serving the domain
127    <code>www.example.com</code> and you wish to add the virtual host
128    <code>other.example.com</code>, which points at the same IP address.
129    Then you simply add the following to <code>httpd.conf</code>:</p>
130
131    <pre class="prettyprint lang-config">&lt;VirtualHost *:80&gt;
132    # This first-listed virtual host is also the default for *:80
133    ServerName www.example.com
134    ServerAlias example.com 
135    DocumentRoot /www/domain
136&lt;/VirtualHost&gt;
137
138&lt;VirtualHost *:80&gt;
139    ServerName other.example.com
140    DocumentRoot /www/otherdomain
141&lt;/VirtualHost&gt;</pre>
142
143
144    <p>You can alternatively specify an explicit IP address in place of the
145    <code>*</code> in <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> directives. For example, you might want to do this
146    in order to run some name-based virtual hosts on one IP address, and either
147    IP-based, or another set of name-based virtual hosts on another address.</p>
148
149    <p>Many servers want to be accessible by more than one name. This is
150    possible with the <code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code>
151    directive, placed inside the <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> section. For example in the first <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> block above, the
152    <code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code> directive indicates that
153    the listed names are other names which people can use to see that same
154    web site:</p>
155
156    <pre class="prettyprint lang-config">ServerAlias example.com *.example.com</pre>
157
158
159    <p>then requests for all hosts in the <code>example.com</code> domain will
160    be served by the <code>www.example.com</code> virtual host. The wildcard
161    characters <code>*</code> and <code>?</code> can be used to match names.
162    Of course, you can't just make up names and place them in <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code> or <code>ServerAlias</code>. You must
163    first have your DNS server properly configured to map those names to an IP
164    address associated with your server.</p>
165
166    <p>Name-based virtual hosts for the best-matching set of  <code class="directive"><a href="/mod/core.html#virtualhost">&lt;virtualhost&gt;</a></code>s are processed 
167    in the order they appear in the configuration.  The first matching <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code> or <code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code> is used, with no different precedence for wildcards
168    (nor for ServerName vs. ServerAlias).  </p>
169
170    <p>The complete list of names in the <code class="directive"><a href="/mod/core.html#virtualhost">VirtualHost</a></code>
171    directive are treated just like a (non wildcard) 
172    <code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code>.</p>
173
174    <p>Finally, you can fine-tune the configuration of the virtual hosts
175    by placing other directives inside the <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> containers. Most directives can be
176    placed in these containers and will then change the configuration only of
177    the relevant virtual host. To find out if a particular directive is allowed,
178    check the <a href="/mod/directive-dict.html#Context">Context</a> of the
179    directive. Configuration directives set in the <em>main server context</em>
180    (outside any <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code>
181    container) will be used only if they are not overridden by the virtual host
182    settings.</p>
183
184</div></div>
185<div class="bottomlang">
186<p><span>Available Languages: </span><a href="/de/vhosts/name-based.html" hreflang="de" rel="alternate" title="Deutsch">&nbsp;de&nbsp;</a> |
187<a href="/en/vhosts/name-based.html" title="English">&nbsp;en&nbsp;</a> |
188<a href="/fr/vhosts/name-based.html" hreflang="fr" rel="alternate" title="Fran�ais">&nbsp;fr&nbsp;</a> |
189<a href="/ja/vhosts/name-based.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
190<a href="/ko/vhosts/name-based.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
191<a href="/tr/vhosts/name-based.html" hreflang="tr" rel="alternate" title="T�rk�e">&nbsp;tr&nbsp;</a></p>
192</div><div class="top"><a href="#page-header"><img src="/images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
193<script type="text/javascript"><!--//--><![CDATA[//><!--
194var comments_shortname = 'httpd';
195var comments_identifier = 'http://httpd.apache.org/docs/2.4/vhosts/name-based.html';
196(function(w, d) {
197    if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
198        d.write('<div id="comments_thread"><\/div>');
199        var s = d.createElement('script');
200        s.type = 'text/javascript';
201        s.async = true;
202        s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
203        (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
204    }
205    else { 
206        d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
207    }
208})(window, document);
209//--><!]]></script></div><div id="footer">
210<p class="apache">Copyright 2014 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
211<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
212if (typeof(prettyPrint) !== 'undefined') {
213    prettyPrint();
214}
215//--><!]]></script>
216</body></html>