Virtual Hosting on Apache

 < Day Day Up > 



Virtual hosting allows the Apache Web server to host multiple Web sites as part of its own. In effect, the server can act as several servers, each hosted Web site appearing separate to outside users. Apache supports both IP address–based and name-based virtual hosting. IP address–based virtual hosts use valid registered IP addresses, whereas name-based virtual hosts use fully qualified domain addresses. These domain addresses are provided by the host header from the requesting browser. The server can then determine the correct virtual host to use on the basis of the domain name alone. Note that SSL servers require IP virtual hosting. See httpd.apache.org for more information.

IP Address–Based Virtual Hosts

In the IP address–based virtual hosting method, your server must have a different IP address for each virtual host. The IP address you use is already set up to reference your system. Network system administration operations can set up your machine to support several IP addresses. Your machine could have separate physical network connections for each one, or a particular connection could be configured to listen for several IP addresses at once. In effect, any of the IP addresses can access your system.

You can configure Apache to run a separate daemon for each virtual host, separately listening for each IP address, or you can have a single daemon running that listens for requests for all the virtual hosts. To set up a single daemon to manage all virtual hosts, use VirtualHost directives. To set up a separate daemon for each host, also use the Listen directive.

A VirtualHost directive block must be set up for each virtual host. Within each VirtualHost block, you place the appropriate directives for accessing a host. You should have ServerAdmin, ServerName, DocumentRoot, and TransferLog directives specifying the particular values for that host. You can use any directive within a VirtualHost block, except for ServerType (1.3), StartServers, MaxSpareServers, MinSpareServers, MaxRequestsPerChild, Listen, PidFile, TypesConfig, ServerRoot, and NameVirtualHost.

Although you can use domain names for the address in the VirtualHost directive, using the actual IP address is preferable. This way, you are not dependent on your domain name service to make the correct domain name associations. Be sure to leave an IP address for your main server. If you use all the available IP addresses for your machine for virtual hosts, you can no longer access your main server. You could, of course, reconfigure your main server as a virtual host. The following example shows two IP-based virtual hosts blocks: one using an IP address, and the other a domain name that associates with an IP address:

<VirtualHost 192.168.1.23>  ServerAdmin webmaster@mail.mypics.com  DocumentRoot /groups/mypics/html  ServerName www.mypics.com  ErrorLog /groups/mypics/logs/error_log  ..... </VirtualHost>     <VirtualHost www.myproj.org>  ServerAdmin webmaster@mail.myproj.org  DocumentRoot /groups/myproj/html  ServerName www.myproj.org  ErrorLog /groups/myproj/logs/error_log  .... </VirtualHost>

Name-Based Virtual Hosts

With IP-based virtual hosting, you are limited to the number of IP addresses your system supports. With name-based virtual hosting, you can support any number of virtual hosts using no additional IP addresses. With only a single IP address for your machine, you can still support an unlimited number of virtual hosts. Such a capability is made possible by the HTTP/1.1 protocol, which lets a server identify the name by which it is being accessed. This method requires the client, the remote user, to use a browser that supports the HTTP/1.1 protocol, as current browsers do (though older ones may not). A browser using such a protocol can send a host header specifying the particular host to use on a machine.

To implement name-based virtual hosting, use a VirtualHost directive for each host and a NameVirtualHost directive to specify the IP address you want to use for the virtual hosts. If your system has only one IP address, you need to use that address. Within the VirtualHost directives, you use the ServerName directive to specify the domain name you want to use for that host. Using ServerName to specify the domain name is important to avoid a DNS lookup. A DNS lookup failure disables the virtual host. The VirtualHost directives each take the same IP address specified in the NameVirtualHost directive as their argument. You use Apache directives within the VirtualHost blocks to configure each host separately. Name-based virtual hosting uses the domain name address specified in a host header to determine the virtual host to use. If no such information exists, the first host is used as the default. The following example implements two name-based virtual hosts. Here, www.mypics.com and www.myproj.org are implemented as name-based virtual hosts instead of IP-based hosts:

ServerName turtle.mytrek.com     NameVirtualHost 192.168.1.5     <VirtualHost 192.168.1.5>  ServerName www.mypics.com  ServerAdmin webmaster@mail.mypics.com  DocumentRoot /var/www/mypics/html  ErrorLog /var/www/mypics/logs/error_log  ... </VirtualHost>     <VirtualHost 192.168.1.5>  ServerName www.myproj.org  ServerAdmin webmaster@mail.myproj.org  DocumentRoot /var/www/myproj/html  ErrorLog /var/www/myproj/logs/error_log  .... </VirtualHost> 

If your system has only one IP address, implementing virtual hosts prevents access to your main server with that address. You could no longer use your main server as a Web server directly; you could use it only indirectly to manage your virtual host. You could configure a virtual host to manage your main server's Web pages. You would then use your main server to support a set of virtual hosts that would function as Web sites, rather than the main server operating as one site directly. If your machine has two or more IP addresses, you can use one for the main server and the other for your virtual hosts. You can even mix IP-based virtual hosts and name-based virtual hosts on your server. You can also use separate IP addresses to support different sets of virtual hosts. You can further have several domain addresses access the same virtual host. To do so, place a ServerAlias directive listing the domain names within the selected VirtualHost block.

ServerAlias www.mypics.com www.greatpics.com

Requests sent to the IP address used for your virtual hosts have to match one of the configured virtual domain names. To catch requests that do not match one of these virtual hosts, you can set up a default virtual host using _default_:*. Unmatched requests are then handled by this virtual host.

<VirtualHost _default_:*>

Dynamic Virtual Hosting

If you have implemented many virtual hosts on your server that have the same configuration, 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 version 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 webmaster@mail.mygolf.com  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

Interpolated Strings

The mod_vhots_alias module supports various interpolated strings, each beginning with a % symbol and followed by a number. As you have seen, %0 references the entire Web address. %1 references only the first segment, %2 references the second, %-1 references the last part, and %2+ references from the second part on. For example, if you want to use only the second part of a Web address for the directory name, you would use the following directives:

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

In this case, a request made for www.mygolf.com/html/mypage would use only the second part of the Web address. This would be "mygolf" in www.mygolf.com, and would evaluate to

/var/www/hosts/mygolf/html/mypage

If you used %2+ instead, as in /var/www/hosts/%2/html, the request for www.mygolf.com/html/mypage would evaluate to

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

The same method works for IP addresses, where %1 references the first IP address segment, %2 references the second, and so on.

Logs for Virtual Hosts

One drawback of dynamic virtual hosting is that you can set up only one log for all your hosts. However, you can create your own shell program to simply cut out the entries for the different hosts in that log.

LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon CustomLog logs/access_log vcommon

IP Addressing

Implementing dynamic virtual hosting in the standard way as shown previously will slow down the process, as your server will have to perform a DNS lookup to discover the name of your server using its IP address. You can avoid this step by simply using the IP address for your virtual host's directory. So, for IP virtual host 192.198.1.6, you would create a directory /var/www/hosts/192.198.1.6, with an html subdirectory for that host's document root. You would use the VirtualDocumentRootIP and VirtualScriptAliasIP directives to use IP addresses as directory names. Now the IP address can be mapped directly to the document root directory name, no longer requiring a DNS lookup. Also be sure to include the IP address in your log, %A.

UseCanonicalName DNS LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon CustomLog logs/access_log vcommon 
VirtualDocumentRootIP /var/www/hosts/%0/html VirtualScriptAliasIP /var/www/hosts/%0/cgi-bin

You can mix these commands in with other virtual host entries as you need them. For example, to specify the document root directory for a nondynamic name-based virtual host, you could simply use the VirtualDocumentRoot directive. In other words, you can simply use the same directories for both dynamic and nondynamic virtual hosts. You could still specify other directories for different nondynamic virtual hosts as you wish. In the following example, the www.mypics.com name-based virtual host uses the dynamic virtual host directive VirtualDocumentRoot to set its document root directory. It now uses /var/www/www.mypics.com/html as its document root directory. The CGI directory, however, is set as a nondynamic directory, /var/www/mypics/cgi-bin.

UseCanonicalName Off     NameVirtualHost 192.168.1.5     <VirtualHost 192.168.1.5>  ServerName www.mypics.com  ServerAdmin webmaster@mail.mypics.com  VirtualDocumentRoot /var/www/%0/html  ScriptAlias /var/www/mypics/cgi-bin  ... </VirtualHost>



 < Day Day Up > 



Red Hat(c) The Complete Reference
Red Hat Enterprise Linux & Fedora Edition (DVD): The Complete Reference
ISBN: 0072230754
EAN: 2147483647
Year: 2004
Pages: 328

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net