summaryrefslogtreecommitdiff
path: root/docs/manual/vhosts/examples.html
blob: 46697bcec4d029cb1a056eb07b387df886d9b3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML><HEAD>
<TITLE>VirtualHost Examples</TITLE>
</HEAD>

<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
<BODY
 BGCOLOR="#FFFFFF"
 TEXT="#000000"
 LINK="#0000FF"
 VLINK="#000080"
 ALINK="#FF0000"
>
<!--#include virtual="header.html" -->
<H1 ALIGN="CENTER">Virtual Host examples for common setups</H1>


<H2>Base configuration</H2>

<UL>
<LI><A HREF="#ip">IP-based vhosts only</A>
<LI><A HREF="#name">Name-based vhosts only</A>
<LI><A HREF="#mixed">Mixed name-/IP-based vhosts</A>
<LI><A HREF="#port">Port-based vhosts</A>
</UL>

<H2>Additional features</H2>

<UL>
<LI><A HREF="#default">Using <CODE>_default_</CODE> vhosts</A>
<LI><A HREF="#migrate">Migrating a named-based vhost to an IP-based vhost</A>
<LI><A HREF="#serverpath">Using the <CODE>ServerPath</CODE> directive</A>
</UL>

<HR>

<H3><A NAME="ip">IP-based vhosts only</A></H3>

<UL>

<LI><STRONG>Setup 1:</STRONG>
    The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
    and <SAMP>111.22.33.55</SAMP>)
    which resolve to the names <SAMP>server.domain.tld</SAMP> and
    <SAMP>www.otherdomain.tld</SAMP> respectively.
    The hostname <SAMP>www.domain.tld</SAMP> is an alias (CNAME)
    for <SAMP>server.domain.tld</SAMP> and will represent the
    main server.
    <P>
    <STRONG>Server configuration:</STRONG>


    <BLOCKQUOTE><PRE>
    ...
    Port 80
    DocumentRoot /www/domain
    ServerName www.domain.tld

    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/otherdomain
    ServerName www.otherdomain.tld
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    <SAMP>www.otherdomain.tld</SAMP> can only be reached through the
    address <SAMP>111.22.33.55</SAMP>, while <SAMP>www.domain.tld</SAMP>
    can only be reached through <SAMP>111.22.33.44</SAMP>
    (which represents our main server).
    </BLOCKQUOTE>
    <P>

<LI><STRONG>Setup 2:</STRONG>
    Same as setup 1, but we don't want to have a dedicated main server.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName server.domain.tld
    
    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/domain
    ServerName www.domain.tld
    ...
    &lt;/VirtualHost&gt;

    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/otherdomain
    ServerName www.otherdomain.tld
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    The main server can never catch a request, because all IP addresses
    of our machine are in use for IP-based virtual hosts
    (only <SAMP>localhost</SAMP> requests can hit the main server).
    </BLOCKQUOTE>
    <P>
    
<LI><STRONG>Setup 3:</STRONG>
    The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
    and <SAMP>111.22.33.55</SAMP>)
    which resolve to the names <SAMP>server.domain.tld</SAMP> and
    <SAMP>www-cache.domain.tld</SAMP> respectively.
    The hostname <SAMP>www.domain.tld</SAMP> is an alias (CNAME)
    for <SAMP>server.domain.tld</SAMP> and will represent the
    main server.
    <SAMP>www-cache.domain.tld</SAMP> will become our proxy-cache 
    listening on port 8080, while the web server itself uses the default
    port 80.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    Listen 111.22.33.44:80
    Listen 111.22.33.55:8080
    ServerName server.domain.tld
    
    &lt;VirtualHost 111.22.33.44:80&gt;
    DocumentRoot /www/domain
    ServerName www.domain.tld
    ...
    &lt;/VirtualHost&gt;

    &lt;VirtualHost 111.22.33.55:8080&gt;
    ServerName www-cache.domain.tld
    ...
      &lt;Directory proxy:&gt;
      order deny,allow
      deny from all
      allow from 111.22.33
      &lt;/Directory&gt;
    &lt;/VirtualHost&gt;
    </PRE>
    The main server can never catch a request, because all IP addresses
    (apart from <SAMP>localhost</SAMP>) of our machine are in use for IP-based
    virtual hosts. The web server can only be reached on the first address
    through port 80 and the proxy only on the second address through port 8080.
    </BLOCKQUOTE>
</UL>
<HR>

<H3><A NAME="name">Name-based vhosts only</A></H3>

<UL>

<LI><STRONG>Setup 1:</STRONG>
    The server machine has one IP address (<SAMP>111.22.33.44</SAMP>)
    which resolves to the name <SAMP>server.domain.tld</SAMP>.
    There are two aliases (CNAMEs) <SAMP>www.domain.tld</SAMP> and
    <SAMP>www.sub.domain.tld</SAMP> for the address <SAMP>111.22.33.44</SAMP>.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName server.domain.tld

    NameVirtualHost 111.22.33.44 

    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/domain
    ServerName www.domain.tld
    ...
    &lt;/VirtualHost&gt;
    
    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain
    ServerName www.sub.domain.tld
    ...
    &lt;/VirtualHost&gt; 
    </PRE>
    Apart from <SAMP>localhost</SAMP> there are no unspecified
    addresses/ports, therefore the main server only serves
    <SAMP>localhost</SAMP> requests. Due to the fact
    that <SAMP>www.domain.tld</SAMP> has the highest priority
    it can be seen as the <CITE>default</CITE> or
    <CITE>primary</CITE> server.
    </BLOCKQUOTE>
    <P>
   
<LI><STRONG>Setup 2:</STRONG>
    The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
    and <SAMP>111.22.33.55</SAMP>)
    which resolve to the names <SAMP>server1.domain.tld</SAMP> and
    <SAMP>server2.domain.tld</SAMP> respectively.
    The alias <SAMP>www.domain.tld</SAMP> should be used for the
    main server which should also catch any unspecified addresses. 
    We want to use a virtual host for the alias
    <SAMP>www.otherdomain.tld</SAMP> and one virtual host should
    catch any request to hostnames of the form
    <SAMP>*.sub.domain.tld</SAMP> with <SAMP>www.sub.domain.tld</SAMP>
    as its server name. The address <SAMP>111.22.33.55</SAMP> should be
    used for the virtual hosts.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName www.domain.tld
    DocumentRoot /www/domain

    NameVirtualHost 111.22.33.55

    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/otherdomain
    ServerName www.otherdomain.tld
    ...
    &lt;/VirtualHost&gt;
   
    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/subdomain
    ServerName www.sub.domain.tld
    ServerAlias *.sub.domain.tld
    ...
    &lt;/VirtualHost&gt; 
    </PRE>
    Any request to an address other than <SAMP>111.22.33.55</SAMP>
    will be served from the main server. A request to
    <SAMP>111.22.33.55</SAMP> with an unknown or no <CODE>Host:</CODE>
    header will be served from <SAMP>www.otherdomain.tld</SAMP>.
    </BLOCKQUOTE>
</UL>

<HR>

<H3><A NAME="mixed">Mixed name-/IP-based vhosts</A></H3>

<UL>

<LI><STRONG>Setup:</STRONG>
    The server machine has three IP addresses (<SAMP>111.22.33.44</SAMP>,
    <SAMP>111.22.33.55</SAMP> and <SAMP>111.22.33.66</SAMP>)
    which resolve to the names <SAMP>server.domain.tld</SAMP>,
    <SAMP>www.otherdomain1.tld</SAMP> and <SAMP>www.otherdomain2.tld</SAMP>
    respectively.
    The address <SAMP>111.22.33.44</SAMP> should we used for a couple
    of name-based vhosts and the other addresses for IP-based vhosts. 
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName server.domain.tld

    NameVirtualHost 111.22.33.44

    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/domain
    ServerName www.domain.tld
    ...
    &lt;/VirtualHost&gt;
   
    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain1
    ServerName www.sub1.domain.tld
    ...
    &lt;/VirtualHost&gt; 
    
    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain2
    ServerName www.sub2.domain.tld
    ...
    &lt;/VirtualHost&gt; 
 
    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/otherdomain1
    ServerName www.otherdomain1.tld
    ...
    &lt;/VirtualHost&gt; 
    
    &lt;VirtualHost 111.22.33.66&gt;
    DocumentRoot /www/otherdomain2
    ServerName www.otherdomain2.tld
    ...
    &lt;/VirtualHost&gt;     
    </PRE></BLOCKQUOTE>

</UL>

<HR>

<H3><A NAME="port">Port-based vhosts</A></H3>

<UL>

<LI><STRONG>Setup:</STRONG>
    The server machine has one IP address (<SAMP>111.22.33.44</SAMP>)
    which resolves to the name <SAMP>www.domain.tld</SAMP>.
    If we don't have the option to get another address or alias
    for our server we can use port-based vhosts if we need
    a virtual host with a different configuration.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Listen 80
    Listen 8080
    ServerName www.domain.tld
    DocumentRoot /www/domain

    &lt;VirtualHost 111.22.33.44:8080&gt;
    DocumentRoot /www/domain2
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    A request to <SAMP>www.domain.tld</SAMP> on port 80 is served
    from the main server and a request to port 8080 is served from
    the virtual host.
    </BLOCKQUOTE>
</UL> 
   
<HR>

<H3><A NAME="default">Using <CODE>_default_</CODE> vhosts</A></H3>

<UL>

<LI><STRONG>Setup 1:</STRONG> 
    Catching <EM>every</EM> request to any unspecified IP address and port,
    <EM>i.e.</EM>, an address/port combination that is not used for any other
    virtual host.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    &lt;VirtualHost _default_:*&gt;
    DocumentRoot /www/default
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    Using such a default vhost with a wildcard port effectively
    prevents any request going to the main server.<BR>
    A default vhost never serves a request that was sent to an
    address/port that is used for name-based vhosts. If the request
    contained an unknown or no <CODE>Host:</CODE> header it is
    always served from the primary name-based vhost (the
    vhost for that address/port appearing first in the configuration
    file).<BR>
    You can use
    <A HREF="../mod/mod_alias.html#aliasmatch"><CODE>AliasMatch</CODE></A>
    or
    <A HREF="../mod/mod_rewrite.html#RewriteRule"><CODE>RewriteRule</CODE></A>
    to rewrite any request to a single information page (or script).
    </BLOCKQUOTE>
    <P>
    
<LI><STRONG>Setup 2:</STRONG> 
    Same as setup 1, but the server listens on several ports and
    we want to use a second <CODE>_default_</CODE> vhost for port 80.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    &lt;VirtualHost _default_:80&gt;
    DocumentRoot /www/default80
    ...
    &lt;/VirtualHost&gt;
    
    &lt;VirtualHost _default_:*&gt;
    DocumentRoot /www/default
    ...
    &lt;/VirtualHost&gt;    
    </PRE>
    The default vhost for port 80 (which <EM>must</EM> appear before
    any default vhost with a wildcard port) catches all requests that
    were sent to an unspecified IP address. The main server is
    never used to serve a request.
    </BLOCKQUOTE>    
    <P>
    
<LI><STRONG>Setup 3:</STRONG> 
    We want to have a default vhost for port 80, but no other default vhosts.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    &lt;VirtualHost _default_:80&gt;
    DocumentRoot /www/default
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    A request to an unspecified address on port 80 is served from the
    default vhost any other request to an unspecified address and port
    is served from the main server.
    </BLOCKQUOTE>

</UL>

<HR>

<H3><A NAME="migrate">Migrating a name-based vhost to an IP-based vhost</A></H3>

<UL>

<LI><STRONG>Setup:</STRONG>
    The name-based vhost with the hostname
    <SAMP>www.otherdomain.tld</SAMP> (from our <A HREF="#name">name-based</A>
    example, setup 2) should get its own IP address.
    To avoid problems with name servers or proxies who cached the old
    IP address for the name-based vhost we want to provide both variants
    during a migration phase.<BR>
    The solution is easy, because we can simply add the new IP address
    (<SAMP>111.22.33.66</SAMP>) to the <CODE>VirtualHost</CODE> directive.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    Port 80
    ServerName www.domain.tld
    DocumentRoot /www/domain

    NameVirtualHost 111.22.33.55

    &lt;VirtualHost 111.22.33.55 111.22.33.66&gt;
    DocumentRoot /www/otherdomain
    ServerName www.otherdomain.tld
    ...
    &lt;/VirtualHost&gt;
   
    &lt;VirtualHost 111.22.33.55&gt;
    DocumentRoot /www/subdomain
    ServerName www.sub.domain.tld
    ServerAlias *.sub.domain.tld
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    The vhost can now be accessed through the new address (as an IP-based
    vhost) and through the old address (as a name-based vhost).
    </BLOCKQUOTE>

</UL>

<HR>

<H3><A NAME="serverpath">Using the <CODE>ServerPath</CODE> directive</A></H3>

<UL>

<LI><STRONG>Setup:</STRONG>
    We have a server with two name-based vhosts. In order to match the correct
    virtual host a client must send the correct <CODE>Host:</CODE> header.
    Old HTTP/1.0 clients do not send such a header and Apache has no clue
    what vhost the client tried to reach (and serves the request from
    the primary vhost). To provide as much backward compatibility
    as possible we create a primary vhost which returns a single page
    containing links with an URL prefix to the name-based virtual hosts.
    <P>
    <STRONG>Server configuration:</STRONG>

    <BLOCKQUOTE><PRE>
    ...
    NameVirtualHost 111.22.33.44

    &lt;VirtualHost 111.22.33.44&gt;
    # primary vhost
    DocumentRoot /www/subdomain
    RewriteEngine On
    RewriteRule ^/.* /www/subdomain/index.html
    ...
    &lt;/VirtualHost&gt;

    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain/sub1
    ServerName www.sub1.domain.tld
    ServerPath /sub1/
    RewriteEngine On
    RewriteRule ^(/sub1/.*) /www/subdomain$1 
    ...
    &lt;/VirtualHost&gt;

    &lt;VirtualHost 111.22.33.44&gt;
    DocumentRoot /www/subdomain/sub2
    ServerName www.sub2.domain.tld
    ServerPath /sub2/
    RewriteEngine On
    RewriteRule ^(/sub2/.*) /www/subdomain$1 
    ...
    &lt;/VirtualHost&gt;
    </PRE>
    Due to the <A HREF="../mod/core.html#serverpath"><CODE>ServerPath</CODE></A>
    directive a request to the
    URL <SAMP>http://www.sub1.domain.tld/sub1/</SAMP> is <EM>always</EM>
    served from the sub1-vhost. <BR>
    A request to the URL <SAMP>http://www.sub1.domain.tld/</SAMP>
    is only served from the sub1-vhost if the client sent a correct
    <CODE>Host:</CODE> header.
    If no <CODE>Host:</CODE> header is sent the client gets the
    information page from the primary host.<BR>
    Please note that there is one oddity: A request to
    <SAMP>http://www.sub2.domain.tld/sub1/</SAMP> is also served from
    the sub1-vhost if the client sent no <CODE>Host:</CODE> header. <BR>
    The <CODE>RewriteRule</CODE> directives are used to make sure that
    a client which sent a correct <CODE>Host:</CODE> header can use
    both URL variants, <EM>i.e.</EM>, with or without URL prefix.
    </BLOCKQUOTE>

</UL> 

<!--#include virtual="footer.html" -->
</BODY>
</HTML>