Virtual Host Examples - Apache HTTP Server
Virtual Host Examples - Apache HTTP Server
Note
Creating virtual host configurations on your Apache server does not magically cause DNS entries to be
created for those host names. You must have the names in DNS, resolving to your IP address, or
nobody else will be able to see your web site. You can put entries in your hosts file for local testing,
but that will work only from the machine with those hosts entries.
Server configuration
# Ensure that Apache listens on port 80
Listen 80
<VirtualHost *:80>
DocumentRoot /www/example 1
ServerName www.example .com
</VirtualHost >
<VirtualHost *:80>
DocumentRoot /www/example 2
ServerName www.example .org
</VirtualHost >
The asterisks match all addresses, so the main server serves no requests. Due to the fact that
www.example.com is first in the configuration file, it has the highest priority and can be seen as the default
or primary server. That means that if a request is received that does not match one of the specified
ServerName directives, it will be served by this first VirtualHost.
Note
You can, if you wish, replace * with the actual IP address of the system. In that case, the argument to
VirtualHost must match the argument to NameVirtualHost:
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
# etc ...
However, it is additionally useful to use * on systems where the IP address is not predictable - for
example if you have a dynamic IP address with your ISP, and you are using some variety of dynamic
DNS solution. Since * matches any IP address, this configuration would work without changes
whenever your IP address changes.
The above configuration is what you will want to use in almost all name-based virtual hosting situations. The
only thing that this configuration will not work for, in fact, is when you are serving different content based on
differing IP addresses or ports.
Note
Any of the techniques discussed here can be extended to any number of IP addresses.
The server has two IP addresses. On one (172.20.30.40), we will serve the "main" server,
server.domain.com and on the other (172.20.30.50), we will serve two or more virtual hosts.
Server configuration
Listen 80
<VirtualHost 172.20.30.50>
DocumentRoot /www/example 1
ServerName www.example .com
</VirtualHost >
<VirtualHost 172.20.30.50>
DocumentRoot /www/example 2
ServerName www.example .org
</VirtualHost >
Any request to an address other than 172.20.30.50 will be served from the main server. A request to
172.20.30.50 with an unknown hostname, or no Host: header, will be served from
www.example.com.
The server can be made to respond to internal and external requests with the same content, with just one
VirtualHost section.
Server configuration
NameVirtualHost 192.168.1.1
NameVirtualHost 172.20.30.40
Now requests from both networks will be served from the same VirtualHost.
Note:
On the internal network, one can just use the name server rather than the fully qualified host name
server.example.com.
Note also that, in the above example, you can replace the list of IP addresses with *, which will cause
the server to respond the same on all addresses.
Server configuration
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
<VirtualHost 172.20.30.40:80>
ServerName www.example .com
DocumentRoot /www/domain-80
</VirtualHost >
<VirtualHost 172.20.30.40:8080>
ServerName www.example .com
DocumentRoot /www/domain-8080
</VirtualHost >
<VirtualHost 172.20.30.40:80>
ServerName www.example .org
DocumentRoot /www/otherdomain -80
</VirtualHost >
<VirtualHost 172.20.30.40:8080>
ServerName www.example .org
DocumentRoot /www/otherdomain -8080
</VirtualHost >
Server configuration
Listen 80
<VirtualHost 172.20.30.40>
DocumentRoot /www/example 1
ServerName www.example .com
</VirtualHost >
<VirtualHost 172.20.30.50>
DocumentRoot /www/example 2
ServerName www.example .org
</VirtualHost >
Requests for any address not specified in one of the <VirtualHost> directives (such as localhost, for
example) will go to the main server, if there is one.
Server configuration
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example 1-80
ServerName www.example .com
</VirtualHost >
<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example 1-8080
ServerName www.example .com
</VirtualHost >
<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example 2-80
ServerName www.example .org
</VirtualHost >
<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example 2-8080
ServerName www.example .org
</VirtualHost >
Server configuration
Listen 80
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
DocumentRoot /www/example 1
ServerName www.example .com
</VirtualHost >
<VirtualHost 172.20.30.40>
DocumentRoot /www/example 2
ServerName www.example .org
</VirtualHost >
<VirtualHost 172.20.30.40>
DocumentRoot /www/example 3
ServerName www.example 3.net
</VirtualHost >
# IP-based
<VirtualHost 172.20.30.50>
DocumentRoot /www/example 4
ServerName www.example 4.edu
</VirtualHost >
<VirtualHost 172.20.30.60>
DocumentRoot /www/example 5
ServerName www.example 5.gov
</VirtualHost >
<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://192.168.111.2
ProxyPassReverse / http://192.168.111.2/
ServerName hostname .example .com
</VirtualHost >
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).
Server configuration
<VirtualHost _default _:80>
DocumentRoot /www/default 80
# ...
</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.
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.
The solution is easy, because we can simply add the new IP address (172.20.30.50) to the
VirtualHost directive.
Server configuration
Listen 80
ServerName www.example .com
DocumentRoot /www/example 1
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
DocumentRoot /www/example 3
ServerName www.example .net
ServerAlias *.example .net
# ...
</VirtualHost >
The vhost can now be accessed through the new address (as an IP-based vhost) and through the old address (as
a name-based vhost).
Server configuration
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
# primary vhost
DocumentRoot /www/subdomain
RewriteEngine On
RewriteRule ^/.* /www/subdomain/index.html
# ...
</VirtualHost >
<VirtualHost 172.20.30.40>
DocumentRoot /www/subdomain/sub1
ServerName www.sub1.domain.tld
ServerPath /sub1/
RewriteEngine On
RewriteRule ^(/sub1/.*) /www/subdomain$1
# ...
</VirtualHost >
<VirtualHost 172.20.30.40>
DocumentRoot /www/subdomain/sub2
ServerName www.sub2.domain.tld
ServerPath /sub2/
RewriteEngine On
RewriteRule ^(/sub2/.*) /www/subdomain$1
# ...
</VirtualHost >
Please note that there is one oddity: A request to http://www.sub2.domain.tld/sub1/ is also served
from the sub1-vhost if the client sent no Host: header.
The RewriteRule directives are used to make sure that a client which sent a correct Host: header can use
both URL variants, i.e. , with or without URL prefix.