DNS Geotargeting – Bind9

Geo targetingWe 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.

Leave a Reply

help-hint.png
Purpose of the commenting system is to share your experience. I encourage you to post feedback with your own suggestions, ideas or optimizations regarding the topic of a blog post. What commenting system isn't for, is asking questions about similar issues of yours and requesting support for it. Blog post is provided as is and I am not here to solve all your problems. Please bear that in mind and try to avoid posting such comments. I do take privilege to remove comment from my blog for any reason whatsoever. Usually I do it when I sense a comment was posted only for spam/seo reasons or is out of blog post's topic. Thank you for reading this, now you may continue :)
 

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