controlling the default site in apache configs

March 14, 2010

in linux,web

I configure apache about once every 3 months, so every time I get into the files I feel like I’m learning it all over again. Apache has a ton of config options, and a ton of documentation, as well as all kinds of random sites with “help” on setting it up. This means that when you want to do something simple that isn’t quite shown in an example, it can take hours to figure out. I just ran into one of those situations, so I thought I’d post my desired setup and the resulting config (as a reminder to me, and maybe a help to others?).

In this setup, there is a single public IP which apache will be using, and multiple web sites which should be served from this IP, differing by hostname. Let’s use mattvsworld.com as the example here, with the websites “www.mattvsworld.com”, and “www2.mattvsworld.com”. What I wanted apache to do was this:

  1. If a request was for “www.mattvsworld.com”, serve www.mattvsworld.com
  2. If a request was for “mattvsworld.com”, serve www.mattvsworld.com
  3. If a request was for “www2.mattvsworld.com”, serve www2.mattvsworld.com
  4. For all other requests, serve a default site/page, and don’t serve either www.mattvsworld.com OR www2.mattvsworld.com

In other words for the last one, if I went to www3.mattvsworld.com, *do not* show me www or www2. Sounds good, right?

This is for the default ubuntu install of apache, so I went in and set up a couple of sites in /site-available, but for some reason I kept on getting either “www” or “www2″ served up when I went to “www3″… Turns out the *sequence* of your VirtualHost entries in your configs is important. Not only because apache matches them in order, but apache also uses the first VirtualHost entry as the default site to serve when no other entry matches the request. Since I was using site configs in separate files, it turned out that the filename was important to ensure they loaded correctly, and that I needed my default to be loaded last… so here’s some sample snippets of how it turned out (the folder hierarchy is based on the debian config layout for apache, everything with root /etc/apache2/):

/sites-available/010_www.mattvsworld.com:

<VirtualHost *:80>
        ServerName www.mattvsworld.com
        ServerAlias mattvsworld.com
 
        DocumentRoot /var/www/www.mattvsworld.com
</VirtualHost>

/sites-available/020_www2.mattvsworld.com

<VirtualHost *:80>
        ServerName www2.mattvsworld.com
 
        DocumentRoot /var/www/www2.mattvsworld.com
</VirtualHost>

/sites-available/999_default:

<VirtualHost *:80>
        ServerAlias *
        DocumentRoot /var/www/default
</VirtualHost>

Of course, symlink or a2ensite these files, and then apache will follow the serving path I wanted. The important thing here is that the 999_default now acts as a sort of catch-all, so that even though 010_www.mattvsworld.com is loaded first, it shouldn’t be shown as the default due to a request coming in not matching one of the VirtualHost directives.

Cheers!

Leave a Comment

Previous post:

Next post: