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 mainhttpd.conf
before thevhosts.d
directory is included.I could just specify a
DocumentRoot
in my mainhttpd.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?
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:If it feels dirty to abuse alphabetization to set the default, maybe put your default name-based vhost in your main
httpd.conf
, before theInclude
line?This is actually the best way to do it, though. You want to be consistent with your config, so don't put one
<VirtualHost>
block in the httpd.conf and the rest in separate files. Name the config file for the default vhost!default.conf
, and then just make sure there are no other conf files invhosts.d
with names that start with punctuation.