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
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" />
12 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
15 <p class="apache">Apache HTTP Server Version 2.0</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
19 <a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.0</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>Name-based Virtual Host Support</h1>
21 <p><span>Available Languages: </span><a href="../de/vhosts/name-based.html" hreflang="de" rel="alternate" title="Deutsch"> de </a> |
22 <a href="../en/vhosts/name-based.html" title="English"> en </a> |
23 <a href="../es/vhosts/name-based.html" hreflang="es" rel="alternate" title="Español"> es </a> |
24 <a href="../ja/vhosts/name-based.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
25 <a href="../ko/vhosts/name-based.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
26 <a href="../tr/vhosts/name-based.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
29 <p>This document describes when and how to use name-based virtual hosts.</p>
31 <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>
32 <li><img alt="" src="../images/down.gif" /> <a href="#using">Using Name-based Virtual Hosts</a></li>
33 <li><img alt="" src="../images/down.gif" /> <a href="#compat">Compatibility with Older Browsers</a></li>
34 </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><li><a href="examples.html#serverpath">ServerPath configuration example</a></li></ul></div>
35 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
37 <h2><a name="namevip" id="namevip">Name-based vs. IP-based Virtual Hosts</a></h2>
39 <p>IP-based virtual hosts use the IP address of the connection to
40 determine the correct virtual host to serve. Therefore you need to
41 have a separate IP address for each host. With name-based virtual
42 hosting, the server relies on the client to report the hostname as
43 part of the HTTP headers. Using this technique, many different hosts
44 can share the same IP address.</p>
46 <p>Name-based virtual hosting is usually simpler, since you need
47 only configure your DNS server to map each hostname to the correct
48 IP address and then configure the Apache HTTP Server to recognize
49 the different hostnames. Name-based virtual hosting also eases
50 the demand for scarce IP addresses. Therefore you should use
51 name-based virtual hosting unless there is a specific reason to
52 choose IP-based virtual hosting. Some reasons why you might consider
53 using IP-based virtual hosting:</p>
56 <li>Some ancient clients are not compatible with name-based virtual
57 hosting. For name-based virtual hosting to work, the client must send
58 the HTTP Host header. This is required by HTTP/1.1, and is
59 implemented by all modern HTTP/1.0 browsers as an extension. If you
60 need to support obsolete clients and still use name-based virtual
61 hosting, a possible technique is discussed at the end of this
64 <li>Name-based virtual hosting cannot be used with SSL secure servers
65 because of the nature of the SSL protocol.</li>
67 <li>Some operating systems and network equipment implement bandwidth
68 management techniques that cannot differentiate between hosts unless
69 they are on separate IP addresses.</li>
72 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
74 <h2><a name="using" id="using">Using Name-based Virtual Hosts</a></h2>
76 <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#namevirtualhost">NameVirtualHost</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#serverpath">ServerPath</a></code></li><li><code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code></li></ul></td></tr></table>
78 <p>To use name-based virtual hosting, you must designate the IP
79 address (and possibly port) on the server that will be accepting
80 requests for the hosts. This is configured using the <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code> directive.
81 In the normal case where any and all IP addresses on the server should
82 be used, you can use <code>*</code> as the argument to <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code>. If you're planning to use
83 multiple ports (e.g. running SSL) you should add a Port to the argument,
84 such as <code>*:80</code>. Note that mentioning an IP address in a
85 <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code> directive does not
86 automatically make the server listen to that IP address. See
87 <a href="../bind.html">Setting which addresses and ports Apache uses</a>
88 for more details. In addition, any IP address specified here must be
89 associated with a network interface on the server.</p>
91 <p>The next step is to create a <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> block for
92 each different host that you would like to serve. The argument to the
93 <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> directive
94 should be the same as the argument to the <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code> directive (ie, an IP address,
95 or <code>*</code> for all addresses). Inside each <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> block, you will need at minimum a
96 <code class="directive"><a href="../mod/core.html#servername">ServerName</a></code> directive to designate
97 which host is served and a <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
98 directive to show where in the filesystem the content for that host
101 <div class="note"><h3>Main host goes away</h3>
102 <p>If you are adding virtual hosts to an existing web server, you
103 must also create a <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> block for the existing host. The <code class="directive"><a href="../mod/core.html#servername">ServerName</a></code> and <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> included in this virtual host should be the
104 same as the global <code class="directive"><a href="../mod/core.html#servername">ServerName</a></code> and
105 <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>. List this virtual
106 host first in the configuration file so that it will act as the default
110 <p>For example, suppose that you are serving the domain
111 <code>www.domain.tld</code> and you wish to add the virtual host
112 <code>www.otherdomain.tld</code>, which points at the same IP address.
113 Then you simply add the following to <code>httpd.conf</code>:</p>
115 <div class="example"><p><code>
116 NameVirtualHost *:80<br />
118 <VirtualHost *:80><br />
119 <span class="indent">
120 ServerName www.domain.tld<br />
121 ServerAlias domain.tld *.domain.tld<br />
122 DocumentRoot /www/domain<br />
124 </VirtualHost><br />
126 <VirtualHost *:80><br />
127 <span class="indent">ServerName www.otherdomain.tld<br />
128 DocumentRoot /www/otherdomain<br />
130 </VirtualHost><br />
133 <p>You can alternatively specify an explicit IP address in place of the
134 <code>*</code> in both the <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code> and <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> directives. For example, you might want to do this
135 in order to run some name-based virtual hosts on one IP address, and either
136 IP-based, or another set of name-based virtual hosts on another address.</p>
138 <p>Many servers want to be accessible by more than one name. This is
139 possible with the <code class="directive"><a href="../mod/core.html#serveralias">ServerAlias</a></code>
140 directive, placed inside the <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> section. For example in the first <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> block above, the
141 <code class="directive"><a href="../mod/core.html#serveralias">ServerAlias</a></code> directive indicates that
142 the listed names are other names which people can use to see that same
145 <div class="example"><p><code>
146 ServerAlias domain.tld *.domain.tld
149 <p>then requests for all hosts in the <code>domain.tld</code> domain will
150 be served by the <code>www.domain.tld</code> virtual host. The wildcard
151 characters <code>*</code> and <code>?</code> can be used to match names.
152 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
153 first have your DNS server properly configured to map those names to an IP
154 address associated with your server.</p>
156 <p>Finally, you can fine-tune the configuration of the virtual hosts
157 by placing other directives inside the <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> containers. Most directives can be
158 placed in these containers and will then change the configuration only of
159 the relevant virtual host. To find out if a particular directive is allowed,
160 check the <a href="../mod/directive-dict.html#Context">Context</a> of the
161 directive. Configuration directives set in the <em>main server context</em>
162 (outside any <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>
163 container) will be used only if they are not overridden by the virtual host
166 <p>Now when a request arrives, the server will first check if it is using
167 an IP address that matches the <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code>. If it is, then it will look at each <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> section with a matching
168 IP address and try to find one where the <code class="directive"><a href="../mod/core.html#servername">ServerName</a></code> or <code>ServerAlias</code> matches the requested
169 hostname. If it finds one, then it uses the configuration for that server.
170 If no matching virtual host is found, then <strong>the first listed virtual
171 host</strong> that matches the IP address will be used.</p>
173 <p>As a consequence, the first listed virtual host is the <em>default</em>
174 virtual host. The <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> from
175 the <em>main server</em> will <strong>never</strong> be used when an IP
176 address matches the <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code>
177 directive. If you would like to have a special configuration for requests
178 that do not match any particular virtual host, simply put that configuration
179 in a <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>
180 container and list it first in the configuration file.</p>
182 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
183 <div class="section">
184 <h2><a name="compat" id="compat">Compatibility with Older Browsers</a></h2>
186 <p>As mentioned earlier, there are some clients
187 who do not send the required data for the name-based virtual
188 hosts to work properly. These clients will always be sent the
189 pages from the first virtual host listed for that IP address
190 (the <cite>primary</cite> name-based virtual host).</p>
192 <div class="note"><h3>How much older?</h3>
193 <p>Please note that when we say older, we really do mean older. You are
194 very unlikely to encounter one of these browsers in use today. All
195 current versions of any browser send the <code>Host</code> header as
196 required for name-based virtual hosts.</p>
199 <p>There is a possible workaround with the <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>
200 directive, albeit a slightly cumbersome one:</p>
202 <p>Example configuration:</p>
204 <div class="example"><p><code>
205 NameVirtualHost 111.22.33.44<br />
207 <VirtualHost 111.22.33.44><br />
208 <span class="indent">
209 ServerName www.domain.tld<br />
210 ServerPath /domain<br />
211 DocumentRoot /web/domain<br />
213 </VirtualHost><br />
216 <p>What does this mean? It means that a request for any URI
217 beginning with "<code>/domain</code>" will be served from the
218 virtual host <code>www.domain.tld</code>. This means that the
219 pages can be accessed as <code>http://www.domain.tld/domain/</code>
220 for all clients, although clients sending a <code>Host:</code> header
221 can also access it as <code>http://www.domain.tld/</code>.</p>
223 <p>In order to make this work, put a link on your primary
224 virtual host's page to
225 <code>http://www.domain.tld/domain/</code>. Then, in the virtual
226 host's pages, be sure to use either purely relative links
227 (<em>e.g.</em>, "<code>file.html</code>" or
228 "<code>../icons/image.gif</code>") or links containing the
229 prefacing <code>/domain/</code> (<em>e.g.</em>,
230 "<code>http://www.domain.tld/domain/misc/file.html</code>" or
231 "<code>/domain/misc/file.html</code>").</p>
233 <p>This requires a bit of discipline, but adherence to these
234 guidelines will, for the most part, ensure that your pages will
235 work with all browsers, new and old.</p>
238 <div class="bottomlang">
239 <p><span>Available Languages: </span><a href="../de/vhosts/name-based.html" hreflang="de" rel="alternate" title="Deutsch"> de </a> |
240 <a href="../en/vhosts/name-based.html" title="English"> en </a> |
241 <a href="../es/vhosts/name-based.html" hreflang="es" rel="alternate" title="Español"> es </a> |
242 <a href="../ja/vhosts/name-based.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
243 <a href="../ko/vhosts/name-based.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
244 <a href="../tr/vhosts/name-based.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
245 </div><div id="footer">
246 <p class="apache">Copyright 2009 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>
247 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>