Virtual Hosting


Virtual hosting is the practice of keeping the website contents for multiple different domains or hostnames on the same server; a single installation of Apache serves requests for all of them. For instance, www.mystore.com and www.frankspage.com might both be configured in DNS to point to the same IP address on your FreeBSD machine, and Apache is responsible for serving both of them, as well as its own hostname (as specified in the ServerName directive).

As you've seen, HTTP/1.0 provided no indication of what hostname the HTTP client was trying to reach (as specified by the user), so in earlier days, virtual hosting had to be done by pointing each hostname to a separate IP address and then binding each IP address as an IP alias to the same Ethernet card. Each virtual host was then specified by IP address, so a request coming in from a web browser would always be sure to get the correct website in response. The downside was that binding these large blocks of IP addresses to the same card became unwieldynobody wants to have to make DNS changes every time he reconfigures his web serverand it caused unnecessary consumption of IP address space (which, as you saw in Chapter 22, is not infinite).

The need for this cumbersome process has been much alleviated, however, by HTTP/1.1. The mandatory Host: header specifies what hostname the client is trying to reach. Therefore, "name-based" virtual hosts are the norm on the modern web. Clients that don't support the Host: header are almost unheard-of these days, so we will be discussing only name-based virtual hosts; if you're interested in address-based virtual hosts, you can find information on them at the Apache website.

The bulk of the httpd.conf file specifies the "default" servera global set of definitions that apply to all requests that Apache receives. In the default server, the ServerName directive is used primarily for constructing 301 redirection URLs, as discussed earlier. However, you can then set up small sets of overrides to these global settings, which are used if the Host: header matches a certain specified hostname. These groupings of overrides make up what are known as virtual hosts.

Note

In Apache 2.x, the http-vhosts.conf file in the exTRa directory can be used to keep your virtual host configuration separate from the main server configuration. This is especially important if you've got dozens of virtual hosts.


Let's say your server is called stripes.example.com, and that's what your main ServerName directive specifies. To configure name-based virtual hosts, you need a NameVirtualHost directive with an argument of * (the wildcard specifies "all hostnames"), followed by as many different <VirtualHost *> blocks as you like:

NameVirtualHost * <VirtualHost *>   ServerName www.example.com   DocumentRoot /usr/local/www/data   ServerAdmin webmaster@example.com   ErrorLog logs/www.example.com-error_log   CustomLog logs/www.example.com-access_log common </VirtualHost> <VirtualHost *>   ServerName www.frankspage.com   ServerAlias frankspage.com   DocumentRoot /home/frank/public_html   ServerAdmin frank@frankspage.com   ErrorLog logs/www.frankspage.com-error_log   CustomLog logs/www.frankspage.com-access_log common </VirtualHost>


Note

<VirtualHost> directives in Apache 2.x specify not just the hostname, but also the port, in the format <VirtualHost *:80> (TCP port 80 is the HTTP standard). However, because the * argument conflicts with the configuration for SSL encryption, it should be avoided in favor of explicit IP addresses.


Inside a <VirtualHost> container, the ServerName directive determines the hostname to match against the client's Host: header, and a match will result in the corresponding set of overrides (the appropriate <VirtualHost> block being applied to the configuration). DocumentRoot specifies where in the filesystem the files asked for in the incoming request are to be found, and the ErrorLog and CustomLog directives specify alternate log files for each virtual host. ServerAlias provides a way of listing multiple alternate matching hostnames for a virtual host. You can also include any other directives you like, as long as they're allowed inside a <VirtualHost> blockapachectl configtest will tell you if they're not allowed there.

It's important to note, though, that in the setup shown earlier, a request for the default server (stripes.example.com), or for any other hostname that maps to the server's IP address but doesn't match any of the <VirtualHost> blocks, will not be answered by the default server; the default server never answers requests for any address that is specified for name-based virtual hosts (in the NameVirtualHost directive). It exists only to specify the default configuration set. Because we've used the * wildcard to specify name-based virtual hosts on all IP addresses, the default server will never itself service a request. In the absence of a <VirtualHost> matching the requested hostname, the server that responds will be the first <VirtualHost> that appears in the config file (www.example.com in this case).

A more correct configuration than the previous one, then, would be this:

NameVirtualHost * <VirtualHost *>   ServerName stripes.example.com </VirtualHost> <VirtualHost *>   ServerName www.example.com   ServerAlias *.example.com   DocumentRoot /usr/local/www/data   ServerAdmin webmaster@example.com   ErrorLog logs/www.example.com-error_log   CustomLog logs/www.example.com-access_log common </VirtualHost> <VirtualHost *>   ServerName www.frankspage.com   ServerAlias frankspage.com   DocumentRoot /home/frank/public_html   ServerAdmin frank@frankspage.com   ErrorLog logs/www.frankspage.com-error_log   CustomLog logs/www.frankspage.com-access_log common </VirtualHost>


This setup ensures that the default server configuration for stripes.example.com is the one that answers a request with no explicit Host: header.

Virtual hosts can be done in a myriad of other ways, allowing you to specify different IP addresses and ports to match certain groups of <VirtualHost> blocks. Examples of the syntax for these methods can be found at http://httpd.apache.org/docs/1.3/vhosts/.




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

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