Wednesday, March 23, 2011

VirtualHost Examples: Using _default_ vhosts

This document attempts to answer the commonly-asked questions about setting up virtual hosts.

These scenarios are those involving multiple web sites running on a single server, via name-based or IP-based virtual hosts.

_default_ vhosts for all ports

Catching every request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host.
##############################################
Server configuration
<VirtualHost _default_:*>
DocumentRoot /www/default
</VirtualHost>
##############################################

Using such a default vhost with a wildcard port effectively prevents any request going to the main server.

A default vhost never serves a request that was sent to an address/port that is used for name-based vhosts. If the request contained an unknown or no Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file).

You can use AliasMatch or RewriteRule to rewrite any request to a single information page (or script).
_default_ vhosts for different ports

Same as setup 1, but the server listens on several ports and we want to use a second _default_ vhost for port 80.

##############################################
Server configuration

<VirtualHost _default_:80>
DocumentRoot /www/default80
# ...
</VirtualHost>
<VirtualHost _default_:*>
DocumentRoot /www/default
# ...
</VirtualHost>
#############################################
The default vhost for port 80 (which must appear before any default vhost with a wildcard port) catches all requests that were sent to an unspecified IP address. The main server is never used to serve a request.
_default_ vhosts for one port

We want to have a default vhost for port 80, but no other default vhosts.
###############################
Server configuration
<VirtualHost _default_:80>
DocumentRoot /www/default
...
</VirtualHost>
################################
A request to an unspecified address on port 80 is served from the default vhost any other request to an unspecified address and port is served from the main server.

No comments:

Post a Comment