VirtualHost Examples
Ensure that Apache listens on port 80 Listen 80 DocumentRoot /www/example1 ServerName www.example.com
Other directives here DocumentRoot /www/example2 ServerName www.example.org
Other directives here
This is the "main" server running on 172.20.30.40 ServerName server.example.com DocumentRoot /www/mainserver DocumentRoot /www/example1 ServerName www.ex
Other directives here ... DocumentRoot /www/example2 ServerName www.example.org
Other directives here ...
IP-based DocumentRoot /www/example4 ServerName www.example.edu DocumentRoot /www/example5 ServerName www.exampl
... DocumentRoot /www/default
...
... DocumentRoot /www/example3 ServerName www.example.net ServerAlias *.example.net
primary vhost DocumentRoot /www/subdomain RewriteEngine On RewriteRule . /www/subdomain/index.html
... DocumentRoot /www/subdomain/sub1 ServerName www.sub1.domain.tld ServerPath /sub1/ RewriteEngine On RewriteRule ^(/sub1/.*) /www/subdom
... DocumentRoot /www/subdomain/sub2 ServerName www.sub2.domain.tld ServerPath /sub2/ RewriteEngine On RewriteRule ^(/sub2/.*) /www/subdom
directiveYour server has a single IP address, and multiple aliases (CNAMES) point to this machine in DNS. You want to run a web server for www.example.com
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
The asterisks match all addresses, so the main server serves no requests. Due to the fact that the virtual host with ServerName 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
You can, if you wish, replace *
with the actual IP address of the system, when you don't care to discriminate based on the IP address or port.
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.
Any of the techniques discussed here can be extended to any number of IP addresses.
), we will serve two or more virtual hosts.
with an unknown hostname, or no Host:
header, will be served from www.example.com
The server machine has two IP addresses (192.168.1.1
). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name server.example.com
), but inside the network, that same name resolves to the internal address (192.168.1.1
The server can be made to respond to internal and external requests with the same content, with just one VirtualHost
Now requests from both networks will be served from the same VirtualHost
rather than the fully qualified host name server.example.com
You have multiple domains going to the same IP and also want to serve multiple ports. The example below illustrates that the name-matching takes place after the best matching IP address and port combination is determined.
Listen 80 Listen 8080
respectively. In each case, we want to run hosts on ports 80 and 8080.
Any address mentioned in the argument to a virtualhost that never appears in another virtual host is a strictly IP-based virtual host.
and mod_proxy togetherThe following example allows a front-end machine to proxy a virtual host through to a server running on another machine. In the example, a virtual host of the same name is configured on a machine at 192.168.111.2
directive is used so that the desired hostname is passed through, in case we are proxying multiple hostnames to a single machine.ProxyPreserveHost On
vhosts for all portsCatching every request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host.
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).
to rewrite any request to a single information page (or script).RewriteRule
vhosts for different portsSame as setup 1, but the server listens on several ports and we want to use a second default
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.
in a virtual host declaration will have higher precedence than default
(from our name-based example, setup 2) should get its own IP address. To avoid problems with name servers or proxies who cached the old IP address for the name-based vhost we want to provide both variants during a migration phase.
The solution is easy, because we can simply add the new IP address (172.20.30.50
directiveWe have a server with two name-based vhosts. In order to match the correct virtual host a client must send the correct Host:
header. Old HTTP/1.0 clients do not send such a header and Apache has no clue what vhost the client tried to reach (and serves the request from the primary vhost). To provide as much backward compatibility as possible we create a primary vhost which returns a single page containing links with an URL prefix to the name-based virtual hosts.
Please note that there is one oddity: A request to http://www.sub2.domain.tld/sub1/
directives are used to make sure that a client which sent a correct RewriteRuleHost:
Modules | Directives | FAQ | Glossary | Sitemap
Apache HTTP Server Version 2.4
VirtualHost Examples
Available Languages: en | fr | ja | ko | tr
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.
- Running several name-based web sites on a single IP address.
- Name-based hosts on more than one IP address.
- Serving the same content on different IP addresses (such as an internal and external address).
- Running different sites on different ports.
- IP-based virtual hosting
- Mixed port-based and ip-based virtual hosts
- Mixed name-based and IP-based vhosts
- Using Virtual_host and mod_proxy together
- Using _default_ vhosts
- Migrating a name-based vhost to an IP-based vhost
- Using the ServerPath directive
Virtual_host
_default_
ServerPath