Whole document tree ![]() Apache HTTP Server Version 1.3Name-based Virtual Host SupportThis document describes when and how to use name-based virtual hosts.
See also: Virtual Host examples for common setups, IP-based Virtual Host Support, An In-Depth Discussion of Virtual Host Matching, and Dynamically configured mass virtual hosting. Name-based vs. IP-based Virtual HostsIP-based virtual hosts use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host. With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address. Name-based virtual hosting is usually simpler, since you need only configure your DNS server to map each hostname to the correct IP address and then configure the Apache HTTP Server to recognize the different hostnames. Name-based virtual hosting also eases the demand for scarce IP addresses. Therefore you should use name-based virtual hosting unless there is a specific reason to choose IP-based virtual hosting. Some reasons why you might consider using IP-based virtual hosting:
Using Name-based Virtual Hosts
To use name-based virtual hosting, you must designate the IP
address (and possibly port) on the server that will be accepting
requests for the hosts. This is configured using the NameVirtualHost directive.
In the normal case where any and all IP addresses on the server should
be used, you can use The next step is to create a <VirtualHost> block for
each different host that you would like to serve. The argument to the
For example, suppose that both www.domain.tld and
www.otherdomain.tld point at an IP address
that the server is listening to. Then you simply add the following
to NameVirtualHost * <VirtualHost *> ServerName www.domain.tld DocumentRoot /www/domain </VirtualHost> <VirtualHost *> ServerName www.otherdomain.tld DocumentRoot /www/otherdomain </VirtualHost> You can alternatively specify an explicit IP address in place of
the * in both the Many servers want to be accessible by more than one name. This is
possible with the
then requests for all hosts in the Finally, you can fine-tune the configuration of the virtual hosts
by placing other directives inside the
Now when a request arrives, the server will first check if it is
using an IP address that matches the As a consequence, the first listed virtual host is the
default virtual host. The Compatibility with Older BrowsersAs mentioned earlier, there are some clients who do not send the required data for the name-based virtual hosts to work properly. These clients will always be sent the pages from the first virtual host listed for that IP address (the primary name-based virtual host). There is a possible workaround with the Example configuration: NameVirtualHost 111.22.33.44 <VirtualHost 111.22.33.44> ServerName www.domain.tld ServerPath /domain DocumentRoot /web/domain </VirtualHost> What does this mean? It means that a request for any URI
beginning with "/domain" will be served from the
virtual host www.domain.tld This means that the
pages can be accessed as
In order to make this work, put a link on your primary virtual host's page to http://www.domain.tld/domain/ Then, in the virtual host's pages, be sure to use either purely relative links (e.g., "file.html" or "../icons/image.gif" or links containing the prefacing /domain/ (e.g., "http://www.domain.tld/domain/misc/file.html" or "/domain/misc/file.html"). This requires a bit of discipline, but adherence to these guidelines will, for the most part, ensure that your pages will work with all browsers, new and old. See also: ServerPath configuration example Apache HTTP Server Version 1.3![]() ![]() |