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.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>VirtualHost Examples</h1>
21 <p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
22 <a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
23 <a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
27 <p>This document attempts to answer the commonly-asked questions about
28 setting up virtual hosts. These scenarios are those involving multiple
29 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.
33 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#purename">Running several name-based web
34 sites on a single IP address.</a></li>
35 <li><img alt="" src="../images/down.gif" /> <a href="#twoips">Name-based hosts on more than one
37 <li><img alt="" src="../images/down.gif" /> <a href="#intraextra">Serving the same content on
38 different IP addresses (such as an internal and external
40 <li><img alt="" src="../images/down.gif" /> <a href="#port">Running different sites on different
42 <li><img alt="" src="../images/down.gif" /> <a href="#ip">IP-based virtual hosting</a></li>
43 <li><img alt="" src="../images/down.gif" /> <a href="#ipport">Mixed port-based and ip-based virtual
45 <li><img alt="" src="../images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based
47 <li><img alt="" src="../images/down.gif" /> <a href="#proxy">Using <code>Virtual_host</code> and
48 mod_proxy together</a></li>
49 <li><img alt="" src="../images/down.gif" /> <a href="#default">Using <code>_default_</code>
51 <li><img alt="" src="../images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an
52 IP-based vhost</a></li>
53 <li><img alt="" src="../images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code>
56 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
58 <h2><a name="purename" id="purename">Running several name-based web
59 sites on a single IP address.</a></h2>
61 <p>Your server has a single IP address, and multiple aliases (CNAMES)
62 point to this machine in DNS. You want to run a web server for
63 <code>www.example1.com</code> and <code>www.example2.org</code> on this
66 <div class="note"><h3>Note</h3><p>Creating virtual
67 host configurations on your Apache server does not magically
68 cause DNS entries to be created for those host names. You
69 <em>must</em> have the names in DNS, resolving to your IP
70 address, or nobody else will be able to see your web site. You
71 can put entries in your <code>hosts</code> file for local
72 testing, but that will work only from the machine with those
76 <div class="example"><h3>Server configuration</h3><p><code>
79 # Ensure that Apache listens on port 80<br />
82 # Listen for virtual host requests on all IP addresses<br />
83 NameVirtualHost *:80<br />
85 <VirtualHost *:80><br />
87 DocumentRoot /www/example1<br />
88 ServerName www.example1.com<br />
90 # Other directives here<br />
93 </VirtualHost><br />
95 <VirtualHost *:80><br />
97 DocumentRoot /www/example2<br />
98 ServerName www.example2.org<br />
100 # Other directives here<br />
106 <p>The asterisks match all addresses, so the main server serves no
107 requests. Due to the fact that <code>www.example1.com</code> is first
108 in the configuration file, it has the highest priority and can be seen
109 as the <cite>default</cite> or <cite>primary</cite> server. That means
110 that if a request is received that does not match one of the specified
111 <code>ServerName</code> directives, it will be served by this first
112 <code>VirtualHost</code>.</p>
117 <p>You can, if you wish, replace <code>*</code> with the actual
118 IP address of the system. In that case, the argument to
119 <code>VirtualHost</code> <em>must</em> match the argument to
120 <code>NameVirtualHost</code>:</p>
122 <div class="example"><p><code>
123 NameVirtualHost 172.20.30.40<br />
125 <VirtualHost 172.20.30.40><br />
129 <p>However, it is additionally useful to use <code>*</code>
130 on systems where the IP address is not predictable - for
131 example if you have a dynamic IP address with your ISP, and
132 you are using some variety of dynamic DNS solution. Since
133 <code>*</code> matches any IP address, this configuration
134 would work without changes whenever your IP address
138 <p>The above configuration is what you will want to use in almost
139 all name-based virtual hosting situations. The only thing that this
140 configuration will not work for, in fact, is when you are serving
141 different content based on differing IP addresses or ports.</p>
143 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
144 <div class="section">
145 <h2><a name="twoips" id="twoips">Name-based hosts on more than one
149 <h3>Note</h3><p>Any of the
150 techniques discussed here can be extended to any number of IP
154 <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
155 will serve the "main" server, <code>server.domain.com</code> and on the
156 other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
158 <div class="example"><h3>Server configuration</h3><p><code>
163 # This is the "main" server running on 172.20.30.40<br />
164 ServerName server.domain.com<br />
165 DocumentRoot /www/mainserver<br />
167 # This is the other address<br />
168 NameVirtualHost 172.20.30.50<br />
170 <VirtualHost 172.20.30.50><br />
171 <span class="indent">
172 DocumentRoot /www/example1<br />
173 ServerName www.example1.com<br />
175 # Other directives here ...<br />
178 </VirtualHost><br />
180 <VirtualHost 172.20.30.50><br />
181 <span class="indent">
182 DocumentRoot /www/example2<br />
183 ServerName www.example2.org<br />
185 # Other directives here ...<br />
191 <p>Any request to an address other than <code>172.20.30.50</code> will be
192 served from the main server. A request to <code>172.20.30.50</code> with an
193 unknown hostname, or no <code>Host:</code> header, will be served from
194 <code>www.example1.com</code>.</p>
196 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
197 <div class="section">
198 <h2><a name="intraextra" id="intraextra">Serving the same content on
199 different IP addresses (such as an internal and external
202 <p>The server machine has two IP addresses (<code>192.168.1.1</code>
203 and <code>172.20.30.40</code>). The machine is sitting between an
204 internal (intranet) network and an external (internet) network. Outside
205 of the network, the name <code>server.example.com</code> resolves to
206 the external address (<code>172.20.30.40</code>), but inside the
207 network, that same name resolves to the internal address
208 (<code>192.168.1.1</code>).</p>
210 <p>The server can be made to respond to internal and external requests
211 with the same content, with just one <code>VirtualHost</code>
214 <div class="example"><h3>Server configuration</h3><p><code>
217 NameVirtualHost 192.168.1.1<br />
218 NameVirtualHost 172.20.30.40<br />
220 <VirtualHost 192.168.1.1 172.20.30.40><br />
221 <span class="indent">
222 DocumentRoot /www/server1<br />
223 ServerName server.example.com<br />
224 ServerAlias server<br />
229 <p>Now requests from both networks will be served from the same
230 <code>VirtualHost</code>.</p>
233 <h3>Note:</h3><p>On the internal
234 network, one can just use the name <code>server</code> rather
235 than the fully qualified host name
236 <code>server.example.com</code>.</p>
238 <p>Note also that, in the above example, you can replace the list
239 of IP addresses with <code>*</code>, which will cause the server to
240 respond the same on all addresses.</p>
243 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
244 <div class="section">
245 <h2><a name="port" id="port">Running different sites on different
248 <p>You have multiple domains going to the same IP and also want to
249 serve multiple ports. By defining the ports in the "NameVirtualHost"
250 tag, you can allow this to work. If you try using <VirtualHost
251 name:port> without the NameVirtualHost name:port or you try to use
252 the Listen directive, your configuration will not work.</p>
254 <div class="example"><h3>Server configuration</h3><p><code>
260 NameVirtualHost 172.20.30.40:80<br />
261 NameVirtualHost 172.20.30.40:8080<br />
263 <VirtualHost 172.20.30.40:80><br />
264 <span class="indent">
265 ServerName www.example1.com<br />
266 DocumentRoot /www/domain-80<br />
268 </VirtualHost><br />
270 <VirtualHost 172.20.30.40:8080><br />
271 <span class="indent">
272 ServerName www.example1.com<br />
273 DocumentRoot /www/domain-8080<br />
275 </VirtualHost><br />
277 <VirtualHost 172.20.30.40:80><br />
278 <span class="indent">
279 ServerName www.example2.org<br />
280 DocumentRoot /www/otherdomain-80<br />
282 </VirtualHost><br />
284 <VirtualHost 172.20.30.40:8080><br />
285 <span class="indent">
286 ServerName www.example2.org<br />
287 DocumentRoot /www/otherdomain-8080<br />
292 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
293 <div class="section">
294 <h2><a name="ip" id="ip">IP-based virtual hosting</a></h2>
296 <p>The server has two IP addresses (<code>172.20.30.40</code> and
297 <code>172.20.30.50</code>) which resolve to the names
298 <code>www.example1.com</code> and <code>www.example2.org</code>
301 <div class="example"><h3>Server configuration</h3><p><code>
306 <VirtualHost 172.20.30.40><br />
307 <span class="indent">
308 DocumentRoot /www/example1<br />
309 ServerName www.example1.com<br />
311 </VirtualHost><br />
313 <VirtualHost 172.20.30.50><br />
314 <span class="indent">
315 DocumentRoot /www/example2<br />
316 ServerName www.example2.org<br />
321 <p>Requests for any address not specified in one of the
322 <code><VirtualHost></code> directives (such as
323 <code>localhost</code>, for example) will go to the main server, if
326 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
327 <div class="section">
328 <h2><a name="ipport" id="ipport">Mixed port-based and ip-based virtual
331 <p>The server machine has two IP addresses (<code>172.20.30.40</code> and
332 <code>172.20.30.50</code>) which resolve to the names
333 <code>www.example1.com</code> and <code>www.example2.org</code>
334 respectively. In each case, we want to run hosts on ports 80 and
337 <div class="example"><h3>Server configuration</h3><p><code>
340 Listen 172.20.30.40:80<br />
341 Listen 172.20.30.40:8080<br />
342 Listen 172.20.30.50:80<br />
343 Listen 172.20.30.50:8080<br />
345 <VirtualHost 172.20.30.40:80><br />
346 <span class="indent">
347 DocumentRoot /www/example1-80<br />
348 ServerName www.example1.com<br />
350 </VirtualHost><br />
352 <VirtualHost 172.20.30.40:8080><br />
353 <span class="indent">
354 DocumentRoot /www/example1-8080<br />
355 ServerName www.example1.com<br />
357 </VirtualHost><br />
359 <VirtualHost 172.20.30.50:80><br />
360 <span class="indent">
361 DocumentRoot /www/example2-80<br />
362 ServerName www.example1.org<br />
364 </VirtualHost><br />
366 <VirtualHost 172.20.30.50:8080><br />
367 <span class="indent">
368 DocumentRoot /www/example2-8080<br />
369 ServerName www.example2.org<br />
374 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
375 <div class="section">
376 <h2><a name="mixed" id="mixed">Mixed name-based and IP-based
379 <p>On some of my addresses, I want to do name-based virtual hosts, and
380 on others, IP-based hosts.</p>
382 <div class="example"><h3>Server configuration</h3><p><code>
387 NameVirtualHost 172.20.30.40<br />
389 <VirtualHost 172.20.30.40><br />
390 <span class="indent">
391 DocumentRoot /www/example1<br />
392 ServerName www.example1.com<br />
394 </VirtualHost><br />
396 <VirtualHost 172.20.30.40><br />
397 <span class="indent">
398 DocumentRoot /www/example2<br />
399 ServerName www.example2.org<br />
401 </VirtualHost><br />
403 <VirtualHost 172.20.30.40><br />
404 <span class="indent">
405 DocumentRoot /www/example3<br />
406 ServerName www.example3.net<br />
408 </VirtualHost><br />
411 <VirtualHost 172.20.30.50><br />
412 <span class="indent">
413 DocumentRoot /www/example4<br />
414 ServerName www.example4.edu<br />
416 </VirtualHost><br />
418 <VirtualHost 172.20.30.60><br />
419 <span class="indent">
420 DocumentRoot /www/example5<br />
421 ServerName www.example5.gov<br />
426 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
427 <div class="section">
428 <h2><a name="proxy" id="proxy">Using <code>Virtual_host</code> and
429 mod_proxy together</a></h2>
431 <p>The following example allows a front-end machine to proxy a
432 virtual host through to a server running on another machine. In the
433 example, a virtual host of the same name is configured on a machine
434 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
435 used so that the desired hostname is passed through, in case we are
436 proxying multiple hostnames to a single machine.</p>
438 <div class="example"><p><code>
439 <VirtualHost *:*><br />
440 ProxyPreserveHost On<br />
441 ProxyPass / http://192.168.111.2/<br />
442 ProxyPassReverse / http://192.168.111.2/<br />
443 ServerName hostname.example.com<br />
447 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
448 <div class="section">
449 <h2><a name="default" id="default">Using <code>_default_</code>
452 <h3><a name="defaultallports" id="defaultallports"><code>_default_</code> vhosts
453 for all ports</a></h3>
455 <p>Catching <em>every</em> request to any unspecified IP address and
456 port, <em>i.e.</em>, an address/port combination that is not used for
457 any other virtual host.</p>
459 <div class="example"><h3>Server configuration</h3><p><code>
462 <VirtualHost _default_:*><br />
463 <span class="indent">
464 DocumentRoot /www/default<br />
469 <p>Using such a default vhost with a wildcard port effectively prevents
470 any request going to the main server.</p>
472 <p>A default vhost never serves a request that was sent to an
473 address/port that is used for name-based vhosts. If the request
474 contained an unknown or no <code>Host:</code> header it is always
475 served from the primary name-based vhost (the vhost for that
476 address/port appearing first in the configuration file).</p>
478 <p>You can use <code class="directive"><a href="../mod/mod_alias.html#aliasmatch">AliasMatch</a></code> or
479 <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to rewrite any
480 request to a single information page (or script).</p>
483 <h3><a name="defaultdifferentports" id="defaultdifferentports"><code>_default_</code> vhosts
484 for different ports</a></h3>
486 <p>Same as setup 1, but the server listens on several ports and we want
487 to use a second <code>_default_</code> vhost for port 80.</p>
489 <div class="example"><h3>Server configuration</h3><p><code>
492 <VirtualHost _default_:80><br />
493 <span class="indent">
494 DocumentRoot /www/default80<br />
497 </VirtualHost><br />
499 <VirtualHost _default_:*><br />
500 <span class="indent">
501 DocumentRoot /www/default<br />
507 <p>The default vhost for port 80 (which <em>must</em> appear before any
508 default vhost with a wildcard port) catches all requests that were sent
509 to an unspecified IP address. The main server is never used to serve a
513 <h3><a name="defaultoneport" id="defaultoneport"><code>_default_</code> vhosts
514 for one port</a></h3>
516 <p>We want to have a default vhost for port 80, but no other default
519 <div class="example"><h3>Server configuration</h3><p><code>
522 <VirtualHost _default_:80><br />
523 DocumentRoot /www/default<br />
528 <p>A request to an unspecified address on port 80 is served from the
529 default vhost any other request to an unspecified address and port is
530 served from the main server.</p>
533 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
534 <div class="section">
535 <h2><a name="migrate" id="migrate">Migrating a name-based vhost to an
536 IP-based vhost</a></h2>
538 <p>The name-based vhost with the hostname
539 <code>www.example2.org</code> (from our <a href="#name">name-based</a> example, setup 2) should get its own IP
540 address. To avoid problems with name servers or proxies who cached the
541 old IP address for the name-based vhost we want to provide both
542 variants during a migration phase.<br />
543 The solution is easy, because we can simply add the new IP address
544 (<code>172.20.30.50</code>) to the <code>VirtualHost</code>
547 <div class="example"><h3>Server configuration</h3><p><code>
551 ServerName www.example1.com<br />
552 DocumentRoot /www/example1<br />
554 NameVirtualHost 172.20.30.40<br />
556 <VirtualHost 172.20.30.40 172.20.30.50><br />
557 <span class="indent">
558 DocumentRoot /www/example2<br />
559 ServerName www.example2.org<br />
562 </VirtualHost><br />
564 <VirtualHost 172.20.30.40><br />
565 <span class="indent">
566 DocumentRoot /www/example3<br />
567 ServerName www.example3.net<br />
568 ServerAlias *.example3.net<br />
574 <p>The vhost can now be accessed through the new address (as an
575 IP-based vhost) and through the old address (as a name-based
578 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
579 <div class="section">
580 <h2><a name="serverpath" id="serverpath">Using the <code>ServerPath</code>
583 <p>We have a server with two name-based vhosts. In order to match the
584 correct virtual host a client must send the correct <code>Host:</code>
585 header. Old HTTP/1.0 clients do not send such a header and Apache has
586 no clue what vhost the client tried to reach (and serves the request
587 from the primary vhost). To provide as much backward compatibility as
588 possible we create a primary vhost which returns a single page
589 containing links with an URL prefix to the name-based virtual
592 <div class="example"><h3>Server configuration</h3><p><code>
595 NameVirtualHost 172.20.30.40<br />
597 <VirtualHost 172.20.30.40><br />
598 <span class="indent">
599 # primary vhost<br />
600 DocumentRoot /www/subdomain<br />
601 RewriteEngine On<br />
602 RewriteRule ^/.* /www/subdomain/index.html<br />
605 </VirtualHost><br />
607 <VirtualHost 172.20.30.40><br />
608 DocumentRoot /www/subdomain/sub1<br />
609 <span class="indent">
610 ServerName www.sub1.domain.tld<br />
611 ServerPath /sub1/<br />
612 RewriteEngine On<br />
613 RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
616 </VirtualHost><br />
618 <VirtualHost 172.20.30.40><br />
619 <span class="indent">
620 DocumentRoot /www/subdomain/sub2<br />
621 ServerName www.sub2.domain.tld<br />
622 ServerPath /sub2/<br />
623 RewriteEngine On<br />
624 RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
630 <p>Due to the <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>
631 directive a request to the URL
632 <code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
633 from the sub1-vhost.<br /> A request to the URL
634 <code>http://www.sub1.domain.tld/</code> is only
635 served from the sub1-vhost if the client sent a correct
636 <code>Host:</code> header. If no <code>Host:</code> header is sent the
637 client gets the information page from the primary host.<br />
638 Please note that there is one oddity: A request to
639 <code>http://www.sub2.domain.tld/sub1/</code> is also served from the
640 sub1-vhost if the client sent no <code>Host:</code> header.<br />
641 The <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives
642 are used to make sure that a client which sent a correct
643 <code>Host:</code> header can use both URL variants, <em>i.e.</em>,
644 with or without URL prefix.</p>
647 <div class="bottomlang">
648 <p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
649 <a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> |
650 <a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p>
651 </div><div id="footer">
652 <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>
653 <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>