Apache Virtual Host

 

Control Default Apache Virtual Host

 

1. Introduction

When running multiple websites on a server as virtual hosts, there might occasionally be an issue somewhere, like a typo in a host configuration. The result might be that the first virtual host's content would be displayed instead of the desired content. Since the website owner gets rather upset, it is not desirable. It'd be better to see a generic page.

 

2. Problem

When looking for the appropriate virtual host to use for a specific request, Apache goes through them in the 'nix sort order they are defined. If it gets to the end, without a match, it uses the first virtual host.

 

3. Solution

Add a generic virtual host that is used instead of the current first one.

 

4. Virtual Host using httpd.conf

Adding the virtual host container to the end of the /etc/httpd/conf/httpd.conf file is the most obvious method for adding virtual hosts.

If inserting as the first host, put the container after the NameVirtualHost:80 line and before any other virtual host containers.

If adding a catch all virtual host at the end, make sure there are no containers after it because they won't be available.

 

5. Virtual Host Files

A second method of adding virtual host containers to the Apache configuration is by using virtual host files. This is described in more detail in ApacheVhostDir.

If inserting as first host, name the file something that shows first in a directory listing, like 0Default.conf.

If adding a catch all virtual host, name the file something that will always be listed last, like zDefault.conf.

 

6. First Virtual Host

As a first virtual host, the ServerName should be something specific that is not actually used. It can be as simple as:

<VirtualHost *:80>
  ServerName fail
</VirtualHost>

 

7. Last Virtual Host

As a catch all virtual host, it should match everything. This can done with:

<VirtualHost *:80>
  ServerAlias *
</VirtualHost>

 

8. Restart Apache

To make your changes take effect, restart Apache.

service httpd graceful

 


 

Tags: