0

I have 2 virtual hosts loaded from 2 separate conf files in /etc/httpd/conf.d/ when I point my browser to vhost1.test.com it loads the proper index.html as does vhost2.test.com. However the web server now responds with the 2nd vhost as the servers wildcard domain, if I make a dns entry for foo.test.com the page for vhost2 will load. If I remove the configuration for vhost2 the same behavior now happens for vhost1. What in my configuration is causing the last virtual server loaded to become a wildcard domain? With this configuration apache is also ignoring the default document root of /var/www/html when I point my browser to test.com and instead loading the wildcard. Without either vhost.conf loaded the documents in /var/www/html load as expected.

vhost1.conf

<VirtualHost *:80>
DocumentRoot /vhost1
ServerName vhost1.test.com
ErrorLog logs/vhost1-error_log
CustomLog logs/vhost1-access_log common
<Directory "/vhost1">
order deny,allow
Require all granted
Allow from localhost 127.0.0.1 192.168.
</Directory>

vhost2.conf

<VirtualHost *:80>
DocumentRoot /vhost2
ServerName vhost2.test.com
ErrorLog logs/vhost2-error_log
CustomLog logs/vhost2-access_log common
<Directory "/vhost2">
order deny,allow
Require all granted
Allow from localhost 127.0.0.1 192.168.
</Directory> 

0

From the apache documentation on How the server selects the proper name-based virtual host

The default name-based vhost for an IP and port combination
If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.

So when you point your DNS to your non-existent "foo.test.com" your HTTP requests are containing a header entry host: foo.test.com, apache cant match this to one of your VHOSTS, so its always serves the first file it finds containing a VHOST container.
Because files are listed alphabetically, the first file will be the one with the lowest alphabetical listing.
So in fact if your VHOSTs - or more importantly their filenames were:

  • vhost1
  • vhost2

vhost1 would always be served if apache was unable to match the request to a particular VHOST by IP address or hostname.

Yes this is quite alarming when you first discover it, that is why some packages of apache will define a default virtual host with a filename of 000_default_vhost.
This way you can be sure this VHOST will always be served if a request doesn't match your VHOSTs by hostname or IP.
You should consider configuring your server this way where the 000_default_vhost will always reject requests with a 404 as they are obviously not intended for your "proper" domains.
If this seems irrational behaviour for apache, note that nginx behaves the same way.
if you were using nginx and wanted to prevent processing requests with undefined server names

You can also define a server that just drops the requests:

server {
    listen      80;
    server_name "";
    return      444;
}

and put it into a file which appears first alphabetically, i.e. /etc/nginx/sites-available/000_catchall

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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