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>VirtualHost Examples - 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.3</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.3</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>VirtualHost Examples</h1>
21 <p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
22 <a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Français"> fr </a> |
23 <a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
24 <a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
25 <a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
29 <p>This document attempts to answer the commonly-asked questions about
30 setting up <a href="index.html">virtual hosts</a>. These scenarios are those involving multiple
31 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.
35 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#purename">Running several name-based web
36 sites on a single IP address.</a></li>
37 <li><img alt="" src="../images/down.gif" /> <a href="#twoips">Name-based hosts on more than one
39 <li><img alt="" src="../images/down.gif" /> <a href="#intraextra">Serving the same content on
40 different IP addresses (such as an internal and external
42 <li><img alt="" src="../images/down.gif" /> <a href="#port">Running different sites on different
44 <li><img alt="" src="../images/down.gif" /> <a href="#ip">IP-based virtual hosting</a></li>
45 <li><img alt="" src="../images/down.gif" /> <a href="#ipport">Mixed port-based and ip-based virtual
47 <li><img alt="" src="../images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based
49 <li><img alt="" src="../images/down.gif" /> <a href="#proxy">Using <code>Virtual_host</code> and
50 mod_proxy together</a></li>
51 <li><img alt="" src="../images/down.gif" /> <a href="#default">Using <code>_default_</code>
53 <li><img alt="" src="../images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an
54 IP-based vhost</a></li>
55 <li><img alt="" src="../images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code>
58 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
60 <h2><a name="purename" id="purename">Running several name-based web
61 sites on a single IP address.</a></h2>
63 <p>Your server has a single IP address, and multiple aliases (CNAMES)
64 point to this machine in DNS. You want to run a web server for
65 <code>www.example.com</code> and <code>www.example.org</code> on this
68 <div class="note"><h3>Note</h3><p>Creating virtual
69 host configurations on your Apache server does not magically
70 cause DNS entries to be created for those host names. You
71 <em>must</em> have the names in DNS, resolving to your IP
72 address, or nobody else will be able to see your web site. You
73 can put entries in your <code>hosts</code> file for local
74 testing, but that will work only from the machine with those
75 <code>hosts</code> entries.</p>
78 <div class="example"><h3>Server configuration</h3><p><code>
81 # Ensure that Apache listens on port 80<br />
84 # Listen for virtual host requests on all IP addresses<br />
85 NameVirtualHost *:80<br />
87 <VirtualHost *:80><br />
89 DocumentRoot /www/example1<br />
90 ServerName www.example.com<br />
92 # Other directives here<br />
95 </VirtualHost><br />
97 <VirtualHost *:80><br />
99 DocumentRoot /www/example2<br />
100 ServerName www.example.org<br />
102 # Other directives here<br />
108 <p>The asterisks match all addresses, so the main server serves no
109 requests. Due to the fact that <code>www.example.com</code> is first
110 in the configuration file, it has the highest priority and can be seen
111 as the <cite>default</cite> or <cite>primary</cite> server. That means
112 that if a request is received that does not match one of the specified
113 <code>ServerName</code> directives, it will be served by this first
114 <code>VirtualHost</code>.</p>
119 <p>You can, if you wish, replace <code>*</code> with the actual
120 IP address of the system. In that case, the argument to
121 <code>VirtualHost</code> <em>must</em> match the argument to
122 <code>NameVirtualHost</code>:</p>
124 <div class="example"><p><code>
125 NameVirtualHost 172.20.30.40<br />
127 <VirtualHost 172.20.30.40><br />
131 <p>However, it is additionally useful to use <code>*</code>
132 on systems where the IP address is not predictable - for
133 example if you have a dynamic IP address with your ISP, and
134 you are using some variety of dynamic DNS solution. Since
135 <code>*</code> matches any IP address, this configuration
136 would work without changes whenever your IP address
140 <p>The above configuration is what you will want to use in almost
141 all name-based virtual hosting situations. The only thing that this
142 configuration will not work for, in fact, is when you are serving
143 different content based on differing IP addresses or ports.</p>
145 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
146 <div class="section">
147 <h2><a name="twoips" id="twoips">Name-based hosts on more than one
152 <p>Any of the techniques discussed here can be extended to any
153 number of IP addresses.</p>
156 <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
157 will serve the "main" server, <code>server.domain.com</code> and on the
158 other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
160 <div class="example"><h3>Server configuration</h3><p><code>
165 # This is the "main" server running on 172.20.30.40<br />
166 ServerName server.domain.com<br />
167 DocumentRoot /www/mainserver<br />
169 # This is the other address<br />
170 NameVirtualHost 172.20.30.50<br />
172 <VirtualHost 172.20.30.50><br />
173 <span class="indent">
174 DocumentRoot /www/example1<br />
175 ServerName www.example.com<br />
177 # Other directives here ...<br />
180 </VirtualHost><br />
182 <VirtualHost 172.20.30.50><br />
183 <span class="indent">
184 DocumentRoot /www/example2<br />
185 ServerName www.example.org<br />
187 # Other directives here ...<br />
193 <p>Any request to an address other than <code>172.20.30.50</code> will be
194 served from the main server. A request to <code>172.20.30.50</code> with an
195 unknown hostname, or no <code>Host:</code> header, will be served from
196 <code>www.example.com</code>.</p>
198 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
199 <div class="section">
200 <h2><a name="intraextra" id="intraextra">Serving the same content on
201 different IP addresses (such as an internal and external
204 <p>The server machine has two IP addresses (<code>192.168.1.1</code>
205 and <code>172.20.30.40</code>). The machine is sitting between an
206 internal (intranet) network and an external (internet) network. Outside
207 of the network, the name <code>server.example.com</code> resolves to
208 the external address (<code>172.20.30.40</code>), but inside the
209 network, that same name resolves to the internal address
210 (<code>192.168.1.1</code>).</p>
212 <p>The server can be made to respond to internal and external requests
213 with the same content, with just one <code>VirtualHost</code>
216 <div class="example"><h3>Server configuration</h3><p><code>
219 NameVirtualHost 192.168.1.1<br />
220 NameVirtualHost 172.20.30.40<br />
222 <VirtualHost 192.168.1.1 172.20.30.40><br />
223 <span class="indent">
224 DocumentRoot /www/server1<br />
225 ServerName server.example.com<br />
226 ServerAlias server<br />
231 <p>Now requests from both networks will be served from the same
232 <code>VirtualHost</code>.</p>
235 <h3>Note:</h3><p>On the internal
236 network, one can just use the name <code>server</code> rather
237 than the fully qualified host name
238 <code>server.example.com</code>.</p>
240 <p>Note also that, in the above example, you can replace the list
241 of IP addresses with <code>*</code>, which will cause the server to
242 respond the same on all addresses.</p>
245 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
246 <div class="section">
247 <h2><a name="port" id="port">Running different sites on different
250 <p>You have multiple domains going to the same IP and also want to
251 serve multiple ports. By defining the ports in the "NameVirtualHost"
252 tag, you can allow this to work. If you try using <VirtualHost
253 name:port> without the NameVirtualHost name:port or you try to use
254 the Listen directive, your configuration will not work.</p>
256 <div class="example"><h3>Server configuration</h3><p><code>
262 NameVirtualHost 172.20.30.40:80<br />
263 NameVirtualHost 172.20.30.40:8080<br />
265 <VirtualHost 172.20.30.40:80><br />
266 <span class="indent">
267 ServerName www.example.com<br />
268 DocumentRoot /www/domain-80<br />
270 </VirtualHost><br />
272 <VirtualHost 172.20.30.40:8080><br />
273 <span class="indent">
274 ServerName www.example.com<br />
275 DocumentRoot /www/domain-8080<br />
277 </VirtualHost><br />
279 <VirtualHost 172.20.30.40:80><br />
280 <span class="indent">
281 ServerName www.example.org<br />
282 DocumentRoot /www/otherdomain-80<br />
284 </VirtualHost><br />
286 <VirtualHost 172.20.30.40:8080><br />
287 <span class="indent">
288 ServerName www.example.org<br />
289 DocumentRoot /www/otherdomain-8080<br />
294 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
295 <div class="section">
296 <h2><a name="ip" id="ip">IP-based virtual hosting</a></h2>
298 <p>The server has two IP addresses (<code>172.20.30.40</code> and
299 <code>172.20.30.50</code>) which resolve to the names
300 <code>www.example.com</code> and <code>www.example.org</code>
303 <div class="example"><h3>Server configuration</h3><p><code>
308 <VirtualHost 172.20.30.40><br />
309 <span class="indent">
310 DocumentRoot /www/example1<br />
311 ServerName www.example.com<br />
313 </VirtualHost><br />
315 <VirtualHost 172.20.30.50><br />
316 <span class="indent">
317 DocumentRoot /www/example2<br />
318 ServerName www.example.org<br />
323 <p>Requests for any address not specified in one of the
324 <code><VirtualHost></code> directives (such as
325 <code>localhost</code>, for example) will go to the main server, if
328 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
329 <div class="section">
330 <h2><a name="ipport" id="ipport">Mixed port-based and ip-based virtual
333 <p>The server machine has two IP addresses (<code>172.20.30.40</code> and
334 <code>172.20.30.50</code>) which resolve to the names
335 <code>www.example.com</code> and <code>www.example.org</code>
336 respectively. In each case, we want to run hosts on ports 80 and
339 <div class="example"><h3>Server configuration</h3><p><code>
342 Listen 172.20.30.40:80<br />
343 Listen 172.20.30.40:8080<br />
344 Listen 172.20.30.50:80<br />
345 Listen 172.20.30.50:8080<br />
347 <VirtualHost 172.20.30.40:80><br />
348 <span class="indent">
349 DocumentRoot /www/example1-80<br />
350 ServerName www.example.com<br />
352 </VirtualHost><br />
354 <VirtualHost 172.20.30.40:8080><br />
355 <span class="indent">
356 DocumentRoot /www/example1-8080<br />
357 ServerName www.example.com<br />
359 </VirtualHost><br />
361 <VirtualHost 172.20.30.50:80><br />
362 <span class="indent">
363 DocumentRoot /www/example2-80<br />
364 ServerName www.example.org<br />
366 </VirtualHost><br />
368 <VirtualHost 172.20.30.50:8080><br />
369 <span class="indent">
370 DocumentRoot /www/example2-8080<br />
371 ServerName www.example.org<br />
376 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
377 <div class="section">
378 <h2><a name="mixed" id="mixed">Mixed name-based and IP-based
381 <p>On some of my addresses, I want to do name-based virtual hosts, and
382 on others, IP-based hosts.</p>
384 <div class="example"><h3>Server configuration</h3><p><code>
389 NameVirtualHost 172.20.30.40<br />
391 <VirtualHost 172.20.30.40><br />
392 <span class="indent">
393 DocumentRoot /www/example1<br />
394 ServerName www.example.com<br />
396 </VirtualHost><br />
398 <VirtualHost 172.20.30.40><br />
399 <span class="indent">
400 DocumentRoot /www/example2<br />
401 ServerName www.example.org<br />
403 </VirtualHost><br />
405 <VirtualHost 172.20.30.40><br />
406 <span class="indent">
407 DocumentRoot /www/example3<br />
408 ServerName www.example3.net<br />
410 </VirtualHost><br />
413 <VirtualHost 172.20.30.50><br />
414 <span class="indent">
415 DocumentRoot /www/example4<br />
416 ServerName www.example4.edu<br />
418 </VirtualHost><br />
420 <VirtualHost 172.20.30.60><br />
421 <span class="indent">
422 DocumentRoot /www/example5<br />
423 ServerName www.example5.gov<br />
428 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
429 <div class="section">
430 <h2><a name="proxy" id="proxy">Using <code>Virtual_host</code> and
431 mod_proxy together</a></h2>
433 <p>The following example allows a front-end machine to proxy a
434 virtual host through to a server running on another machine. In the
435 example, a virtual host of the same name is configured on a machine
436 at <code>192.168.111.2</code>. The <code class="directive"><a href="../mod/mod_proxy.html#proxypreservehost on">ProxyPreserveHost On</a></code> directive is
437 used so that the desired hostname is passed through, in case we are
438 proxying multiple hostnames to a single machine.</p>
440 <div class="example"><p><code>
441 <VirtualHost *:*><br />
442 ProxyPreserveHost On<br />
443 ProxyPass / http://192.168.111.2/<br />
444 ProxyPassReverse / http://192.168.111.2/<br />
445 ServerName hostname.example.com<br />
449 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
450 <div class="section">
451 <h2><a name="default" id="default">Using <code>_default_</code>
454 <h3><a name="defaultallports" id="defaultallports"><code>_default_</code> vhosts
455 for all ports</a></h3>
457 <p>Catching <em>every</em> request to any unspecified IP address and
458 port, <em>i.e.</em>, an address/port combination that is not used for
459 any other virtual host.</p>
461 <div class="example"><h3>Server configuration</h3><p><code>
464 <VirtualHost _default_:*><br />
465 <span class="indent">
466 DocumentRoot /www/default<br />
471 <p>Using such a default vhost with a wildcard port effectively prevents
472 any request going to the main server.</p>
474 <p>A default vhost never serves a request that was sent to an
475 address/port that is used for name-based vhosts. If the request
476 contained an unknown or no <code>Host:</code> header it is always
477 served from the primary name-based vhost (the vhost for that
478 address/port appearing first in the configuration file).</p>
480 <p>You can use <code class="directive"><a href="../mod/mod_alias.html#aliasmatch">AliasMatch</a></code> or
481 <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to rewrite any
482 request to a single information page (or script).</p>
485 <h3><a name="defaultdifferentports" id="defaultdifferentports"><code>_default_</code> vhosts
486 for different ports</a></h3>
488 <p>Same as setup 1, but the server listens on several ports and we want
489 to use a second <code>_default_</code> vhost for port 80.</p>
491 <div class="example"><h3>Server configuration</h3><p><code>
494 <VirtualHost _default_:80><br />
495 <span class="indent">
496 DocumentRoot /www/default80<br />
499 </VirtualHost><br />
501 <VirtualHost _default_:*><br />
502 <span class="indent">
503 DocumentRoot /www/default<br />
509 <p>The default vhost for port 80 (which <em>must</em> appear before any
510 default vhost with a wildcard port) catches all requests that were sent
511 to an unspecified IP address. The main server is never used to serve a
515 <h3><a name="defaultoneport" id="defaultoneport"><code>_default_</code> vhosts
516 for one port</a></h3>
518 <p>We want to have a default vhost for port 80, but no other default
521 <div class="example"><h3>Server configuration</h3><p><code>
524 <VirtualHost _default_:80><br />
525 DocumentRoot /www/default<br />
530 <p>A request to an unspecified address on port 80 is served from the
531 default vhost. Any other request to an unspecified address and port is
532 served from the main server.</p>
534 <p>Any use of <code>*</code> in a virtual host declaration will have
535 higher precedence than <code>_default_</code>.</p>
539 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
540 <div class="section">
541 <h2><a name="migrate" id="migrate">Migrating a name-based vhost to an
542 IP-based vhost</a></h2>
544 <p>The name-based vhost with the hostname
545 <code>www.example.org</code> (from our <a href="#name">name-based</a> example, setup 2) should get its own IP
546 address. To avoid problems with name servers or proxies who cached the
547 old IP address for the name-based vhost we want to provide both
548 variants during a migration phase.</p>
551 The solution is easy, because we can simply add the new IP address
552 (<code>172.20.30.50</code>) to the <code>VirtualHost</code>
555 <div class="example"><h3>Server configuration</h3><p><code>
559 ServerName www.example.com<br />
560 DocumentRoot /www/example1<br />
562 NameVirtualHost 172.20.30.40<br />
564 <VirtualHost 172.20.30.40 172.20.30.50><br />
565 <span class="indent">
566 DocumentRoot /www/example2<br />
567 ServerName www.example.org<br />
570 </VirtualHost><br />
572 <VirtualHost 172.20.30.40><br />
573 <span class="indent">
574 DocumentRoot /www/example3<br />
575 ServerName www.example.net<br />
576 ServerAlias *.example.net<br />
582 <p>The vhost can now be accessed through the new address (as an
583 IP-based vhost) and through the old address (as a name-based
586 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
587 <div class="section">
588 <h2><a name="serverpath" id="serverpath">Using the <code>ServerPath</code>
591 <p>We have a server with two name-based vhosts. In order to match the
592 correct virtual host a client must send the correct <code>Host:</code>
593 header. Old HTTP/1.0 clients do not send such a header and Apache has
594 no clue what vhost the client tried to reach (and serves the request
595 from the primary vhost). To provide as much backward compatibility as
596 possible we create a primary vhost which returns a single page
597 containing links with an URL prefix to the name-based virtual
600 <div class="example"><h3>Server configuration</h3><p><code>
603 NameVirtualHost 172.20.30.40<br />
605 <VirtualHost 172.20.30.40><br />
606 <span class="indent">
607 # primary vhost<br />
608 DocumentRoot /www/subdomain<br />
609 RewriteEngine On<br />
610 RewriteRule ^/.* /www/subdomain/index.html<br />
613 </VirtualHost><br />
615 <VirtualHost 172.20.30.40><br />
616 DocumentRoot /www/subdomain/sub1<br />
617 <span class="indent">
618 ServerName www.sub1.domain.tld<br />
619 ServerPath /sub1/<br />
620 RewriteEngine On<br />
621 RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
624 </VirtualHost><br />
626 <VirtualHost 172.20.30.40><br />
627 <span class="indent">
628 DocumentRoot /www/subdomain/sub2<br />
629 ServerName www.sub2.domain.tld<br />
630 ServerPath /sub2/<br />
631 RewriteEngine On<br />
632 RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
638 <p>Due to the <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>
639 directive a request to the URL
640 <code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
641 from the sub1-vhost.<br /> A request to the URL
642 <code>http://www.sub1.domain.tld/</code> is only
643 served from the sub1-vhost if the client sent a correct
644 <code>Host:</code> header. If no <code>Host:</code> header is sent the
645 client gets the information page from the primary host.</p>
647 <p>Please note that there is one oddity: A request to
648 <code>http://www.sub2.domain.tld/sub1/</code> is also served from the
649 sub1-vhost if the client sent no <code>Host:</code> header.</p>
651 <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives
652 are used to make sure that a client which sent a correct
653 <code>Host:</code> header can use both URL variants, <em>i.e.</em>,
654 with or without URL prefix.</p>
657 <div class="bottomlang">
658 <p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
659 <a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Français"> fr </a> |
660 <a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
661 <a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
662 <a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
663 </div><div id="footer">
664 <p class="apache">Copyright 2010 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>
665 <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>