What is the Apache Default Virtual Host Server ?

When there are multiple name-based virtual hosts defined in a particular Apache web server setup, the first one by appearance-order or the order in which the virtual host blocks were defined, is also known as the default server or primary server.

Why is that ? Because when a client connection or incoming request is received on some address and port on the machine, httpd takes that IP address and port combination to look for matching <VirtualHost addr[:port] [addr[:port]] ...> blocks. If it finds multiple name-based virtual hosts in the lookup, it further tries to search for a virtual host whose ServerName or ServerAlias directive matches with the request’s Host header.

If no match is found then the first virtual host from the lookup list of multiple name-based virtual hosts, acts as the default server for the request, i.e., is used to serve the request. Let’s see an example:

# Main server config
Listen 80
...

<VirtualHost *:80>
  DocumentRoot /home/catchall/public_html
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName foo.com
  DocumentRoot /home/foo/public_html
  ...
</VirtualHost>

<VirtualHost *:80>
  ServerName bar.com
  ServerAlias baz.com
  DocumentRoot /home/bar/public_html
  ...
</VirtualHost>

In the sample configuration above, we’ve defined three name-based virtual hosts or namevhosts on *:80. The first one will act as the default or primary server, i.e., whenever a request comes on this machine on any IP address and port 80, if the Host header is absent or contains any value other than foo.com or bar.com, then the request will be served by the first virtual host (default server).

Here’s a dump of parsed vhost settings from one of my servers that lists an IP-based virtual host along with a bunch of other Name-based virtual hosts out of which the first one is the default server.

$ httpd -D DUMP_VHOSTS
VirtualHost configuration:
*:80                   143.110.176.71 (/etc/httpd/conf.d/welcome.conf:29)
143.110.176.71:8080    is a NameVirtualHost
         default server centos-s-1vcpu-1gb-blr1-01 (/etc/httpd/conf.d/welcome.conf:38)
         port 8080 namevhost centos-s-1vcpu-1gb-blr1-01 (/etc/httpd/conf.d/welcome.conf:38)
         port 8080 namevhost centos-s-1vcpu-1gb-blr1-01 (/etc/httpd/conf.d/welcome.conf:42)

The entire request matching logic of Apache is explained in this guide and the difference between IP-based and Name-based virtual hosts are explained here.

Leave a Reply

Your email address will not be published. Required fields are marked *