I have installed apache 2 and created and enabled a virtual host.
The virtual host has: ServerName example.com ServerAlias *.example.com ServerAlias foo.com ServerAlias *.foo.com ServerAlias bar.com ServerAlias *.bar.com
If I direct the browser to www.example.com, test.example.com, foo.com, www.foo.com, etc, ANYTHING except the bare server name, it works as expected, showing the content of /srv/www/example.com/public_html/index.php which is the DocumentRoot for the virtual host.
However, if I write only "example.com" in the browser, i see the contents of /var/www/index.html
It's NOT cached in the browser, I have already cleared the cache and also tried another browser and tried through a proxy.
This makes no sense to me. Any idea?
Here are the contents of /etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And here are the contents of /etc/apache2/sites-enabled/example.com:
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
ServerAlias foo.com
ServerAlias *.foo.com
ServerAlias bar.com
ServerAlias *.bar.com
#... and a few others
DocumentRoot /srv/www/example.com/public_html
ErrorLog /srv/www/example.com/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel notice
CustomLog /srv/www/example.com/access.log combined
<Directory /srv/www/example.com/public_html>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
I can't understand why, with the bare servername, the default vhost prevails over the one that specifically matches the server name, while all aliases work fine.
And here's the output of # apache2ctl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443 is a NameVirtualHost
default server example.com (/etc/apache2/sites-enabled/default-ssl:2)
port 443 namevhost example.com (/etc/apache2/sites-enabled/default-ssl:2)
*:80 is a NameVirtualHost
default server example.com (/etc/apache2/sites-enabled/000-default:1)
port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default:1)
port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com:1)
Syntax OK
(I guess it's needless to say that I've replaced the actual domain names with "example.com", "foo.com" and "bar.com" just for posting here at SF)
What other files are linked in /etc/apache2/sites-enabled/?
Often it is useful for diagnosing purposes to enable mod-info, like so (from http://httpd.apache.org/docs/2.2/mod/mod_info.html):
Generally I secure this with a certificate, but that goes beyond this explanation. Once you have that enabled, and restricted to 127.0.0.1 (tunnel with ssh to the server, set your hosts file etc). You can see the exact running configuration as parsed by Apache. You'll probably see that example.com is referenced elsewhere. Often things make more sense when you look using mod-info.
Apparently, documentation isn't clear about this.
From https://issues.apache.org/bugzilla/show_bug.cgi?id=57384
"If you omit the ServerName [in the virtual host definition], it's calculated from the system hostname (double-reverse DNS lookup of the system hostname)". Hence, if the enabled site 000-default has no server name, it is exactly as if it had example.com as the ServerName, being example.com my server's host name.
Hence in my case, when the client requests the domain example.com, 000-default is the first server that matches the requested hostname, and "catches" the request.
If I want (like I do) to have the default site catch all the request that don't match any server name, I need to specify a dummy ServerName in the default VirtualHost definition. This way, requests to the server's hostname won't match the default virtual host but they will match the example.com virtual host. And requests that don't match any servername or alias will be caught by the default site because it's the first one.
"The first-listed vhost captures anything that doesn't match a servername/serveralias, even if you specify a servername."