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"?>
6 Copyright 2002-2004 The Apache Software Foundation
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
21 <manualpage metafile="examples.xml.meta">
22 <parentdocument href="./">Virtual Hosts</parentdocument>
23 <title>VirtualHost Examples</title>
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
30 href="name-based.html">name-based</a> or <a
31 href="ip-based.html">IP-based</a> virtual hosts. A document should be
32 coming soon about running sites on several servers behind a single
37 <section id="purename"><title>Running several name-based web
38 sites on a single IP address.</title>
40 <p>Your server has a single IP address, and multiple aliases (CNAMES)
41 point to this machine in DNS. You want to run a web server for
42 <code>www.example1.com</code> and <code>www.example2.org</code> on this
45 <note><title>Note</title><p>Creating virtual
46 host configurations on your Apache server does not magically
47 cause DNS entries to be created for those host names. You
48 <em>must</em> have the names in DNS, resolving to your IP
49 address, or nobody else will be able to see your web site. You
50 can put entries in your <code>hosts</code> file for local
51 testing, but that will work only from the machine with those
56 <title>Server configuration</title>
58 # Ensure that Apache listens on port 80<br />
61 # Listen for virtual host requests on all IP addresses<br />
62 NameVirtualHost *<br />
64 <VirtualHost *><br />
66 DocumentRoot /www/example1<br />
67 ServerName www.example1.com<br />
69 # Other directives here<br />
72 </VirtualHost><br />
74 <VirtualHost *><br />
76 DocumentRoot /www/example2<br />
77 ServerName www.example2.org<br />
79 # Other directives here<br />
85 <p>The asterisks match all addresses, so the main server serves no
86 requests. Due to the fact that <code>www.example1.com</code> is first
87 in the configuration file, it has the highest priority and can be seen
88 as the <cite>default</cite> or <cite>primary</cite> server. That means
89 that if a request is received that does not match one of the specified
90 <code>ServerName</code> directives, it will be served by this first
91 <code>VirtualHost</code>.</p>
96 <p>You can, if you wish, replace <code>*</code> with the actual
97 IP address of the system. In that case, the argument to
98 <code>VirtualHost</code> <em>must</em> match the argument to
99 <code>NameVirtualHost</code>:</p>
102 NameVirtualHost 172.20.30.40<br />
104 <VirtualHost 172.20.30.40><br />
108 <p>However, it is additionally useful to use <code>*</code>
109 on systems where the IP address is not predictable - for
110 example if you have a dynamic IP address with your ISP, and
111 you are using some variety of dynamic DNS solution. Since
112 <code>*</code> matches any IP address, this configuration
113 would work without changes whenever your IP address
117 <p>The above configuration is what you will want to use in almost
118 all name-based virtual hosting situations. The only thing that this
119 configuration will not work for, in fact, is when you are serving
120 different content based on differing IP addresses or ports.</p>
124 <section id="twoips"><title>Name-based hosts on more than one
128 <title>Note</title><p>Any of the
129 techniques discussed here can be extended to any number of IP
133 <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
134 will serve the "main" server, <code>server.domain.com</code> and on the
135 other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
138 <title>Server configuration</title>
142 # This is the "main" server running on 172.20.30.40<br />
143 ServerName server.domain.com<br />
144 DocumentRoot /www/mainserver<br />
146 # This is the other address<br />
147 NameVirtualHost 172.20.30.50<br />
149 <VirtualHost 172.20.30.50><br />
151 DocumentRoot /www/example1<br />
152 ServerName www.example1.com<br />
154 # Other directives here ...<br />
157 </VirtualHost><br />
159 <VirtualHost 172.20.30.50><br />
161 DocumentRoot /www/example2<br />
162 ServerName www.example2.org<br />
164 # Other directives here ...<br />
170 <p>Any request to an address other than <code>172.20.30.50</code> will be
171 served from the main server. A request to <code>172.20.30.50</code> with an
172 unknown hostname, or no <code>Host:</code> header, will be served from
173 <code>www.example1.com</code>.</p>
177 <section id="intraextra"><title>Serving the same content on
178 different IP addresses (such as an internal and external
181 <p>The server machine has two IP addresses (<code>192.168.1.1</code>
182 and <code>172.20.30.40</code>). The machine is sitting between an
183 internal (intranet) network and an external (internet) network. Outside
184 of the network, the name <code>server.example.com</code> resolves to
185 the external address (<code>172.20.30.40</code>), but inside the
186 network, that same name resolves to the internal address
187 (<code>192.168.1.1</code>).</p>
189 <p>The server can be made to respond to internal and external requests
190 with the same content, with just one <code>VirtualHost</code>
194 <title>Server configuration</title>
196 NameVirtualHost 192.168.1.1<br />
197 NameVirtualHost 172.20.30.40<br />
199 <VirtualHost 192.168.1.1 172.20.30.40><br />
201 DocumentRoot /www/server1<br />
202 ServerName server.example.com<br />
203 ServerAlias server<br />
208 <p>Now requests from both networks will be served from the same
209 <code>VirtualHost</code>.</p>
212 <title>Note:</title><p>On the internal
213 network, one can just use the name <code>server</code> rather
214 than the fully qualified host name
215 <code>server.example.com</code>.</p>
217 <p>Note also that, in the above example, you can replace the list
218 of IP addresses with <code>*</code>, which will cause the server to
219 respond the same on all addresses.</p>
224 <section id="port"><title>Running different sites on different
227 <p>You have multiple domains going to the same IP and also want to
228 serve multiple ports. By defining the ports in the "NameVirtualHost"
229 tag, you can allow this to work. If you try using <VirtualHost
230 name:port> without the NameVirtualHost name:port or you try to use
231 the Listen directive, your configuration will not work.</p>
234 <title>Server configuration</title>
239 NameVirtualHost 172.20.30.40:80<br />
240 NameVirtualHost 172.20.30.40:8080<br />
242 <VirtualHost 172.20.30.40:80><br />
244 ServerName www.example1.com<br />
245 DocumentRoot /www/domain-80<br />
247 </VirtualHost><br />
249 <VirtualHost 172.20.30.40:8080><br />
251 ServerName www.example1.com<br />
252 DocumentRoot /www/domain-8080<br />
254 </VirtualHost><br />
256 <VirtualHost 172.20.30.40:80><br />
258 ServerName www.example2.org<br />
259 DocumentRoot /www/otherdomain-80<br />
261 </VirtualHost><br />
263 <VirtualHost 172.20.30.40:8080><br />
265 ServerName www.example2.org<br />
266 DocumentRoot /www/otherdomain-8080<br />
273 <section id="ip"><title>IP-based virtual hosting</title>
275 <p>The server has two IP addresses (<code>172.20.30.40</code> and
276 <code>172.20.30.50</code>) which resolve to the names
277 <code>www.example1.com</code> and <code>www.example2.org</code>
281 <title>Server configuration</title>
285 <VirtualHost 172.20.30.40><br />
287 DocumentRoot /www/example1<br />
288 ServerName www.example1.com<br />
290 </VirtualHost><br />
292 <VirtualHost 172.20.30.50><br />
294 DocumentRoot /www/example2<br />
295 ServerName www.example2.org<br />
300 <p>Requests for any address not specified in one of the
301 <code><VirtualHost></code> directives (such as
302 <code>localhost</code>, for example) will go to the main server, if
307 <section id="ipport"><title>Mixed port-based and ip-based virtual
310 <p>The server machine has two IP addresses (<code>172.20.30.40</code> and
311 <code>172.20.30.50</code>) which resolve to the names
312 <code>www.example1.com</code> and <code>www.example2.org</code>
313 respectively. In each case, we want to run hosts on ports 80 and
317 <title>Server configuration</title>
319 Listen 172.20.30.40:80<br />
320 Listen 172.20.30.40:8080<br />
321 Listen 172.20.30.50:80<br />
322 Listen 172.20.30.50:8080<br />
324 <VirtualHost 172.20.30.40:80><br />
326 DocumentRoot /www/example1-80<br />
327 ServerName www.example1.com<br />
329 </VirtualHost><br />
331 <VirtualHost 172.20.30.40:8080><br />
333 DocumentRoot /www/example1-8080<br />
334 ServerName www.example1.com<br />
336 </VirtualHost><br />
338 <VirtualHost 172.20.30.50:80><br />
340 DocumentRoot /www/example2-80<br />
341 ServerName www.example1.org<br />
343 </VirtualHost><br />
345 <VirtualHost 172.20.30.50:8080><br />
347 DocumentRoot /www/example2-8080<br />
348 ServerName www.example2.org<br />
355 <section id="mixed"><title>Mixed name-based and IP-based
358 <p>On some of my addresses, I want to do name-based virtual hosts, and
359 on others, IP-based hosts.</p>
362 <title>Server configuration</title>
366 NameVirtualHost 172.20.30.40<br />
368 <VirtualHost 172.20.30.40><br />
370 DocumentRoot /www/example1<br />
371 ServerName www.example1.com<br />
373 </VirtualHost><br />
375 <VirtualHost 172.20.30.40><br />
377 DocumentRoot /www/example2<br />
378 ServerName www.example2.org<br />
380 </VirtualHost><br />
382 <VirtualHost 172.20.30.40><br />
384 DocumentRoot /www/example3<br />
385 ServerName www.example3.net<br />
387 </VirtualHost><br />
390 <VirtualHost 172.20.30.50><br />
392 DocumentRoot /www/example4<br />
393 ServerName www.example4.edu<br />
395 </VirtualHost><br />
397 <VirtualHost 172.20.30.60><br />
399 DocumentRoot /www/example5<br />
400 ServerName www.example5.gov<br />
407 <section id="default"><title>Using <code>_default_</code>
410 <section id="defaultallports"><title><code>_default_</code> vhosts
411 for all ports</title>
413 <p>Catching <em>every</em> request to any unspecified IP address and
414 port, <em>i.e.</em>, an address/port combination that is not used for
415 any other virtual host.</p>
418 <title>Server configuration</title>
420 <VirtualHost _default_:*><br />
422 DocumentRoot /www/default<br />
427 <p>Using such a default vhost with a wildcard port effectively prevents
428 any request going to the main server.</p>
430 <p>A default vhost never serves a request that was sent to an
431 address/port that is used for name-based vhosts. If the request
432 contained an unknown or no <code>Host:</code> header it is always
433 served from the primary name-based vhost (the vhost for that
434 address/port appearing first in the configuration file).</p>
436 <p>You can use <directive module="mod_alias">AliasMatch</directive> or
437 <directive module="mod_rewrite">RewriteRule</directive> to rewrite any
438 request to a single information page (or script).</p>
441 <section id="defaultdifferentports"><title><code>_default_</code> vhosts
442 for different ports</title>
444 <p>Same as setup 1, but the server listens on several ports and we want
445 to use a second <code>_default_</code> vhost for port 80.</p>
448 <title>Server configuration</title>
450 <VirtualHost _default_:80><br />
452 DocumentRoot /www/default80<br />
455 </VirtualHost><br />
457 <VirtualHost _default_:*><br />
459 DocumentRoot /www/default<br />
465 <p>The default vhost for port 80 (which <em>must</em> appear before any
466 default vhost with a wildcard port) catches all requests that were sent
467 to an unspecified IP address. The main server is never used to serve a
471 <section id="defaultoneport"><title><code>_default_</code> vhosts
474 <p>We want to have a default vhost for port 80, but no other default
478 <title>Server configuration</title>
480 <VirtualHost _default_:80><br />
481 DocumentRoot /www/default<br />
486 <p>A request to an unspecified address on port 80 is served from the
487 default vhost any other request to an unspecified address and port is
488 served from the main server.</p>
493 <section id="migrate"><title>Migrating a name-based vhost to an
494 IP-based vhost</title>
496 <p>The name-based vhost with the hostname
497 <code>www.example2.org</code> (from our <a
498 href="#name">name-based</a> example, setup 2) should get its own IP
499 address. To avoid problems with name servers or proxies who cached the
500 old IP address for the name-based vhost we want to provide both
501 variants during a migration phase.<br />
502 The solution is easy, because we can simply add the new IP address
503 (<code>172.20.30.50</code>) to the <code>VirtualHost</code>
507 <title>Server configuration</title>
510 ServerName www.example1.com<br />
511 DocumentRoot /www/example1<br />
513 NameVirtualHost 172.20.30.40<br />
515 <VirtualHost 172.20.30.40 172.20.30.50><br />
517 DocumentRoot /www/example2<br />
518 ServerName www.example2.org<br />
521 </VirtualHost><br />
523 <VirtualHost 172.20.30.40><br />
525 DocumentRoot /www/example3<br />
526 ServerName www.example3.net<br />
527 ServerAlias *.example3.net<br />
533 <p>The vhost can now be accessed through the new address (as an
534 IP-based vhost) and through the old address (as a name-based
539 <section id="serverpath"><title>Using the <code>ServerPath</code>
542 <p>We have a server with two name-based vhosts. In order to match the
543 correct virtual host a client must send the correct <code>Host:</code>
544 header. Old HTTP/1.0 clients do not send such a header and Apache has
545 no clue what vhost the client tried to reach (and serves the request
546 from the primary vhost). To provide as much backward compatibility as
547 possible we create a primary vhost which returns a single page
548 containing links with an URL prefix to the name-based virtual
552 <title>Server configuration</title>
554 NameVirtualHost 172.20.30.40<br />
556 <VirtualHost 172.20.30.40><br />
558 # primary vhost<br />
559 DocumentRoot /www/subdomain<br />
560 RewriteEngine On<br />
561 RewriteRule ^/.* /www/subdomain/index.html<br />
564 </VirtualHost><br />
566 <VirtualHost 172.20.30.40><br />
567 DocumentRoot /www/subdomain/sub1<br />
569 ServerName www.sub1.domain.tld<br />
570 ServerPath /sub1/<br />
571 RewriteEngine On<br />
572 RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
575 </VirtualHost><br />
577 <VirtualHost 172.20.30.40><br />
579 DocumentRoot /www/subdomain/sub2<br />
580 ServerName www.sub2.domain.tld<br />
581 ServerPath /sub2/<br />
582 RewriteEngine On<br />
583 RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
589 <p>Due to the <directive module="core">ServerPath</directive>
590 directive a request to the URL
591 <code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
592 from the sub1-vhost.<br /> A request to the URL
593 <code>http://www.sub1.domain.tld/</code> is only
594 served from the sub1-vhost if the client sent a correct
595 <code>Host:</code> header. If no <code>Host:</code> header is sent the
596 client gets the information page from the primary host.<br />
597 Please note that there is one oddity: A request to
598 <code>http://www.sub2.domain.tld/sub1/</code> is also served from the
599 sub1-vhost if the client sent no <code>Host:</code> header.<br />
600 The <directive module="mod_rewrite">RewriteRule</directive> directives
601 are used to make sure that a client which sent a correct
602 <code>Host:</code> header can use both URL variants, <em>i.e.</em>,
603 with or without URL prefix.</p>