Why might Apache ignore a virtual host with a ServerName matching the requested URL?

Solution 1:

This may be obvious, but don't forget to restart the apache service after enabling additional virtual host.

After executing a2ensite for the second virtual host, the output of apache2ctl -S reflects that both sites are configured (and one of them is the default), but it won't be live until you reload apache.

Let's say you have two virtual hosts - site1 and site2. You run a2ensite site1 and then reload apache service. Now you can access http://site1 and it is the default. Now you run a2ensite site2, but forget to restart apache. The output of apache2ctl -S will be:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server site1 (/etc/apache2/sites-enabled/site1:1)
         port 80 namevhost site1 (/etc/apache2/sites-enabled/site1:1)
         port 80 namevhost site2 (/etc/apache2/sites-enabled/site2:1)
Syntax OK

But when you try to load http://site2, it will actually load the default site (site1), since the configuration isn't loaded.

Solution 2:

I had a similar problem where my additional vhosts on port 443 (SSL/HTTPS) were all being directed to the directory of the first vhost listed. Apache was essentially ignoring the servername property and matching on the ip:port only.

Turns out that I was missing the command 'NameVirtualHost *:443' to enable Named virtual hosting for port 443.

'NameVirtualHost *:443' just needs to be called once, and must be defined above your vhosts for port 443. I put my definition in the ports.config file so it looks like:

NameVirtualHost *:80
Listen 80

<IfModule mod_ssl.c>
    NameVirtualHost *:443
    Listen 443

<IfModule mod_gnutls.c>
    NameVirtualHost *:443
    Listen 443

Don't forget to restart apache after any changes.

Solution 3:

My 2 cents: as I have to stick with an IP (I don't want the site to be served on all networks installed), it happened that after the local private IP of the server changed, I forgot to change it here:

NameVirtualHost <VirtualHost>

Of course it's not an Apache problem to let you know that IP does not exist locally.

Solution 4:

Tom, please look here http://httpd.apache.org/docs/2.0/en/mod/core.html#namevirtualhost


Note, that the "main server" and any default servers will never be served for a request to a NameVirtualHost IP address (unless for some reason you specify NameVirtualHost but then don't define any VirtualHosts for that address).

So it should be okay if you change the default to the ip-adress of your server.