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 <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7 This file is generated from xml source: DO NOT EDIT
8 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10 <title>VirtualHost Examples - Apache HTTP Server Version 2.5</title>
11 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
12 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
13 <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" />
14 <script src="../style/scripts/prettify.min.js" type="text/javascript">
17 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
18 <body id="manual-page"><div id="page-header">
19 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.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>
20 <p class="apache">Apache HTTP Server Version 2.5</p>
21 <img alt="" src="../images/feather.png" /></div>
22 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
24 <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.5</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>VirtualHost Examples</h1>
26 <p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
27 <a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Français"> fr </a> |
28 <a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
29 <a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
30 <a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
34 <p>This document attempts to answer the commonly-asked questions about
35 setting up <a href="index.html">virtual hosts</a>. These scenarios are those involving multiple
36 web sites running on a single server, via <a href="name-based.html">name-based</a> or <a href="ip-based.html">IP-based</a> virtual hosts.
40 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#purename">Running several name-based web
41 sites on a single IP address.</a></li>
42 <li><img alt="" src="../images/down.gif" /> <a href="#twoips">Name-based hosts on more than one
44 <li><img alt="" src="../images/down.gif" /> <a href="#intraextra">Serving the same content on
45 different IP addresses (such as an internal and external
47 <li><img alt="" src="../images/down.gif" /> <a href="#port">Running different sites on different
49 <li><img alt="" src="../images/down.gif" /> <a href="#ip">IP-based virtual hosting</a></li>
50 <li><img alt="" src="../images/down.gif" /> <a href="#ipport">Mixed port-based and ip-based virtual
52 <li><img alt="" src="../images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based
54 <li><img alt="" src="../images/down.gif" /> <a href="#proxy">Using <code>Virtual_host</code> and
55 mod_proxy together</a></li>
56 <li><img alt="" src="../images/down.gif" /> <a href="#default">Using <code>_default_</code>
58 <li><img alt="" src="../images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an
59 IP-based vhost</a></li>
60 <li><img alt="" src="../images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code>
62 </ul><h3>See also</h3><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
63 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
65 <h2><a name="purename" id="purename">Running several name-based web
66 sites on a single IP address.</a></h2>
68 <p>Your server has multiple hostnames that resolve to a single address,
69 and you want to respond differently for <code>www.example.com</code>
70 and <code>www.example.org</code>.</p>
72 <div class="note"><h3>Note</h3><p>Creating virtual
73 host configurations on your Apache server does not magically
74 cause DNS entries to be created for those host names. You
75 <em>must</em> have the names in DNS, resolving to your IP
76 address, or nobody else will be able to see your web site. You
77 can put entries in your <code>hosts</code> file for local
78 testing, but that will work only from the machine with those
79 <code>hosts</code> entries.</p>
82 <pre class="prettyprint lang-config"># Ensure that Apache listens on port 80
84 <VirtualHost *:80>
85 DocumentRoot "/www/example1"
86 ServerName www.example.com
88 # Other directives here
91 <VirtualHost *:80>
92 DocumentRoot "/www/example2"
93 ServerName www.example.org
95 # Other directives here
96 </VirtualHost></pre>
99 <p>The asterisks match all addresses, so the main server serves no
100 requests. Due to the fact that the virtual host with
101 <code>ServerName www.example.com</code> is first
102 in the configuration file, it has the highest priority and can be seen
103 as the <cite>default</cite> or <cite>primary</cite> server. That means
104 that if a request is received that does not match one of the specified
105 <code class="directive"><a href="../mod/core.html#servername">ServerName</a></code> directives, it will be served by this first
106 <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>.</p>
108 <p>The above configuration is what you will want to use in almost
109 all name-based virtual hosting situations. The only thing that this
110 configuration will not work for, in fact, is when you are serving
111 different content based on differing IP addresses or ports.</p>
116 <p>You may replace <code>*</code> with a specific IP address
117 on the system. Such virtual hosts will only be used for
118 HTTP requests received on connection to the specified IP
121 <p>However, it is additionally useful to use <code>*</code>
122 on systems where the IP address is not predictable - for
123 example if you have a dynamic IP address with your ISP, and
124 you are using some variety of dynamic DNS solution. Since
125 <code>*</code> matches any IP address, this configuration
126 would work without changes whenever your IP address
129 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
130 <div class="section">
131 <h2><a name="twoips" id="twoips">Name-based hosts on more than one
136 <p>Any of the techniques discussed here can be extended to any
137 number of IP addresses.</p>
140 <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
141 will serve the "main" server, <code>server.example.com</code> and on the
142 other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
144 <pre class="prettyprint lang-config">Listen 80
146 # This is the "main" server running on 172.20.30.40
147 ServerName server.example.com
148 DocumentRoot "/www/mainserver"
150 <VirtualHost 172.20.30.50>
151 DocumentRoot "/www/example1"
152 ServerName www.example.com
154 # Other directives here ...
157 <VirtualHost 172.20.30.50>
158 DocumentRoot "/www/example2"
159 ServerName www.example.org
161 # Other directives here ...
162 </VirtualHost></pre>
165 <p>Any request to an address other than <code>172.20.30.50</code> will be
166 served from the main server. A request to <code>172.20.30.50</code> with an
167 unknown hostname, or no <code>Host:</code> header, will be served from
168 <code>www.example.com</code>.</p>
170 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
171 <div class="section">
172 <h2><a name="intraextra" id="intraextra">Serving the same content on
173 different IP addresses (such as an internal and external
176 <p>The server machine has two IP addresses (<code>192.168.1.1</code>
177 and <code>172.20.30.40</code>). The machine is sitting between an
178 internal (intranet) network and an external (internet) network. Outside
179 of the network, the name <code>server.example.com</code> resolves to
180 the external address (<code>172.20.30.40</code>), but inside the
181 network, that same name resolves to the internal address
182 (<code>192.168.1.1</code>).</p>
184 <p>The server can be made to respond to internal and external requests
185 with the same content, with just one <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> section.</p>
187 <pre class="prettyprint lang-config"><VirtualHost 192.168.1.1 172.20.30.40>
188 DocumentRoot "/www/server1"
189 ServerName server.example.com
191 </VirtualHost></pre>
194 <p>Now requests from both networks will be served from the same
195 <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>.</p>
198 <h3>Note:</h3><p>On the internal
199 network, one can just use the name <code>server</code> rather
200 than the fully qualified host name
201 <code>server.example.com</code>.</p>
203 <p>Note also that, in the above example, you can replace the list
204 of IP addresses with <code>*</code>, which will cause the server to
205 respond the same on all addresses.</p>
208 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
209 <div class="section">
210 <h2><a name="port" id="port">Running different sites on different
213 <p>You have multiple domains going to the same IP and also want to
214 serve multiple ports. The example below illustrates that the name-matching
215 takes place after the best matching IP address and port combination
218 <pre class="prettyprint lang-config">Listen 80
221 <VirtualHost 172.20.30.40:80>
222 ServerName www.example.com
223 DocumentRoot "/www/domain-80"
226 <VirtualHost 172.20.30.40:8080>
227 ServerName www.example.com
228 DocumentRoot "/www/domain-8080"
231 <VirtualHost 172.20.30.40:80>
232 ServerName www.example.org
233 DocumentRoot "/www/otherdomain-80"
236 <VirtualHost 172.20.30.40:8080>
237 ServerName www.example.org
238 DocumentRoot "/www/otherdomain-8080"
239 </VirtualHost></pre>
242 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
243 <div class="section">
244 <h2><a name="ip" id="ip">IP-based virtual hosting</a></h2>
246 <p>The server has two IP addresses (<code>172.20.30.40</code> and
247 <code>172.20.30.50</code>) which resolve to the names
248 <code>www.example.com</code> and <code>www.example.org</code>
251 <pre class="prettyprint lang-config">Listen 80
253 <VirtualHost 172.20.30.40>
254 DocumentRoot "/www/example1"
255 ServerName www.example.com
258 <VirtualHost 172.20.30.50>
259 DocumentRoot "/www/example2"
260 ServerName www.example.org
261 </VirtualHost></pre>
264 <p>Requests for any address not specified in one of the
265 <code><VirtualHost></code> directives (such as
266 <code>localhost</code>, for example) will go to the main server, if
269 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
270 <div class="section">
271 <h2><a name="ipport" id="ipport">Mixed port-based and ip-based virtual
274 <p>The server machine has two IP addresses (<code>172.20.30.40</code> and
275 <code>172.20.30.50</code>) which resolve to the names
276 <code>www.example.com</code> and <code>www.example.org</code>
277 respectively. In each case, we want to run hosts on ports 80 and
280 <pre class="prettyprint lang-config">Listen 172.20.30.40:80
281 Listen 172.20.30.40:8080
282 Listen 172.20.30.50:80
283 Listen 172.20.30.50:8080
285 <VirtualHost 172.20.30.40:80>
286 DocumentRoot "/www/example1-80"
287 ServerName www.example.com
290 <VirtualHost 172.20.30.40:8080>
291 DocumentRoot "/www/example1-8080"
292 ServerName www.example.com
295 <VirtualHost 172.20.30.50:80>
296 DocumentRoot "/www/example2-80"
297 ServerName www.example.org
300 <VirtualHost 172.20.30.50:8080>
301 DocumentRoot "/www/example2-8080"
302 ServerName www.example.org
303 </VirtualHost></pre>
306 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
307 <div class="section">
308 <h2><a name="mixed" id="mixed">Mixed name-based and IP-based
311 <p>Any address mentioned in the argument to a virtualhost that never
312 appears in another virtual host is a strictly IP-based virtual host.</p>
314 <pre class="prettyprint lang-config">Listen 80
315 <VirtualHost 172.20.30.40>
316 DocumentRoot "/www/example1"
317 ServerName www.example.com
320 <VirtualHost 172.20.30.40>
321 DocumentRoot "/www/example2"
322 ServerName www.example.org
325 <VirtualHost 172.20.30.40>
326 DocumentRoot "/www/example3"
327 ServerName www.example.net
331 <VirtualHost 172.20.30.50>
332 DocumentRoot "/www/example4"
333 ServerName www.example.edu
336 <VirtualHost 172.20.30.60>
337 DocumentRoot "/www/example5"
338 ServerName www.example.gov
339 </VirtualHost></pre>
342 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
343 <div class="section">
344 <h2><a name="proxy" id="proxy">Using <code>Virtual_host</code> and
345 mod_proxy together</a></h2>
347 <p>The following example allows a front-end machine to proxy a
348 virtual host through to a server running on another machine. In the
349 example, a virtual host of the same name is configured on a machine
350 at <code>192.168.111.2</code>. The <code class="directive"><a href="../mod/mod_proxy.html#proxypreservehost">ProxyPreserveHost
351 On</a></code> directive is used so that the desired hostname is
352 passed through, in case we are proxying multiple hostnames to a
355 <pre class="prettyprint lang-config"><VirtualHost *:*>
357 ProxyPass "/" "http://192.168.111.2/"
358 ProxyPassReverse "/" "http://192.168.111.2/"
359 ServerName hostname.example.com
360 </VirtualHost></pre>
363 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
364 <div class="section">
365 <h2><a name="default" id="default">Using <code>_default_</code>
368 <h3><a name="defaultallports" id="defaultallports"><code>_default_</code> vhosts
369 for all ports</a></h3>
371 <p>Catching <em>every</em> request to any unspecified IP address and
372 port, <em>i.e.</em>, an address/port combination that is not used for
373 any other virtual host.</p>
375 <pre class="prettyprint lang-config"><VirtualHost _default_:*>
376 DocumentRoot "/www/default"
377 </VirtualHost></pre>
380 <p>Using such a default vhost with a wildcard port effectively prevents
381 any request going to the main server.</p>
383 <p>A default vhost never serves a request that was sent to an
384 address/port that is used for name-based vhosts. If the request
385 contained an unknown or no <code>Host:</code> header it is always
386 served from the primary name-based vhost (the vhost for that
387 address/port appearing first in the configuration file).</p>
389 <p>You can use <code class="directive"><a href="../mod/mod_alias.html#aliasmatch">AliasMatch</a></code> or
390 <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to rewrite any
391 request to a single information page (or script).</p>
394 <h3><a name="defaultdifferentports" id="defaultdifferentports"><code>_default_</code> vhosts
395 for different ports</a></h3>
397 <p>Same as setup 1, but the server listens on several ports and we want
398 to use a second <code>_default_</code> vhost for port 80.</p>
400 <pre class="prettyprint lang-config"><VirtualHost _default_:80>
401 DocumentRoot "/www/default80"
405 <VirtualHost _default_:*>
406 DocumentRoot "/www/default"
408 </VirtualHost></pre>
411 <p>The default vhost for port 80 (which <em>must</em> appear before any
412 default vhost with a wildcard port) catches all requests that were sent
413 to an unspecified IP address. The main server is never used to serve a
417 <h3><a name="defaultoneport" id="defaultoneport"><code>_default_</code> vhosts
418 for one port</a></h3>
420 <p>We want to have a default vhost for port 80, but no other default
423 <pre class="prettyprint lang-config"><VirtualHost _default_:80>
424 DocumentRoot "/www/default"
426 </VirtualHost></pre>
429 <p>A request to an unspecified address on port 80 is served from the
430 default vhost. Any other request to an unspecified address and port is
431 served from the main server.</p>
433 <p>Any use of <code>*</code> in a virtual host declaration will have
434 higher precedence than <code>_default_</code>.</p>
438 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
439 <div class="section">
440 <h2><a name="migrate" id="migrate">Migrating a name-based vhost to an
441 IP-based vhost</a></h2>
443 <p>The name-based vhost with the hostname
444 <code>www.example.org</code> (from our <a href="#name">name-based</a> example, setup 2) should get its own IP
445 address. To avoid problems with name servers or proxies who cached the
446 old IP address for the name-based vhost we want to provide both
447 variants during a migration phase.</p>
450 The solution is easy, because we can simply add the new IP address
451 (<code>172.20.30.50</code>) to the <code>VirtualHost</code>
454 <pre class="prettyprint lang-config">Listen 80
455 ServerName www.example.com
456 DocumentRoot "/www/example1"
458 <VirtualHost 172.20.30.40 172.20.30.50>
459 DocumentRoot "/www/example2"
460 ServerName www.example.org
464 <VirtualHost 172.20.30.40>
465 DocumentRoot "/www/example3"
466 ServerName www.example.net
467 ServerAlias *.example.net
469 </VirtualHost></pre>
472 <p>The vhost can now be accessed through the new address (as an
473 IP-based vhost) and through the old address (as a name-based
476 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
477 <div class="section">
478 <h2><a name="serverpath" id="serverpath">Using the <code>ServerPath</code>
481 <p>We have a server with two name-based vhosts. In order to match the
482 correct virtual host a client must send the correct <code>Host:</code>
483 header. Old HTTP/1.0 clients do not send such a header and Apache has
484 no clue what vhost the client tried to reach (and serves the request
485 from the primary vhost). To provide as much backward compatibility as
486 possible we create a primary vhost which returns a single page
487 containing links with an URL prefix to the name-based virtual
490 <pre class="prettyprint lang-config"><VirtualHost 172.20.30.40>
492 DocumentRoot "/www/subdomain"
494 RewriteRule "." "/www/subdomain/index.html"
498 <VirtualHost 172.20.30.40>
499 DocumentRoot "/www/subdomain/sub1"
500 ServerName www.sub1.domain.tld
503 RewriteRule "^(/sub1/.*)" "/www/subdomain$1"
507 <VirtualHost 172.20.30.40>
508 DocumentRoot "/www/subdomain/sub2"
509 ServerName www.sub2.domain.tld
512 RewriteRule "^(/sub2/.*)" "/www/subdomain$1"
514 </VirtualHost></pre>
517 <p>Due to the <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>
518 directive a request to the URL
519 <code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
520 from the sub1-vhost.<br /> A request to the URL
521 <code>http://www.sub1.domain.tld/</code> is only
522 served from the sub1-vhost if the client sent a correct
523 <code>Host:</code> header. If no <code>Host:</code> header is sent the
524 client gets the information page from the primary host.</p>
526 <p>Please note that there is one oddity: A request to
527 <code>http://www.sub2.domain.tld/sub1/</code> is also served from the
528 sub1-vhost if the client sent no <code>Host:</code> header.</p>
530 <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives
531 are used to make sure that a client which sent a correct
532 <code>Host:</code> header can use both URL variants, <em>i.e.</em>,
533 with or without URL prefix.</p>
536 <div class="bottomlang">
537 <p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
538 <a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Français"> fr </a> |
539 <a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
540 <a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
541 <a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
542 </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&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>
543 <script type="text/javascript"><!--//--><![CDATA[//><!--
544 var comments_shortname = 'httpd';
545 var comments_identifier = 'http://httpd.apache.org/docs/trunk/vhosts/examples.html';
547 if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
548 d.write('<div id="comments_thread"><\/div>');
549 var s = d.createElement('script');
550 s.type = 'text/javascript';
552 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
553 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
556 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
558 })(window, document);
559 //--><!]]></script></div><div id="footer">
560 <p class="apache">Copyright 2016 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>
561 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.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[//><!--
562 if (typeof(prettyPrint) !== 'undefined') {