The GeoIP plugin

The geoip plugin adds new routing vars to your internal routing subsystem.GeoIP’s vars are prefixed with the “geoip” tag. To build the geoip plugin youneed the official GeoIP C library and its headers. The supported databases arethe country and city one, and they are completely loaded on memory at startup.

The country database give access to the following variables:

  • ${geoip[country_code]}
  • ${geoip[country_code3]}
  • ${geoip[country_name]}

while the city one offers a lot more at the cost of increased memory usage forstoring the database

  • ${geoip[continent]}
  • ${geoip[country_code]}
  • ${geoip[country_code3]}
  • ${geoip[country_name]}
  • ${geoip[region]}
  • ${geoip[region_name]}
  • ${geoip[city]}
  • ${geoip[postal_code]}
  • ${geoip[latitude]} (${geoip[lat]})
  • ${geoip[longitude]} (${geoip[lon]})
  • ${geoip[dma]}
  • ${geoip[area]}

Enabling geoip lookup

To enable the GeoIP lookup system you need to load at least one database. Afterhaving loaded the geoip plugin you will get 2 new options:

  • —geoip-country specifies a country database
  • —geoip-city specifies a city database

If you do not specify at least one of them, the system will always return empty strings.

An example

  1. [uwsgi]
  2. plugin = geoip
  3. http-socket = :9090
  4. ; load the geoip city database
  5. geoip-city = GeoLiteCity.dat
  6. module = werkzeug.testapp:test_app
  7. ; first some debug info (addvar will ad WSGI variables you will see in the werkzeug testapp)
  8. route-run = log:${geoip[country_name]}/${geoip[country_code3]}
  9. route-run = addvar:COUNTRY=${geoip[country_name]}
  10. route-run = log:${geoip[city]}/${geoip[region]}/${geoip[continent]}
  11. route-run = addvar:COORDS=${geoip[lon]}/${geoip[lat]}
  12. route-run = log:${geoip[region_name]}
  13. route-run = log:${geoip[dma]}/${geoip[area]}
  14.  
  15. ; then something more useful
  16. ; block access to all of the italians (hey i am italian do not start blasting me...)
  17. route-if = equal:${geoip[country_name]};Italy break:403 Italians cannot see this site :P
  18. ; try to serve a specific page translation
  19. route = ^/foo/bar/test.html static:/var/www/${geoip[country_code]}/test.html

Memory usage

The country database is tiny so you will generally have no problem in using it.Instead, the city database can be huge (from 20MB to more than 40MB). If youhave lot of instances using the GeoIP city database and you are on a recentLinux system, consider using Using Linux KSM in uWSGI to reduce memory usage. All of thememory used by the GeoIP database can be shared by all instances with it.