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.2</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.2</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></p>
28
<p>This document attempts to answer the commonly-asked questions about
29
setting up virtual hosts. These scenarios are those involving multiple
30
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.
34
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#purename">Running several name-based web
35
sites on a single IP address.</a></li>
36
<li><img alt="" src="../images/down.gif" /> <a href="#twoips">Name-based hosts on more than one
38
<li><img alt="" src="../images/down.gif" /> <a href="#intraextra">Serving the same content on
39
different IP addresses (such as an internal and external
41
<li><img alt="" src="../images/down.gif" /> <a href="#port">Running different sites on different
43
<li><img alt="" src="../images/down.gif" /> <a href="#ip">IP-based virtual hosting</a></li>
44
<li><img alt="" src="../images/down.gif" /> <a href="#ipport">Mixed port-based and ip-based virtual
46
<li><img alt="" src="../images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based
48
<li><img alt="" src="../images/down.gif" /> <a href="#proxy">Using <code>Virtual_host</code> and
49
mod_proxy together</a></li>
50
<li><img alt="" src="../images/down.gif" /> <a href="#default">Using <code>_default_</code>
52
<li><img alt="" src="../images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an
53
IP-based vhost</a></li>
54
<li><img alt="" src="../images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code>
57
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
59
<h2><a name="purename" id="purename">Running several name-based web
60
sites on a single IP address.</a></h2>
62
<p>Your server has a single IP address, and multiple aliases (CNAMES)
63
point to this machine in DNS. You want to run a web server for
64
<code>www.example.com</code> and <code>www.example.org</code> on this
67
<div class="note"><h3>Note</h3><p>Creating virtual
68
host configurations on your Apache server does not magically
69
cause DNS entries to be created for those host names. You
70
<em>must</em> have the names in DNS, resolving to your IP
71
address, or nobody else will be able to see your web site. You
72
can put entries in your <code>hosts</code> file for local
73
testing, but that will work only from the machine with those
77
<div class="example"><h3>Server configuration</h3><p><code>
80
# Ensure that Apache listens on port 80<br />
83
# Listen for virtual host requests on all IP addresses<br />
84
NameVirtualHost *:80<br />
86
<VirtualHost *:80><br />
88
DocumentRoot /www/example1<br />
89
ServerName www.example.com<br />
91
# Other directives here<br />
94
</VirtualHost><br />
96
<VirtualHost *:80><br />
98
DocumentRoot /www/example2<br />
99
ServerName www.example.org<br />
101
# Other directives here<br />
107
<p>The asterisks match all addresses, so the main server serves no
108
requests. Due to the fact that <code>www.example.com</code> is first
109
in the configuration file, it has the highest priority and can be seen
110
as the <cite>default</cite> or <cite>primary</cite> server. That means
111
that if a request is received that does not match one of the specified
112
<code>ServerName</code> directives, it will be served by this first
113
<code>VirtualHost</code>.</p>
118
<p>You can, if you wish, replace <code>*</code> with the actual
119
IP address of the system. In that case, the argument to
120
<code>VirtualHost</code> <em>must</em> match the argument to
121
<code>NameVirtualHost</code>:</p>
123
<div class="example"><p><code>
124
NameVirtualHost 172.20.30.40<br />
126
<VirtualHost 172.20.30.40><br />
130
<p>However, it is additionally useful to use <code>*</code>
131
on systems where the IP address is not predictable - for
132
example if you have a dynamic IP address with your ISP, and
133
you are using some variety of dynamic DNS solution. Since
134
<code>*</code> matches any IP address, this configuration
135
would work without changes whenever your IP address
139
<p>The above configuration is what you will want to use in almost
140
all name-based virtual hosting situations. The only thing that this
141
configuration will not work for, in fact, is when you are serving
142
different content based on differing IP addresses or ports.</p>
144
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
145
<div class="section">
146
<h2><a name="twoips" id="twoips">Name-based hosts on more than one
150
<h3>Note</h3><p>Any of the
151
techniques discussed here can be extended to any number of IP
155
<p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
156
will serve the "main" server, <code>server.domain.com</code> and on the
157
other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
159
<div class="example"><h3>Server configuration</h3><p><code>
164
# This is the "main" server running on 172.20.30.40<br />
165
ServerName server.domain.com<br />
166
DocumentRoot /www/mainserver<br />
168
# This is the other address<br />
169
NameVirtualHost 172.20.30.50<br />
171
<VirtualHost 172.20.30.50><br />
172
<span class="indent">
173
DocumentRoot /www/example1<br />
174
ServerName www.example.com<br />
176
# Other directives here ...<br />
179
</VirtualHost><br />
181
<VirtualHost 172.20.30.50><br />
182
<span class="indent">
183
DocumentRoot /www/example2<br />
184
ServerName www.example.org<br />
186
# Other directives here ...<br />
192
<p>Any request to an address other than <code>172.20.30.50</code> will be
193
served from the main server. A request to <code>172.20.30.50</code> with an
194
unknown hostname, or no <code>Host:</code> header, will be served from
195
<code>www.example.com</code>.</p>
197
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
198
<div class="section">
199
<h2><a name="intraextra" id="intraextra">Serving the same content on
200
different IP addresses (such as an internal and external
203
<p>The server machine has two IP addresses (<code>192.168.1.1</code>
204
and <code>172.20.30.40</code>). The machine is sitting between an
205
internal (intranet) network and an external (internet) network. Outside
206
of the network, the name <code>server.example.com</code> resolves to
207
the external address (<code>172.20.30.40</code>), but inside the
208
network, that same name resolves to the internal address
209
(<code>192.168.1.1</code>).</p>
211
<p>The server can be made to respond to internal and external requests
212
with the same content, with just one <code>VirtualHost</code>
215
<div class="example"><h3>Server configuration</h3><p><code>
218
NameVirtualHost 192.168.1.1<br />
219
NameVirtualHost 172.20.30.40<br />
221
<VirtualHost 192.168.1.1 172.20.30.40><br />
222
<span class="indent">
223
DocumentRoot /www/server1<br />
224
ServerName server.example.com<br />
225
ServerAlias server<br />
230
<p>Now requests from both networks will be served from the same
231
<code>VirtualHost</code>.</p>
234
<h3>Note:</h3><p>On the internal
235
network, one can just use the name <code>server</code> rather
236
than the fully qualified host name
237
<code>server.example.com</code>.</p>
239
<p>Note also that, in the above example, you can replace the list
240
of IP addresses with <code>*</code>, which will cause the server to
241
respond the same on all addresses.</p>
244
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
245
<div class="section">
246
<h2><a name="port" id="port">Running different sites on different
249
<p>You have multiple domains going to the same IP and also want to
250
serve multiple ports. By defining the ports in the "NameVirtualHost"
251
tag, you can allow this to work. If you try using <VirtualHost
252
name:port> without the NameVirtualHost name:port or you try to use
253
the Listen directive, your configuration will not work.</p>
255
<div class="example"><h3>Server configuration</h3><p><code>
261
NameVirtualHost 172.20.30.40:80<br />
262
NameVirtualHost 172.20.30.40:8080<br />
264
<VirtualHost 172.20.30.40:80><br />
265
<span class="indent">
266
ServerName www.example.com<br />
267
DocumentRoot /www/domain-80<br />
269
</VirtualHost><br />
271
<VirtualHost 172.20.30.40:8080><br />
272
<span class="indent">
273
ServerName www.example.com<br />
274
DocumentRoot /www/domain-8080<br />
276
</VirtualHost><br />
278
<VirtualHost 172.20.30.40:80><br />
279
<span class="indent">
280
ServerName www.example.org<br />
281
DocumentRoot /www/otherdomain-80<br />
283
</VirtualHost><br />
285
<VirtualHost 172.20.30.40:8080><br />
286
<span class="indent">
287
ServerName www.example.org<br />
288
DocumentRoot /www/otherdomain-8080<br />
293
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
294
<div class="section">
295
<h2><a name="ip" id="ip">IP-based virtual hosting</a></h2>
297
<p>The server has two IP addresses (<code>172.20.30.40</code> and
298
<code>172.20.30.50</code>) which resolve to the names
299
<code>www.example.com</code> and <code>www.example.org</code>
302
<div class="example"><h3>Server configuration</h3><p><code>
307
<VirtualHost 172.20.30.40><br />
308
<span class="indent">
309
DocumentRoot /www/example1<br />
310
ServerName www.example.com<br />
312
</VirtualHost><br />
314
<VirtualHost 172.20.30.50><br />
315
<span class="indent">
316
DocumentRoot /www/example2<br />
317
ServerName www.example.org<br />
322
<p>Requests for any address not specified in one of the
323
<code><VirtualHost></code> directives (such as
324
<code>localhost</code>, for example) will go to the main server, if
327
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
328
<div class="section">
329
<h2><a name="ipport" id="ipport">Mixed port-based and ip-based virtual
332
<p>The server machine has two IP addresses (<code>172.20.30.40</code> and
333
<code>172.20.30.50</code>) which resolve to the names
334
<code>www.example.com</code> and <code>www.example.org</code>
335
respectively. In each case, we want to run hosts on ports 80 and
338
<div class="example"><h3>Server configuration</h3><p><code>
341
Listen 172.20.30.40:80<br />
342
Listen 172.20.30.40:8080<br />
343
Listen 172.20.30.50:80<br />
344
Listen 172.20.30.50:8080<br />
346
<VirtualHost 172.20.30.40:80><br />
347
<span class="indent">
348
DocumentRoot /www/example1-80<br />
349
ServerName www.example.com<br />
351
</VirtualHost><br />
353
<VirtualHost 172.20.30.40:8080><br />
354
<span class="indent">
355
DocumentRoot /www/example1-8080<br />
356
ServerName www.example.com<br />
358
</VirtualHost><br />
360
<VirtualHost 172.20.30.50:80><br />
361
<span class="indent">
362
DocumentRoot /www/example2-80<br />
363
ServerName www.example.org<br />
365
</VirtualHost><br />
367
<VirtualHost 172.20.30.50:8080><br />
368
<span class="indent">
369
DocumentRoot /www/example2-8080<br />
370
ServerName www.example.org<br />
375
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
376
<div class="section">
377
<h2><a name="mixed" id="mixed">Mixed name-based and IP-based
380
<p>On some of my addresses, I want to do name-based virtual hosts, and
381
on others, IP-based hosts.</p>
383
<div class="example"><h3>Server configuration</h3><p><code>
388
NameVirtualHost 172.20.30.40<br />
390
<VirtualHost 172.20.30.40><br />
391
<span class="indent">
392
DocumentRoot /www/example1<br />
393
ServerName www.example.com<br />
395
</VirtualHost><br />
397
<VirtualHost 172.20.30.40><br />
398
<span class="indent">
399
DocumentRoot /www/example2<br />
400
ServerName www.example.org<br />
402
</VirtualHost><br />
404
<VirtualHost 172.20.30.40><br />
405
<span class="indent">
406
DocumentRoot /www/example3<br />
407
ServerName www.example3.net<br />
409
</VirtualHost><br />
412
<VirtualHost 172.20.30.50><br />
413
<span class="indent">
414
DocumentRoot /www/example4<br />
415
ServerName www.example4.edu<br />
417
</VirtualHost><br />
419
<VirtualHost 172.20.30.60><br />
420
<span class="indent">
421
DocumentRoot /www/example5<br />
422
ServerName www.example5.gov<br />
427
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
428
<div class="section">
429
<h2><a name="proxy" id="proxy">Using <code>Virtual_host</code> and
430
mod_proxy together</a></h2>
432
<p>The following example allows a front-end machine to proxy a
433
virtual host through to a server running on another machine. In the
434
example, a virtual host of the same name is configured on a machine
435
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
436
used so that the desired hostname is passed through, in case we are
437
proxying multiple hostnames to a single machine.</p>
439
<div class="example"><p><code>
440
<VirtualHost *:*><br />
441
ProxyPreserveHost On<br />
442
ProxyPass / http://192.168.111.2<br />
443
ProxyPassReverse / http://192.168.111.2/<br />
444
ServerName hostname.example.com<br />
448
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
449
<div class="section">
450
<h2><a name="default" id="default">Using <code>_default_</code>
453
<h3><a name="defaultallports" id="defaultallports"><code>_default_</code> vhosts
454
for all ports</a></h3>
456
<p>Catching <em>every</em> request to any unspecified IP address and
457
port, <em>i.e.</em>, an address/port combination that is not used for
458
any other virtual host.</p>
460
<div class="example"><h3>Server configuration</h3><p><code>
463
<VirtualHost _default_:*><br />
464
<span class="indent">
465
DocumentRoot /www/default<br />
470
<p>Using such a default vhost with a wildcard port effectively prevents
471
any request going to the main server.</p>
473
<p>A default vhost never serves a request that was sent to an
474
address/port that is used for name-based vhosts. If the request
475
contained an unknown or no <code>Host:</code> header it is always
476
served from the primary name-based vhost (the vhost for that
477
address/port appearing first in the configuration file).</p>
479
<p>You can use <code class="directive"><a href="../mod/mod_alias.html#aliasmatch">AliasMatch</a></code> or
480
<code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to rewrite any
481
request to a single information page (or script).</p>
484
<h3><a name="defaultdifferentports" id="defaultdifferentports"><code>_default_</code> vhosts
485
for different ports</a></h3>
487
<p>Same as setup 1, but the server listens on several ports and we want
488
to use a second <code>_default_</code> vhost for port 80.</p>
490
<div class="example"><h3>Server configuration</h3><p><code>
493
<VirtualHost _default_:80><br />
494
<span class="indent">
495
DocumentRoot /www/default80<br />
498
</VirtualHost><br />
500
<VirtualHost _default_:*><br />
501
<span class="indent">
502
DocumentRoot /www/default<br />
508
<p>The default vhost for port 80 (which <em>must</em> appear before any
509
default vhost with a wildcard port) catches all requests that were sent
510
to an unspecified IP address. The main server is never used to serve a
514
<h3><a name="defaultoneport" id="defaultoneport"><code>_default_</code> vhosts
515
for one port</a></h3>
517
<p>We want to have a default vhost for port 80, but no other default
520
<div class="example"><h3>Server configuration</h3><p><code>
523
<VirtualHost _default_:80><br />
524
DocumentRoot /www/default<br />
529
<p>A request to an unspecified address on port 80 is served from the
530
default vhost. Any other request to an unspecified address and port is
531
served from the main server.</p>
534
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
535
<div class="section">
536
<h2><a name="migrate" id="migrate">Migrating a name-based vhost to an
537
IP-based vhost</a></h2>
539
<p>The name-based vhost with the hostname
540
<code>www.example.org</code> (from our <a href="#name">name-based</a> example, setup 2) should get its own IP
541
address. To avoid problems with name servers or proxies who cached the
542
old IP address for the name-based vhost we want to provide both
543
variants during a migration phase.</p>
546
The solution is easy, because we can simply add the new IP address
547
(<code>172.20.30.50</code>) to the <code>VirtualHost</code>
550
<div class="example"><h3>Server configuration</h3><p><code>
554
ServerName www.example.com<br />
555
DocumentRoot /www/example1<br />
557
NameVirtualHost 172.20.30.40<br />
559
<VirtualHost 172.20.30.40 172.20.30.50><br />
560
<span class="indent">
561
DocumentRoot /www/example2<br />
562
ServerName www.example.org<br />
565
</VirtualHost><br />
567
<VirtualHost 172.20.30.40><br />
568
<span class="indent">
569
DocumentRoot /www/example3<br />
570
ServerName www.example.net<br />
571
ServerAlias *.example.net<br />
577
<p>The vhost can now be accessed through the new address (as an
578
IP-based vhost) and through the old address (as a name-based
581
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
582
<div class="section">
583
<h2><a name="serverpath" id="serverpath">Using the <code>ServerPath</code>
586
<p>We have a server with two name-based vhosts. In order to match the
587
correct virtual host a client must send the correct <code>Host:</code>
588
header. Old HTTP/1.0 clients do not send such a header and Apache has
589
no clue what vhost the client tried to reach (and serves the request
590
from the primary vhost). To provide as much backward compatibility as
591
possible we create a primary vhost which returns a single page
592
containing links with an URL prefix to the name-based virtual
595
<div class="example"><h3>Server configuration</h3><p><code>
598
NameVirtualHost 172.20.30.40<br />
600
<VirtualHost 172.20.30.40><br />
601
<span class="indent">
602
# primary vhost<br />
603
DocumentRoot /www/subdomain<br />
604
RewriteEngine On<br />
605
RewriteRule ^/.* /www/subdomain/index.html<br />
608
</VirtualHost><br />
610
<VirtualHost 172.20.30.40><br />
611
DocumentRoot /www/subdomain/sub1<br />
612
<span class="indent">
613
ServerName www.sub1.domain.tld<br />
614
ServerPath /sub1/<br />
615
RewriteEngine On<br />
616
RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
619
</VirtualHost><br />
621
<VirtualHost 172.20.30.40><br />
622
<span class="indent">
623
DocumentRoot /www/subdomain/sub2<br />
624
ServerName www.sub2.domain.tld<br />
625
ServerPath /sub2/<br />
626
RewriteEngine On<br />
627
RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
633
<p>Due to the <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>
634
directive a request to the URL
635
<code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
636
from the sub1-vhost.<br /> A request to the URL
637
<code>http://www.sub1.domain.tld/</code> is only
638
served from the sub1-vhost if the client sent a correct
639
<code>Host:</code> header. If no <code>Host:</code> header is sent the
640
client gets the information page from the primary host.</p>
642
<p>Please note that there is one oddity: A request to
643
<code>http://www.sub2.domain.tld/sub1/</code> is also served from the
644
sub1-vhost if the client sent no <code>Host:</code> header.</p>
646
<p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives
647
are used to make sure that a client which sent a correct
648
<code>Host:</code> header can use both URL variants, <em>i.e.</em>,
649
with or without URL prefix.</p>
652
<div class="bottomlang">
653
<p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
654
<a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a> |
655
<a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
656
<a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
657
</div><div id="footer">
658
<p class="apache">Copyright 2006 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>
659
<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>
b'\\ No newline at end of file'