Why Is Apache Ignoring Some Of My Virtual Hosts?

Posted on: 10 September 2015

Why Is Apache Ignoring Some Of My Virtual Hosts?

Last night I learned an interesting lesson about Apache virtual host directives. On my development machine, I use named virtual hosts for the various projects I work on, a bit like this:

<VirtualHost *:80>
    DocumentRoot "/home/mark/Projects/Project1"
    ServerName project1
    ErrorLog "/var/log/apache2/project1_error_log"
    CustomLog "/var/log/apache2/project1_log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/home/mark/Projects/Project2"
    ServerName project2
    ErrorLog "/var/log/apache2/project2_error_log"
    CustomLog "/var/log/apache2/project2_log" common
</VirtualHost>

and this all works pretty nicely.

Last night one of our customers wanted to put live a site we've been building for them. This site will be reachable via several different hostnames - we redirect most back to a single canonical one, but the server still needs to recognise the different variations.

The virtual hosts on this server were set up by the customer's chosen hosting provider, and were very slightly different from my usual setup:

<VirtualHost livewebsite.com:80>
    DocumentRoot "/var/www/project"
    ServerName livewebsite.com
    ErrorLog "/var/log/apache2/project_error_log"
    CustomLog "/var/log/apache2/project_log" common
</VirtualHost>

<VirtualHost www.livewebsite.com:80>
    DocumentRoot "/var/www/project"
    ServerName www.livewebsite.com
    ErrorLog "/var/log/apache2/project_error_log"
    CustomLog "/var/log/apache2/project_log" common
</VirtualHost>

All looks ok. Before changing DNS, I updated my local /etc/hosts file so I could browse to the new server using the two hostnames, just to check everything worked ok.

Drop livewebsite.com into my browser, and our lovely new site loaded up nicely - everything is good.

Try switching to www.livewebsite.com, and this happens:

Ubuntu default page

By watching the Apache access log on the server, I can see that me request is going to the right server, but instead of serving up my virtual host's DocumentRoot, it's serving Apache's default site. That's not right!

Every time I've had trouble in the past with Apache configurations, and especially virtual hosts, I've pulled out the trusty `apachectl -S` command to see what Apache thinks my configuration means.

And this made some pretty interesting reading - this server is 1.2.3.4:

VirtualHost configuration:
1.2.3.4:80        is a NameVirtualHost
         default server myhost (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost livewebsite.com (/etc/apache2/sites-enabled/livewebsite.com.conf:1)
5.6.7.8:80        is a NameVirtualHost
         port 80 namevhost www.livewebsite.com (/etc/apache2/sites-enabled/www.livewebsite.com.conf:1)

Hang on - what's 5.6.7.8? Where did that come from? After grepping all the Apache config files came up blank, and scratching my head quite a lot didn't help, I tried creating another virtual host with a completely different hostname:

<VirtualHost www.completelymadeupdomainname.com:80>
    DocumentRoot "/var/www/project"
    ServerName www.livewebsite.com
    ErrorLog "/var/log/apache2/project_error_log"
    CustomLog "/var/log/apache2/project_log" common
</VirtualHost>

and now, Apache refused to start, saying there was a DNS lookup failure on www.completelymadeupdomainname.com. Ahah! So, Apache is doing DNS lookups on those virtual host names. As we hadn't put the new server live yet, I knew that public DNS was pointing both the required hostnames (livewebsite.com and www.livewebsite.com) to the old server, which was 5.6.7.8. However, livewebsite.com was in the new server's /etc/hosts file, which is why it was resolving to the new server, while www.livewebsite.com still resolved to the old server.

From there, it was a quick job to add www.livewebsite.com into the new server's /etc/hosts file, and all of a sudden, everything works.

So, today's lesson is that Apache will look up any hostnames in your VirtualHost directives. Be careful!