Centos – Apache2 default vhost in alphabetical order or override with _default_ vhost

apache-2.2centoshttpd.confvirtualhost

I've got multiple named vhosts on an Apache web server (CentOS 5, Apache 2.2.3).
Each vhost has their own config file in /etc/httpd/vhosts.d and these vhost config files are included from the main httpd conf with… Include vhosts.d/*.conf

Here's an example of one of the vhost confs…

NameVirtualHost *:80
<VirtualHost *:80>
        ServerName www.domain.biz
        ServerAlias domain.biz www.domain.biz
        DocumentRoot /var/www/www.domain.biz
        <Directory /var/www/www.domain.biz>
                Options +FollowSymLinks
                Order Allow,Deny
                Allow from all
        </Directory>
        CustomLog /var/log/httpd/www.domain.biz_access.log combined
        ErrorLog /var/log/httpd/www.domain.biz_error.log
</VirtualHost>

Now I when anyone tries to access the server directly by using the public IP address, they get the first vhost specified in the aggregated config (so in my case it's alphabetical order from the vhosts.d directory). Anyone accessing the server directly by IP address, I'd like them to just get an 403 or a 404.

I've discovered several ways to set a default/catch-all vhost and some conflicting opinions.

  • I could create a new vhost conf in vhosts.d called 000aaadefault.conf
    or something but that feels a bit nasty.

  • I could have a <VirtualHost> block in my main httpd.conf before the vhosts.d directory is included.

  • I could just specify a DocumentRoot in my main httpd.conf

What about specifying a default vhost in httpd.conf with _default_ http://httpd.apache.org/docs/2.2/vhosts/examples.html#default
Would having a <VirtualHost _default_:*> block in my httpd.conf before I Include vhosts.d/*.conf be the best way for a catch-all?

Best Answer

Setting a <VirtualHost _default_:*> will do nothing whatsoever, it does not take precedence over virtual hosts configured as <VirtualHost *:80> (except on ports other than 80).

_default_ should only be used with IP-based virtual hosting. From the documentation:

A default vhost never serves a request that was sent to an address/port that is used for name-based vhosts.

If it feels dirty to abuse alphabetization to set the default, maybe put your default name-based vhost in your main httpd.conf, before the Include line?