DNS Geotargeting – Bind9
We recently had to setup some geo targeting with our domain name servers. We have different datacenters around the world and we wanted to keep our infrastructure as intact as possible, while still serving from nearest datacenter. If you don't use anycast or similar technology, DNS geo targeting might help you forward your clients to correct ip's. The main idea was to forward all asian countries to datacenter located in Singapore, while serving the rest of the world from our europe datacenter. In order to achieve that, I patched my bind with geoip patch found here.
Prerequisites
- Prerequisite for using this patch is owning MaxMind®'s database. We have our own GeoIP database which we have simply put into default directory located at: /usr/share/GeoIP
-
As aditional prerequistie you also need GeoIP API C library. It's included in Ubuntu/Debian repositories and installed with simple:
apt-get install libgeoip1
Installing your geoip patched bind
I have created packages for Debian/Ubuntu. They are provided AS IS without warranty and there won't be any upgrades. Fix them on your own, if you want newer version.
Link to GeoIP Bind9 binaries: bind9_9.8.0-P2-1geoip.tar.gz
Configuring patched bind
Correct syntax for configuring bind is: geoip_<DBTYPE>DB_<FIELD>_<VALUE>
Here are some examples on how to target countries and even cities. For detailed explanation of all features you can check the link to original website where patch came from.
view "LJUBLJANA" { match-clients { geoip_cityDB_city_Ljubljana; }; # This is a city in Slovenia zone "example.com" in { type master; file "lj.example.com.dns"; }; }; view "US" { match-clients { geoip_cityDB_country_US; }; zone "example.com" in { type master; file "us.example.com.dns"; }; }; view "DEFAULT" { zone "example.com" in { type master; file "example.com.dns"; }; };
Explanation
First view targets clients originating from capital city of Ljubljana, second targets everyone originating from United States, while default view forwards everyone else to default zone file. You are free to alter your records in specific zone file as you wish. For example you could include A record in us.example.com.dns zone file for cdn.mydomain.com that forwards you to 33.234.22.1, while default A record for this hostname is 193.2.1.122.
Final testing
Once you have things set up, you may test it from different locations. You can also verify which country your test IP originates from, on MaxMind® website located here.