[ Team LiB ] |
Recipe 4.1 Setting Up Name-Based Virtual HostsProblemYou have only one IP address, but you want to support more than one web site on your system. SolutionUse the NameVirtualHost * directive in conjunction with <VirtualHost> sections: ServerName 127.0.0.1 NameVirtualHost * <VirtualHost *> ServerName TheSmiths.name DocumentRoot "C:/Apache/Sites/TheSmiths" </VirtualHost> <VirtualHost *> ServerName JohnSmith.name DocumentRoot "C:/Apache/Sites/JustJohnSmith" </VirtualHost> DiscussionWith IP addresses increasingly hard to come by, name-based virtual hosting is the most common way to run multiple web sites on the same Apache server. The previous recipe works, for most users, in most virtual hosting situations. The * in the previous rules means that the specified hosts run on all addresses. For a machine with only a single address, this means that it runs on that address but will also run on the loopback, or localhost address. Thus if you are sitting at the physical server system, you can view the web site. The argument to the <VirtualHost> container directive is the same as the argument to the NameVirtualHost directive. Putting the hostname here may ignore the virtual host on server startup, and requests to this virtual host may unexpectedly go somewhere else. If your name server is down or otherwise unresponsive at the time that your Apache server is starting up, then Apache can't match the particular <VirtualHost> section to the NameVirtualHost directive to which it belongs. Requests for which there is not a virtual host listed will go to the first virtual host listed in the configuration file. In the case of the previous example, hostnames that are not explicitly mentioned in one of the virtual hosts will be served by the TheSmiths.name virtual host. It is particularly instructive to run httpd -S and observe the virtual host configuration as Apache understands it, to see if it matches the way that you understand it. httpd -S returns the virtual host configuration, showing which hosts are name-based, which are IP-based, and what the defaults are. Multiple names can be listed for a particular virtual host using the ServerAlias directive, as shown here: ServerName TheSmiths.name ServerAlias www.TheSmiths.name Smith.Family.name It is important to understand that virtual hosts render the server listed in the main body of your configuration file no longer accessible—you must create a virtual host section explicitly for that host. List this host first, if you want it to be the default server. Adding name-based virtual hosts to your Apache configuration does not magically add entries to your DNS server. You must still add records to your DNS server so that the names resolve to the IP address of the server system. When users type your server name(s) into their browser location bars, their computers first contact a DNS server to look up that name and resolve it to an IP address. If there is no DNS record, then their browsers can't find your server. For more information on configuring your DNS server, consult the documentation for the DNS software you happen to be running, or talk to your ISP if you're not running your own DNS server. See Also |
[ Team LiB ] |