White-listing IP addresses for your Apache virtual hosts

I tried setting up some sites on public hosting company where I needed to set it up in a way where only specified IP addresses are allowed to access them. Below is what I did.

Basic config

Below is the basic configuration:
1
2
3
4
5
6
7
8
9
<Directory "/srv/httpd/htdocs/mydomain.com">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from 127.0.0.1
    Allow from xxx.xxx.xxx.xxx
    Allow from xxx.xxx.xxx.xxx
    Deny from all
</Directory>

Organized

Since I’m doing this IP blocking for many sites, it make sense to simplify the configuration so that I only need to edit a single list of IP that would apply to all sites. What I did is put the config on file and include it on each virtual host config. Below is the filename and the sample content.
File: /etc/httpd/block-world.conf
1
2
3
4
5
Order deny,allow
Allow from 127.0.0.1
Allow from xxx.xxx.xxx.xxx
Allow from xxx.xxx.xxx.xxx
Deny from all
Then for each virtual host, I include the file like this:
1
2
3
4
5
<Directory "/srv/httpd/htdocs/mydomain.com">
    Options Indexes FollowSymLinks
    AllowOverride All
    Include /etc/httpd/block-world.conf
</Directory>
That’s it. Share and enjoy.

No comments:

Post a Comment