Apache - Block specific countries using mod_geoip
Here's a really simple set of commands to block certain countries from accessing content served via Apache. You can use this on your vps to only accept requests from the UK, for example.
Here's the top of a modified 000-default virtualhost:
<VirtualHost *:80>
DocumentRoot /var/www
<Directory /var/www/>
# Default Config
Options Indexes FollowSymLinks MultiViews
AllowOverride None
# Only allow specific countries
GeoIPEnable On
SetEnvIf GEOIP_COUNTRY_CODE GB AllowCountry
Deny from all
Allow from env=AllowCountry
</Directory>
... etc
Once you restart apache (sudo service apache2 restart
), any IP that isn't from within Great Britain (GB) will be served a 403 - it's great to see a load of these appear in your /var/log/apache2/access.log
files.
Written by James Mallison
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Linux
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#