Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
NameVirtualHosting
Edit
PageHistory
Diff
Info
LikePages
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 * <VirtualHost *> ServerName example.com ServerAlias www.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> 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> This allows you to easily identify the alias <tt>~VirtualHost</tt>s.
2 pages link to
NameVirtualHosting
:
HostName
ApacheNotes