Virtual hosting & Sample cases

Virtual hosting

Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server (or pool of servers). This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name. The term virtual hosting is usually used in reference to web servers but the principles do carry over to other internet services.

There are two main types of virtual hosting, name-based and IP-based.

Name-based virtual hosting uses the host name presented by the client. This saves IP addresses and the associated administrative overhead but the protocol being served must supply the host name at an appropriate point. In particular, there are significant difficulties using name-based virtual hosting with SSL/TLS.

IP-based virtual hosting uses a separate IP address for each host name and it can be performed with any protocol but requires a dedicated IP address per domain name served. Port-based virtual hosting is also possible in principle but is rarely used in practice because it is unfriendly to users.

Name-based and IP-based virtual hosting can be combined: a server may have multiple IP addresses and serve multiple names on some or all of those IP addresses. This technique can be useful when using SSL/TLS with wildcard certificates. For example, if a server operator had two certificates, one for *.example.com and one for *.example.net, he could serve foo.example.com and bar.example.com off the same IP address but would need a separate IP address for baz.example.net.

Virtual Host Examples

Running several name-based web sites on a single IP address.

Your 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.comand www.example.org on this machine.

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.

# Ensure that Apache listens on port 80

Listen 80

<VirtualHost *:80>
DocumentRoot “/www/example1”
ServerName www.example.com
# Other directives here
</VirtualHost>

<VirtualHost *:80>
DocumentRoot “/www/example2”
ServerName www.example.org
# Other directives here
</VirtualHost>

Name-based hosts on more than one IP address.

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.example.com and on the other (172.20.30.50), we will serve two or more virtual hosts.

Listen 80
# This is the “main” server running on 172.20.30.40
ServerName server.example.com
DocumentRoot “/www/mainserver”

<VirtualHost 172.20.30.50>
DocumentRoot “/www/example1”
ServerName www.example.com
# Other directives here …
</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot “/www/example2”
ServerName www.example.org
# Other directives here …
</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 noHost: header, will be served from www.example.com

Serving the same content on different IP addresses (such as an internal and external address).

The server machine has two IP addresses (192.168.1.1 and 172.20.30.40). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name server.example.com resolves to the external address (172.20.30.40), 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 section.

<VirtualHost 192.168.1.1 172.20.30.40>
DocumentRoot “/www/server1”
ServerName server.example.com
ServerAlias server
</VirtualHost>

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.

Running different sites on different ports.

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

<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>

IP-based virtual hosting

The server has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example.com and www.example.orgrespectively.

Listen 80

<VirtualHost 172.20.30.40>
DocumentRoot “/www/example1”
ServerName www.example.com
</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot “/www/example2”
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.

Mixed port-based and ip-based virtual hosts

The server machine has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example.com and www.example.orgrespectively. In each case, we want to run hosts on ports 80 and 8080.

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/example1-80”
ServerName www.example.com
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
DocumentRoot “/www/example1-8080”
ServerName www.example.com
</VirtualHost>

<VirtualHost 172.20.30.50:80>
DocumentRoot “/www/example2-80”
ServerName www.example.org
</VirtualHost>

<VirtualHost 172.20.30.50:8080>
DocumentRoot “/www/example2-8080”
ServerName www.example.org
</VirtualHost>

Mixed name-based and IP-based vhosts

Any address mentioned in the argument to a virtualhost that never appears in another virtual host is a strictly IP-based virtual host.

Listen 80
<VirtualHost 172.20.30.40>
DocumentRoot “/www/example1”
ServerName www.example.com
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot “/www/example2”
ServerName www.example.org
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot “/www/example3”
ServerName www.example.net
</VirtualHost>

# IP-based
<VirtualHost 172.20.30.50>
DocumentRoot “/www/example4”
ServerName www.example.edu
</VirtualHost>

<VirtualHost 172.20.30.60>
DocumentRoot “/www/example5”
ServerName www.example.gov
</VirtualHost>