Penguin
Blame: NameVirtualHosting
EditPageHistoryDiffInfoLikePages
Annotated edit history of NameVirtualHosting version 5, including all changes. View license author blame.
Rev Author # Line
3 AristotlePagaltzis 1 The process of virtually hosting multiple websites on a single [IP] address by using the requested name in the [HTTP] header. An example for [Apache] would be the following configuration in <tt>httpd.conf</tt>:
1 GreigMcGill 2
3 <verbatim>
3 AristotlePagaltzis 4 NameVirtualHost *
1 GreigMcGill 5
3 AristotlePagaltzis 6 <VirtualHost *>
1 GreigMcGill 7 ServerName example.com
8 ServerAlias www.example.com
3 AristotlePagaltzis 9 ServerAdmin webmaster@example.com
1 GreigMcGill 10 DocumentRoot /var/www/virtual/example.com
11 CustomLog /var/log/apache/example.com-access.log combined
12 ErrorLog /var/log/apache/example.com-error.log
13 </VirtualHost>
14
3 AristotlePagaltzis 15 <VirtualHost *>
16 ServerName example.net
17 ServerAlias www.example.net
18 ServerAdmin webmaster2@example.net
19 DocumentRoot /var/www/virtual/example.net
20 CustomLog /var/log/apache/example.net-access.log combined
21 ErrorLog /var/log/apache/example.net-error.log
22 </VirtualHost>
23 </verbatim>
24
25 If the server has more than one [IP] address and you want it to serve a particular site only one a specific one of these addresses, you can replace the asterisks in that configuration with a particular address and optionally a port, eg.
26
27 <verbatim>
28 NameVirtualHost 12.34.56.78:80
29
30 <VirtualHost 12.34.56.78:80>
31 ServerName example.com
32 ...
33 </verbatim>
34
35 The arguments of the <tt>~NameVirtualHost</tt> and corresponding <tt>~VirtualHost</tt> directives must match ''exactly''. Of course, if your server has several [IP]s you probably want several <tt>~NameVirtualHost</tt> directives too – one for every [IP]. You can then serve a different set of sites from each [IP].
36
37 Note that you can also use things like “<tt>*:80</tt>” to serve a particular site only on a particular port, but on every [IP] of the server.
38
39 If you have a site that can be reached through several domains/hostnames, eg. with or without the <tt>www.</tt> and either <tt>.com</tt> or <tt>.co.nz</tt>, you might want to consider making one of them the canonical address. This is better for any number of reasons: it will make your referrer logs easier to process; it will make things more obvious for search engines (might improve your ranking!); people will use almost exclusively the canonical address to link to your site, making it easier to find out who is linking to you; and on and on. With Apache virtual hosting, you can do this easily; in the following example, <tt>www.example.co.nz</tt> was chosen as the canonical address:
40
41 <verbatim>
42 NameVirtualHost *
1 GreigMcGill 43
3 AristotlePagaltzis 44 <VirtualHost *>
45 ServerName www.example.co.nz
46 ServerAdmin webmaster@example.co.nz
47 DocumentRoot /var/www/virtual/example.co.nz
48 CustomLog /var/log/apache/example.co.nz-access.log combined
49 ErrorLog /var/log/apache/example.co.nz-error.log
50 </VirtualHost>
51
52 <VirtualHost *>
4 TimCareySmith 53 ServerName aliases.for.www.example.co.nz
54 ServerAlias example.co.nz
3 AristotlePagaltzis 55 ServerAlias example.com
56 ServerAlias www.example.com
57 RedirectMatch 301 /?(.*) http://www.example.co.nz/$1
1 GreigMcGill 58 </VirtualHost>
59 </verbatim>
2 JohnMcPherson 60
3 AristotlePagaltzis 61 With this configuration, any and every request for a page on one of the alias domains, eg. <tt>~http://example.com/products/</tt>, will redirect the visitor the same page in the canonical domain, in this case <tt>~http://www.example.co.nz/products/</tt>.
62
63 There are other ways to achieve this, such as using mod_rewrite, but none of them is as simple as this one.
5 TimCareySmith 64
65 The advantage with using the <tt>aliases.for.www.example.co.nz</tt> as the <tt>~ServerName</tt> in the second <tt>~VirtualHost</tt> is that when using the command <tt>apache2ctl -S</tt> you get the following output:
66
67 <verbatim>
68 port 80 namevhost www.example.co.nz
69 (/etc/apache2/sites-enabled/ubersite.conf:3)
70 port 80 namevhost aliases.for.www.example.co.nz
71 (/etc/apache2/sites-enabled/ubersite.conf:11)
72 </verbatim>
73
74 This allows you to easily identify the alias <tt>~VirtualHost</tt>s.