Using NetScaler and MaxMind API to insert GeoIP headers to backend

There are some really great blog posts out there regarding GeoIP and NetScaler (Neil Spellings for example). In this case, I need to insert City, Country and Continent into headers to the backend.

My first thought was to use the internal GeoIP (location) database in NetScaler, but it isn’t supported yet to print information from it (send me an email if you want the RFE ID). The next step was to start working with the latest MaxMind API which supports all three data sets. Unfortunately, there’s a bug in the XPATH_JSON parser in NetScaler that can’t handle 3-byte UTF-8 characters (Japanese characters for example). This is something being fixed and will be released later (same here, send me an email if you want the BUG ID). Since NetScaler is the best product out there, and the NetScaler team is the greatest ever, they made a policy exentsion to fix the issue until it’s fixed in the code.

Until the bug is fixed, the following policy extension is needed:

Now, let’s take a look at how we can utilize MaxMinds API to create these headers.

First off, create a load balancing vserver for MaxMind:

Next step, create the callout (and use the policy extension until the bug is fixed):

Please note: Change the authorization header to your own.

This will now cache the following (if IC is licensed and enabled): ”City;Country;Continent”

Now, let’s create the rewrites and add them to a vserver:

See below for how it’s cached:

I just tried it out with NGINX as backend with the following configuration:

And we get a nice and clean response:

Feel free to leave a comment if you have issues with anything or if you know better ways of doing it!

Disclaimer: All information on this blog is offered "as is" with no warranty. It is strongly recommended that you verify all information and validate all scripts in isolated test environments before using them in production environments.