1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="examples.xml.meta">
24 <parentdocument href="./">Virtual Hosts</parentdocument>
25 <title>VirtualHost Examples</title>
29 <p>This document attempts to answer the commonly-asked questions about
30 setting up virtual hosts. These scenarios are those involving multiple
31 web sites running on a single server, via <a
32 href="name-based.html">name-based</a> or <a
33 href="ip-based.html">IP-based</a> virtual hosts.
38 <section id="purename"><title>Running several name-based web
39 sites on a single IP address.</title>
41 <p>Your server has a single IP address, and multiple aliases (CNAMES)
42 point to this machine in DNS. You want to run a web server for
43 <code>www.example.com</code> and <code>www.example.org</code> on this
46 <note><title>Note</title><p>Creating virtual
47 host configurations on your Apache server does not magically
48 cause DNS entries to be created for those host names. You
49 <em>must</em> have the names in DNS, resolving to your IP
50 address, or nobody else will be able to see your web site. You
51 can put entries in your <code>hosts</code> file for local
52 testing, but that will work only from the machine with those
57 <title>Server configuration</title>
59 # Ensure that Apache listens on port 80<br />
62 # Listen for virtual host requests on all IP addresses<br />
63 NameVirtualHost *:80<br />
65 <VirtualHost *:80><br />
67 DocumentRoot /www/example1<br />
68 ServerName www.example.com<br />
70 # Other directives here<br />
73 </VirtualHost><br />
75 <VirtualHost *:80><br />
77 DocumentRoot /www/example2<br />
78 ServerName www.example.org<br />
80 # Other directives here<br />
86 <p>The asterisks match all addresses, so the main server serves no
87 requests. Due to the fact that <code>www.example.com</code> is first
88 in the configuration file, it has the highest priority and can be seen
89 as the <cite>default</cite> or <cite>primary</cite> server. That means
90 that if a request is received that does not match one of the specified
91 <code>ServerName</code> directives, it will be served by this first
92 <code>VirtualHost</code>.</p>
97 <p>You can, if you wish, replace <code>*</code> with the actual
98 IP address of the system. In that case, the argument to
99 <code>VirtualHost</code> <em>must</em> match the argument to
100 <code>NameVirtualHost</code>:</p>
103 NameVirtualHost 172.20.30.40<br />
105 <VirtualHost 172.20.30.40><br />
109 <p>However, it is additionally useful to use <code>*</code>
110 on systems where the IP address is not predictable - for
111 example if you have a dynamic IP address with your ISP, and
112 you are using some variety of dynamic DNS solution. Since
113 <code>*</code> matches any IP address, this configuration
114 would work without changes whenever your IP address
118 <p>The above configuration is what you will want to use in almost
119 all name-based virtual hosting situations. The only thing that this
120 configuration will not work for, in fact, is when you are serving
121 different content based on differing IP addresses or ports.</p>
125 <section id="twoips"><title>Name-based hosts on more than one
130 <p>Any of the techniques discussed here can be extended to any
131 number of IP addresses.</p>
134 <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
135 will serve the "main" server, <code>server.domain.com</code> and on the
136 other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
139 <title>Server configuration</title>
143 # This is the "main" server running on 172.20.30.40<br />
144 ServerName server.domain.com<br />
145 DocumentRoot /www/mainserver<br />
147 # This is the other address<br />
148 NameVirtualHost 172.20.30.50<br />
150 <VirtualHost 172.20.30.50><br />
152 DocumentRoot /www/example1<br />
153 ServerName www.example.com<br />
155 # Other directives here ...<br />
158 </VirtualHost><br />
160 <VirtualHost 172.20.30.50><br />
162 DocumentRoot /www/example2<br />
163 ServerName www.example.org<br />
165 # Other directives here ...<br />
171 <p>Any request to an address other than <code>172.20.30.50</code> will be
172 served from the main server. A request to <code>172.20.30.50</code> with an
173 unknown hostname, or no <code>Host:</code> header, will be served from
174 <code>www.example.com</code>.</p>
178 <section id="intraextra"><title>Serving the same content on
179 different IP addresses (such as an internal and external
182 <p>The server machine has two IP addresses (<code>192.168.1.1</code>
183 and <code>172.20.30.40</code>). The machine is sitting between an
184 internal (intranet) network and an external (internet) network. Outside
185 of the network, the name <code>server.example.com</code> resolves to
186 the external address (<code>172.20.30.40</code>), but inside the
187 network, that same name resolves to the internal address
188 (<code>192.168.1.1</code>).</p>
190 <p>The server can be made to respond to internal and external requests
191 with the same content, with just one <code>VirtualHost</code>
195 <title>Server configuration</title>
197 NameVirtualHost 192.168.1.1<br />
198 NameVirtualHost 172.20.30.40<br />
200 <VirtualHost 192.168.1.1 172.20.30.40><br />
202 DocumentRoot /www/server1<br />
203 ServerName server.example.com<br />
204 ServerAlias server<br />
209 <p>Now requests from both networks will be served from the same
210 <code>VirtualHost</code>.</p>
213 <title>Note:</title><p>On the internal
214 network, one can just use the name <code>server</code> rather
215 than the fully qualified host name
216 <code>server.example.com</code>.</p>
218 <p>Note also that, in the above example, you can replace the list
219 of IP addresses with <code>*</code>, which will cause the server to
220 respond the same on all addresses.</p>
225 <section id="port"><title>Running different sites on different
228 <p>You have multiple domains going to the same IP and also want to
229 serve multiple ports. By defining the ports in the "NameVirtualHost"
230 tag, you can allow this to work. If you try using <VirtualHost
231 name:port> without the NameVirtualHost name:port or you try to use
232 the Listen directive, your configuration will not work.</p>
235 <title>Server configuration</title>
240 NameVirtualHost 172.20.30.40:80<br />
241 NameVirtualHost 172.20.30.40:8080<br />
243 <VirtualHost 172.20.30.40:80><br />
245 ServerName www.example.com<br />
246 DocumentRoot /www/domain-80<br />
248 </VirtualHost><br />
250 <VirtualHost 172.20.30.40:8080><br />
252 ServerName www.example.com<br />
253 DocumentRoot /www/domain-8080<br />
255 </VirtualHost><br />
257 <VirtualHost 172.20.30.40:80><br />
259 ServerName www.example.org<br />
260 DocumentRoot /www/otherdomain-80<br />
262 </VirtualHost><br />
264 <VirtualHost 172.20.30.40:8080><br />
266 ServerName www.example.org<br />
267 DocumentRoot /www/otherdomain-8080<br />
274 <section id="ip"><title>IP-based virtual hosting</title>
276 <p>The server has two IP addresses (<code>172.20.30.40</code> and
277 <code>172.20.30.50</code>) which resolve to the names
278 <code>www.example.com</code> and <code>www.example.org</code>
282 <title>Server configuration</title>
286 <VirtualHost 172.20.30.40><br />
288 DocumentRoot /www/example1<br />
289 ServerName www.example.com<br />
291 </VirtualHost><br />
293 <VirtualHost 172.20.30.50><br />
295 DocumentRoot /www/example2<br />
296 ServerName www.example.org<br />
301 <p>Requests for any address not specified in one of the
302 <code><VirtualHost></code> directives (such as
303 <code>localhost</code>, for example) will go to the main server, if
308 <section id="ipport"><title>Mixed port-based and ip-based virtual
311 <p>The server machine has two IP addresses (<code>172.20.30.40</code> and
312 <code>172.20.30.50</code>) which resolve to the names
313 <code>www.example.com</code> and <code>www.example.org</code>
314 respectively. In each case, we want to run hosts on ports 80 and
318 <title>Server configuration</title>
320 Listen 172.20.30.40:80<br />
321 Listen 172.20.30.40:8080<br />
322 Listen 172.20.30.50:80<br />
323 Listen 172.20.30.50:8080<br />
325 <VirtualHost 172.20.30.40:80><br />
327 DocumentRoot /www/example1-80<br />
328 ServerName www.example.com<br />
330 </VirtualHost><br />
332 <VirtualHost 172.20.30.40:8080><br />
334 DocumentRoot /www/example1-8080<br />
335 ServerName www.example.com<br />
337 </VirtualHost><br />
339 <VirtualHost 172.20.30.50:80><br />
341 DocumentRoot /www/example2-80<br />
342 ServerName www.example.org<br />
344 </VirtualHost><br />
346 <VirtualHost 172.20.30.50:8080><br />
348 DocumentRoot /www/example2-8080<br />
349 ServerName www.example.org<br />
356 <section id="mixed"><title>Mixed name-based and IP-based
359 <p>On some of my addresses, I want to do name-based virtual hosts, and
360 on others, IP-based hosts.</p>
363 <title>Server configuration</title>
367 NameVirtualHost 172.20.30.40<br />
369 <VirtualHost 172.20.30.40><br />
371 DocumentRoot /www/example1<br />
372 ServerName www.example.com<br />
374 </VirtualHost><br />
376 <VirtualHost 172.20.30.40><br />
378 DocumentRoot /www/example2<br />
379 ServerName www.example.org<br />
381 </VirtualHost><br />
383 <VirtualHost 172.20.30.40><br />
385 DocumentRoot /www/example3<br />
386 ServerName www.example3.net<br />
388 </VirtualHost><br />
391 <VirtualHost 172.20.30.50><br />
393 DocumentRoot /www/example4<br />
394 ServerName www.example4.edu<br />
396 </VirtualHost><br />
398 <VirtualHost 172.20.30.60><br />
400 DocumentRoot /www/example5<br />
401 ServerName www.example5.gov<br />
408 <section id="proxy"><title>Using <code>Virtual_host</code> and
409 mod_proxy together</title>
411 <p>The following example allows a front-end machine to proxy a
412 virtual host through to a server running on another machine. In the
413 example, a virtual host of the same name is configured on a machine
414 at <code>192.168.111.2</code>. The <directive
415 module="mod_proxy">ProxyPreserveHost On</directive> directive is
416 used so that the desired hostname is passed through, in case we are
417 proxying multiple hostnames to a single machine.</p>
420 <VirtualHost *:*><br />
421 ProxyPreserveHost On<br />
422 ProxyPass / http://192.168.111.2/<br />
423 ProxyPassReverse / http://192.168.111.2/<br />
424 ServerName hostname.example.com<br />
430 <section id="default"><title>Using <code>_default_</code>
433 <section id="defaultallports"><title><code>_default_</code> vhosts
434 for all ports</title>
436 <p>Catching <em>every</em> request to any unspecified IP address and
437 port, <em>i.e.</em>, an address/port combination that is not used for
438 any other virtual host.</p>
441 <title>Server configuration</title>
443 <VirtualHost _default_:*><br />
445 DocumentRoot /www/default<br />
450 <p>Using such a default vhost with a wildcard port effectively prevents
451 any request going to the main server.</p>
453 <p>A default vhost never serves a request that was sent to an
454 address/port that is used for name-based vhosts. If the request
455 contained an unknown or no <code>Host:</code> header it is always
456 served from the primary name-based vhost (the vhost for that
457 address/port appearing first in the configuration file).</p>
459 <p>You can use <directive module="mod_alias">AliasMatch</directive> or
460 <directive module="mod_rewrite">RewriteRule</directive> to rewrite any
461 request to a single information page (or script).</p>
464 <section id="defaultdifferentports"><title><code>_default_</code> vhosts
465 for different ports</title>
467 <p>Same as setup 1, but the server listens on several ports and we want
468 to use a second <code>_default_</code> vhost for port 80.</p>
471 <title>Server configuration</title>
473 <VirtualHost _default_:80><br />
475 DocumentRoot /www/default80<br />
478 </VirtualHost><br />
480 <VirtualHost _default_:*><br />
482 DocumentRoot /www/default<br />
488 <p>The default vhost for port 80 (which <em>must</em> appear before any
489 default vhost with a wildcard port) catches all requests that were sent
490 to an unspecified IP address. The main server is never used to serve a
494 <section id="defaultoneport"><title><code>_default_</code> vhosts
497 <p>We want to have a default vhost for port 80, but no other default
501 <title>Server configuration</title>
503 <VirtualHost _default_:80><br />
504 DocumentRoot /www/default<br />
509 <p>A request to an unspecified address on port 80 is served from the
510 default vhost. Any other request to an unspecified address and port is
511 served from the main server.</p>
516 <section id="migrate"><title>Migrating a name-based vhost to an
517 IP-based vhost</title>
519 <p>The name-based vhost with the hostname
520 <code>www.example.org</code> (from our <a
521 href="#name">name-based</a> example, setup 2) should get its own IP
522 address. To avoid problems with name servers or proxies who cached the
523 old IP address for the name-based vhost we want to provide both
524 variants during a migration phase.</p>
527 The solution is easy, because we can simply add the new IP address
528 (<code>172.20.30.50</code>) to the <code>VirtualHost</code>
532 <title>Server configuration</title>
535 ServerName www.example.com<br />
536 DocumentRoot /www/example1<br />
538 NameVirtualHost 172.20.30.40<br />
540 <VirtualHost 172.20.30.40 172.20.30.50><br />
542 DocumentRoot /www/example2<br />
543 ServerName www.example.org<br />
546 </VirtualHost><br />
548 <VirtualHost 172.20.30.40><br />
550 DocumentRoot /www/example3<br />
551 ServerName www.example.net<br />
552 ServerAlias *.example.net<br />
558 <p>The vhost can now be accessed through the new address (as an
559 IP-based vhost) and through the old address (as a name-based
564 <section id="serverpath"><title>Using the <code>ServerPath</code>
567 <p>We have a server with two name-based vhosts. In order to match the
568 correct virtual host a client must send the correct <code>Host:</code>
569 header. Old HTTP/1.0 clients do not send such a header and Apache has
570 no clue what vhost the client tried to reach (and serves the request
571 from the primary vhost). To provide as much backward compatibility as
572 possible we create a primary vhost which returns a single page
573 containing links with an URL prefix to the name-based virtual
577 <title>Server configuration</title>
579 NameVirtualHost 172.20.30.40<br />
581 <VirtualHost 172.20.30.40><br />
583 # primary vhost<br />
584 DocumentRoot /www/subdomain<br />
585 RewriteEngine On<br />
586 RewriteRule ^/.* /www/subdomain/index.html<br />
589 </VirtualHost><br />
591 <VirtualHost 172.20.30.40><br />
592 DocumentRoot /www/subdomain/sub1<br />
594 ServerName www.sub1.domain.tld<br />
595 ServerPath /sub1/<br />
596 RewriteEngine On<br />
597 RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
600 </VirtualHost><br />
602 <VirtualHost 172.20.30.40><br />
604 DocumentRoot /www/subdomain/sub2<br />
605 ServerName www.sub2.domain.tld<br />
606 ServerPath /sub2/<br />
607 RewriteEngine On<br />
608 RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
614 <p>Due to the <directive module="core">ServerPath</directive>
615 directive a request to the URL
616 <code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
617 from the sub1-vhost.<br /> A request to the URL
618 <code>http://www.sub1.domain.tld/</code> is only
619 served from the sub1-vhost if the client sent a correct
620 <code>Host:</code> header. If no <code>Host:</code> header is sent the
621 client gets the information page from the primary host.</p>
623 <p>Please note that there is one oddity: A request to
624 <code>http://www.sub2.domain.tld/sub1/</code> is also served from the
625 sub1-vhost if the client sent no <code>Host:</code> header.</p>
627 <p>The <directive module="mod_rewrite">RewriteRule</directive> directives
628 are used to make sure that a client which sent a correct
629 <code>Host:</code> header can use both URL variants, <em>i.e.</em>,
630 with or without URL prefix.</p>