Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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?

share|improve this answer
    
Ok that's great. That's what I wanted to get confirmed about _default_. I think I probably will hard-code a catch-all vhost in my main httpd.conf, before the Include as you suggest. –  benbradley Dec 16 '12 at 9:03
    
Adding a VirtualHost _default:443 in the main conf file before the Include does not seem to do he trick. One of the vhosts in the included files is being set as the default for the ip address. –  Rondo Jun 20 at 4:27
    
@Rondo You should ask a new question with the info for your specific installation (including the apachectl -S output). –  Shane Madden Jun 20 at 4:39

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

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 in vhosts.d with names that start with punctuation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.