Httpd Virtual Host Configuration

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Name-Based Virtual Host Configuration

Virtual hosts can be declared using the <VirtualHost> configuration element. <VirtualHost> encloses a group of directives that apply to only a particular virtual host. Each <VirtualHost> is associated with an IP:port pair.

<VirtualHost ip:port>
</VirtualHost>

ip could be *

Start by declaring a NameVirtualHost directive that defines the IP:port pair on which the server will receive requests for the name-based virtual hosts. Note that the IP:port pair must exactly match the IP:port pairs declared as part of the <VirtualHost IP:port> declarations.

NameVirtualHost 1.2.3.4:80

More details about NameVirtualHost are available here: NameVirtualHost.

For each virtual host you want to serve, create a <VirtualHost> section.

The <VirtualHost> section must contain at minimum a ServerName directive to designate what host to serve and a DocumentRoot directive to indicate the file system directory that contains the content to server.

Name-Based Virtual Host Resolution

When a request arrives, the server must find the best (most specific) matching <VirtualHost> configuration based on the IP address and the port the request came on. If just one <VirtualHost> is identified, that's the one that is used. If there is more than one virtual host that matches the IP:port, httpd will further compare the ServerName and ServerAlias directives to the value of the "Host" header arrived with the request. If no ServerName/ServerAlias match is found, then the first listed virtual host is used.

An IP:port Match Must Occur

Note that an IP:port match must occur, otherwise the main server will be used to serve the request, even if there are declared virtual servers with matching ServerNames.

The Default Virtual Host

It is usually wise to create a default virtual host with a ServerName matching that of the base server. New domains on the same interface and port, but requiring separate configurations, can then be added as subsequent (non-default) virtual hosts.

Configuration Inheritance

All directives present in the httpd configuration file above the <VirtualHost> section set up the values used by the "main" server, which responds to any requests that are not handled by a <VirtualHost> definition. These values also provide defaults for any <VirtualHost> containers that defined later in the file.

If any of these directives appear inside a <VirtualHost> container, their values override the "main" defaults.

Overridable directives:

Example


#
# ... the "main" section
#

IncludeOptional conf.d/*.conf

NameVirtualHost 1.2.3.4:80

<VirtualHost 1.2.3.4:80>
    # This first-listed virtual host is also the default for 108.68.48.75:80 
    ServerName varangian.novaordis.com
    DocumentRoot "/var/www/varangian"
</VirtualHost>

<VirtualHost 1.2.3.4:80>
    ServerName praetorian.novaordis.com
    DocumentRoot "/var/www/praetorian"
</VirtualHost>