Centos – Setup default page for Apache Virtual Hosts

apache-2.2centoscentos6virtualhost

We are running Apache on a server with multiple VirtualHost entries. Whenever someone access the server by it's IP address (http://198.147.xx.xx), one of the websites that is a VirtualHost comes up. This is not desired. We would rather a blank page come up if the IP is accessed directly. I'm not sure if the chosen site is just the first one that comes in the list of Virtual Host entries or what.

We want to setup a default, blank page if someone access the server by IP. I thought that simply setting up another VirtualHost entry for the IP address would do it, and it seemed to, but now accessing any of the other VirtualHost sites leads to the same blank, default page. Here is an example of our config:

<VirtualHost *:80>
    DocumentRoot /vhosts/somesite.com/public
    ServerName somesite.com
    <Directory "/vhosts/somesite.com/public">
        allow from all
        Options +Indexes
        AllowOverride All
    </Directory>
    ServerAlias www.somesite.com
</VirtualHost>

<VirtualHost 198.147.XX.XX:80>
    DocumentRoot /vhosts/default/public
    <Directory /vhosts/default/public>
        allow from all
        Options +Indexes
    </Directory>
</VirtualHost>

What are we doing wrong here? Is there a better way to setup a default landing page for anyone accessing the server via IP address?

Best Answer

If I remember right, the behavior for virtual hosts works like so:

  • httpd searches for the vhost with the most specific qualifiers. (either IP address, or the host header if NameVirtualHost has been declared)
  • In the absence of a specific entry, the first virtual host to be defined gets used. This is called the default virtual host and what normally gets served when a request is made without a Host header. (i.e. by IP address)

The problem you're running into now is that your IP based virtual host is more specific than your catch-all, default virtual host (at least when the Host header doesn't match the ServerName or ServerAlias). To get the behavior you're looking for, I think you'd need to do this:

  • Create a default virtualhost of *:80 with a blank page.
  • Adjust your somesite.com virtualhost so that its ServerAlias contains every name that you don't want hitting the default virtual host.

Hopefully someone has a solution that comes closer to achieving your desired end result; it's been awhile and I'd be interested to learn from it.

Related Topic