Wednesday, February 16, 2011

how to stop default website from getting served in Apache2,Why is there a 000-default file

I run 14 + websites which are divided into a cluster sort of situation i.e. a group of websites on one node another group on another node like this.The requirement of my work keeps increasing in such a situation I found one important thing after discussing on Apache users group and Ubuntu forum.This is applicable for people using Ubuntu or Debian sort of system.
When you use name based virtual hosting to host different websites some times you must have encountered a situation where what ever you are typing in URL is going to some virtual host which is always serving your request even though the original request was not meant for this URL to be served.

What happens is in Ubuntu/Debian based system you define your vhosts in /etc/apache2/sites-available directory.

Some thing such as

abchost1.conf
abchost2.conf
abchost3.conf
abchost4.conf

When apache reads these vhost configurations it does so in a alphabetical order.
That is the reason when you install Apache on Ubuntu you see a file with name
000-default always so as to give priority to this file in case of any request going to some
wrong path.This vhost is one which will reply.
In case 000-default is missing then what ever alphabetically comes next.
That is half the reason your URLs mismatch and when you expected some thing, some thing else came.
As a proof read this link
http://httpd.apache.org/docs/2.2/mod/core.html#include
which says following line
"wildcard characters can be used to include several files at once, in alphabetical order. "

and if you ever noticed in /etc/apache2/apache2.conf
you will notice the last line
Include /etc/apache2/sites-enabled/

which includes all the vhosts definitions in sites-enabled directory and as per above link of Include directive of apache doc it reads those vhosts in alphabetical order.

What this means is suppose you have 3 Name Based Virtual Hosts on same machine

lets say
abc1.com
abc2.com
abc3.com

with configurations like these
/etc/apache2/sites-enabled/abc1.conf

<VirtualHost *:80>
        ServerAdmin webmaster@abc1.com

         ServerName abc1.com
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>
Second /etc/apache2/sites-enabled/abc2.conf
<VirtualHost *:80>
        ServerAdmin webmaster@abc1.com

         ServerName abc2.com
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

Third /etc/apache2/sites-enabled/abc3.conf
<VirtualHost *:80>
        ServerAdmin webmaster@abc1.com

         ServerName abc3.com
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

and now a requests goes to this server as
http://abcdef.com

Then apache will try to match the ServerName abc1.com it will not find abcdef there
then
ServerName abc2.com here also it will not find abcdef there
then
ServerName abc3.com here also
it will not find abcdef there
then
the request will be served by the alphabetically 1st vhost i.e. abc1.conf which has ServerName abc1.com.Note when I say alphabetically I mean the file name of vhost is important.


To get rid of such situation define one more Vhost 000-abc.conf

where I added 000 because it alphabetically will get first priorirty and there we can define what Apache needs to do in such situations where request does not have a valid HTTP_HOST
or in case of other errors also this is a very standard practice.
Read here
1) http://www.mail-archive.com/users@httpd.apache.org/msg43225.html
2) http://httpd.apache.org/docs/2.2/vhosts/details.html (this link might not be that easy to understand)
3) http://www.gossamer-threads.com/lists/apache/users/386570?do=post_view_threaded
4) http://www.mail-archive.com/users@httpd.apache.org/msg43167.html

No comments: