Dynamic Virtual Hosting

If you have implemented many virtual hosts that have the same configuration on your server, you can use a technique called dynamic virtual hosting to have these virtual hosts generated dynamically. The code for implementing your virtual hosts becomes much smaller, and as a result, your server accesses them faster. Adding yet more virtual hosts becomes a simple matter of creating appropriate directories and adding entries for them in the DNS server.

To make dynamic virtual hosting work, the server uses commands in the mod_vhost_ alias module (supported in Apache versions 1.3.6 and up) to rewrite both the server name and the document root to those of the appropriate virtual server (for older Apache versions before 1.3.6, you use the mod_rewrite module). Dynamic virtual hosting can be either name-based or IP-based. In either case, you have to set the UseCanonicalName directive in such a way as to allow the server to use the virtual hostname instead of the server's own name. For name-based hosting, you simply turn off UseCanonicalName. This allows your server to obtain the hostname from the host header of the user request. For IP-based hosting, you set the UseCanonicalName directive to DNS. This allows the server to look up the host in the DNS server.

UseCanonicalName Off UseCanonicalName DNS

You then have to enable the server to locate the different document root directories and CGI bin directories for your various virtual hosts. You use the VirtualDocumentRoot directive to specify the template for virtual hosts' directories. For example, if you place the different host directories in the /var/www/hosts directory, then you could set the VirtualDocumentRoot directive accordingly.

VirtualDocumentRoot /var/www/hosts/%0/html

The %0 will be replaced with the virtual host's name when that virtual host is accessed. It is important that you create the dynamic virtual host's directory using that host's name. For example, for a dynamic virtual host called www.mygolf.org, you would create a directory named /var/www/hosts/www.mygolf.org. Then create subdirectories for the document root and CGI programs, as in /var/www/hosts/www.mygolf.org/html. For the CGI directory, use the VirtualScriptAlias directive to specify the CGI subdirectory you use.

VirtualScriptAlias /var/www/hosts/%0/cgi-bin

A simple example of name-based dynamic virtual hosting directives follows: UseCanonicalName Off

VirtualDocumentRoot /var/www/hosts/%0/html VirtualScriptAlias /var/www/hosts/%0/cgi-bin

If a request was made for www.mygolf.com/html/mypage, that would evaluate to

/var/www/hosts/www.mygolf.com/html/mypage

A simple example of dynamic virtual hosting is shown here:

UseCanonicalName Off

NameVirtualHost 192.168.1.5

<VirtualHost 192.168.1.5> ServerName www.mygolf.com ServerAdmin [email protected] VirtualDocumentRoot /var/www/hosts/%0/html VirtualScriptAlias /var/www/hosts/%0/cgi-bin

</VirtualHost>

To implement IP-based dynamic virtual hosting instead, set the UseCanonicalName to DNS instead of Off.

UseCanonicalName DNS

VirtualDocumentRoot /var/www/hosts/%0/html VirtualScriptAlias /var/www/hosts/%0/cgi-bin

Was this article helpful?

0 0

Post a comment