Inhoud

Apache Name-based Virtual Hosting

Note:
The Configuration section in this document is outdated!
Please consult the official Apache documentation for actual config info.

1. IP-based versus Name-based Virtual Hosting

The Apache webserver supports two types of Virtual Hosting:

When using IP-based Virtual Hosting the webserver hosts multiple IP-addresses on its LAN card (IP-aliasing). All these IP-addresses refer to the same physical server. When a client (Webbrowser) refers to a particular IP-address the IP destination address in the IP-header will be distinctive however. The webserver can be set up such that it maps each IP-address to a different “DocumentRoot”. This is called “IP-based Virtual Hosting”.

Name-based Virtual Hosting takes this concept one step further. On a particular IP-address the webserver additionally can be configured to take in account the “Host” field of the HTTP/1.1 header: 1)

    GET / HTTP/1.1
    Host: www.company2.com
    User-Agent: Mozilla/5.0 (X11; U; Linux i586; en-US; rv:0.9.9)  ...
    Accept: text/xml,application/xml,application/xhtml+xml,text/ht ...
    ...

This behaviour is called “Name-based Virtual Hosting”.

2. Example Apache Name-based Virtual Hosting

File: /etc/apache/httpd.conf:

    NameVirtualHost typhoon
    <VirtualHost typhoon>
         ServerName www.company1.com
         DocumentRoot /var/www/company1
    </VirtualHost>
    <VirtualHost typhoon>
         ServerName www.company2.com
         DocumentRoot /var/www/company2
    </VirtualHost>

The first step is to enable “Name-based Virtual Hosting” on a particular IP-address of the server. This is the role of the “NameVirtualHost” directive. If the server has multiple IP-addresses, these addresses can be enabled for “Name-based Virtual Hosting” using multiple “NameVirtualHost” directives. As the argument of the directive an IP-address or a Hostname can be specified.
Note: If a Hostname is specified, the name must be resolvable into a server IP-address!

The next step is adding <VirtualHost > … </VirtualHost> containers for each “NameVirtualHost”.
Note that the argument string after “ServerName” must exactly match the “Host” field in the HTTP/1.1 header. Thus, when a client specifies a simple Hostname instead of the Fully Qualified Domain Name the “ServerName” directive will not match and the server will default to the first “DocumentRoot” for this IP-address.

If an IP-address is enabled for “Name-based Virtual Hosting” the main server “DocumentRoot” will never be consulted when referring to this IP-address. Other directives e.g. <Directory > … </Directory> containers remain in effect however. You can override these directives by repeating them in a <VirtualHost > … </VirtualHost> container.

.

Copyright (C) 2003 Integrated Services; Tux4u.nl
Author: Ing.J.M.Waldorp
apache-named-vhost 20030514; 2013-11-08

1)
HTTP/1.0 (very old browsers) won't work, since there is no Host field.