Nginx, limit website visitor bandwidth by country

First grab this perl script which you will use to convert Maxmind’s geo IP database into a format usable by Nginx.

make it executable

$ chmod 755 geo2nginx.pl

Then download Maxmind’s latest GeoLite country database in CSV format.

Unzip it, and run perl script

$ unzip GeoIPCountryCSV.zip
$ ./geo2nginx.pl < GeoIPCountryWhois.csv > nginxGeo.txt

Copy nginxGeo.txt into your nginx config directory.

Then add the following text in the ‘http’ section of your nginx.conf file:

geo $country {
	default no;
	include nginxGeo.txt;
}

Then add the following in the ’server’ section of your nginx.conf file:

if ($country ~ ^(?:US|CA|ES)$ ){
	set $limit_rate 10k;
}
if ($country ~ ^(?:BR|ZA)$ ){
	set $limit_rate 20k;
}

This limits anyone from the USA, Canada and Spain to a maximum of 10 kilobits per second of bandwidth. It gives anyone from Brazil and South Africa 20 Kbps of bandwidth. Every other country gets the maximum.

source: http://markmaunder.com/2010/how-to-limit-website-visitor-bandwidth-by-country/

2 Comments

  1. Wow, akhirnya ketemu juga cara jalanin GeoIP di nginx setelah 5 bulan cari2.
    Makasih banget bro

Leave a Reply

Your email address will not be published. Required fields are marked *