Penguin
Diff: NameVirtualHosting
EditPageHistoryDiffInfoLikePages

Differences between current version and predecessor to the previous major change of NameVirtualHosting.

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 5 Last edited on Sunday, February 25, 2007 12:07:34 pm by TimCareySmith
Older page: version 2 Last edited on Wednesday, July 27, 2005 5:25:15 pm by JohnMcPherson Revert
@@ -1,31 +1,74 @@
-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:  
-  
- in httpd.conf: 
+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>
  
 <verbatim> 
-NameVirtualHost 12.34.56.78  
+NameVirtualHost *  
  
-<VirtualHost 12.34.56.78
+<VirtualHost *
  ServerName example.com 
  ServerAlias www.example.com 
- ServerAdmin yourmum @example.com 
+ ServerAdmin webmaster @example.com 
  DocumentRoot /var/www/virtual/example.com 
  CustomLog /var/log/apache/example.com-access.log combined 
  ErrorLog /var/log/apache/example.com-error.log 
 </VirtualHost> 
  
+<VirtualHost *>  
+ ServerName example.net  
+ ServerAlias www.example.net  
+ ServerAdmin webmaster2@example.net  
+ DocumentRoot /var/www/virtual/example.net  
+ CustomLog /var/log/apache/example.net-access.log combined  
+ ErrorLog /var/log/apache/example.net-error.log  
+</VirtualHost>  
+</verbatim>  
  
-<VirtualHost 12.34.56.78>  
- ServerName example2 .com  
- ServerAlias www.example2 .com  
- ServerAdmin yourmum @example2 .com  
- DocumentRoot /var/www/virtual/example2 .com  
- CustomLog /var/log/apache/example2 .com -access.log combined  
- ErrorLog /var/log/apache/example2 .com -error.log 
+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.  
+  
+<verbatim>  
+NameVirtualHost 12.34.56.78:80  
+  
+ <VirtualHost 12.34.56.78:80 >  
+ ServerName example .com  
+ ...  
+</verbatim>  
+  
+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].  
+  
+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.  
+  
+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:  
+  
+<verbatim>  
+NameVirtualHost *  
+  
+<VirtualHost *>  
+ ServerName www.example.co.nz  
+ ServerAdmin webmaster @example .co.nz  
+ DocumentRoot /var/www/virtual/example .co.nz  
+ CustomLog /var/log/apache/example .co.nz -access.log combined  
+ ErrorLog /var/log/apache/example .co.nz -error.log 
 </VirtualHost> 
+  
+<VirtualHost *>  
+ ServerName aliases.for.www.example.co.nz  
+ ServerAlias example.co.nz  
+ ServerAlias example.com  
+ ServerAlias www.example.com  
+ RedirectMatch 301 /?(.*) http://www.example.co.nz/$1  
+</VirtualHost>  
+</verbatim>  
+  
+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>.  
+  
+There are other ways to achieve this, such as using mod_rewrite, but none of them is as simple as this one.  
+  
+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:  
+  
+<verbatim>  
+ port 80 namevhost www.example.co.nz  
+ (/etc/apache2/sites-enabled/ubersite.conf:3)  
+ port 80 namevhost aliases.for.www.example.co.nz  
+ (/etc/apache2/sites-enabled/ubersite.conf:11)  
 </verbatim> 
  
-The Name or IP used in the ~NameVirtualHost directive must be the same name or ip used in each ~VirtualHost. 
+This allows you to easily identify the alias <tt> ~VirtualHost</tt>s