You are viewing our old blog site. For latest posts, please visit us at the new space. Follow our publication there to stay updated with tech articles, tutorials, events & more.

Exploring mod_geoip

0.00 avg. rating (0% score) - 0 votes
Problem Statement
We have been inherently using IP Lookups for various purposes :
  • Redirecting a user based on country.
  • Advertisements and banners specific to the country/city of access.
  • Default landing page based on user location.
  • Country specific contact details.
Fetching country from IP seems a trivial task. However, to fetch a country name against a given IP we usually query the database (either spatial or range queries), which results in a large number of sql queries everyday, thus increasing the database load.
mod_geoip is an Apache extension, which embeds GeoIP database lookups into the Apache web server. It uses MaxMind GeoIP / GeoCity databases.
It exploits the libGeoIP library to look up geolocation information for a client as part of the http request process. When enabled, the module looks at the incoming IP address and sets some variables which provide geolocation information for that IP. The variables it sets depends on the specific GeoIP database being used (Country, City, ISP, etc.). These variables can be set in the environment.
After configuring mod_geoip, the locations can be fetched by using the following environment variables :
  • $_SERVER[‘GEOIP_COUNTRY_CODE’] for short name (Exampe- IN)
  • $_SERVER[‘GEOIP_COUNTRY_NAME’] for full name (Example- INDIA)
Some other parameters provided by mod_geoip , which can be exploited are :
  • $_SERVER[‘GEOIP_CONTINENT_CODE‘] – which gives a two-character code for the continent associated with the IP address.
  • $_SERVER[‘GEOIP_LATITUDE‘] – The latitude associated with the IP address.
  • $_SERVER[‘GEOIP_LONGITUDE‘] – The longitude associated with the IP address.
Memory Footprint :
Memory usage is about the same as the database file size (approx 780KB), no matter how many child processes Apache spawns.
For more insight, check
The usage of mod_geoip decreased the dependency on mysql and led to a significant drop in the number of queries on our server which was serving the IP to Location mapping. A drop of 46% was observed in the total select queries , after shifting from DB to mod_geoip extension for location fetching.
Posted in General