FAQ

Page Discussion Edit History

NginxHttpGeoIPModule

Contents

Edit section: Synopsis Synopsis

This module creates ngx_http_geoip_module variables based on the IP-address of the client matched against the MaxMind GeoIP binary files. This module appeared in nginx version 0.7.63 and 0.8.6.

Precondition

This module needs the geo databases and the library to read the database.

#Get the free database of geo_city
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
#Get the free database of geo_coundty
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
#Get the libgeoip. In debian you can do like this:
sudo apt-get install libgeoip-dev
#In other systems, you can download the source and compile it youself.
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz

On CentOS libgeoip is installed with yum:

yum install geoip-devel

Compile

./configure --with-http_geoip_module

Example

http {
    geoip_country  GeoIP.dat;
    geoip_city     GeoLiteCity.dat;
    ...

Edit section: Directives Directives

Edit section: geoip_country geoip_country

syntax: geoip_country path/to/db.dat;

default: none

context: http

The directive indicates the path to the .dat file used for determining the visitor's country from the IP-address of the client. When set the module makes available the following variables:

  • $geoip_country_code; - two-letter country code, for example, "RU", "US".
  • $geoip_country_code3; - three-letter country code, for example, "RUS", "USA".
  • $geoip_country_name; - the name of the country, for example, "Russian Federation", "United States".

If you only need the country's name, you can just set the geoip_country database(1.1M), while the geoip_city database is much bigger(43M) and all the databses will be cached in memory.

Edit section: geoip_city geoip_city

syntax: geoip_city path/to/db.dat;

default: none

context: http

The directive indicates the path to the .dat file used for determining countries, regions and cities from IP-address of the client. When set the module makes available the following variables:

  • $geoip_city_country_code - two-letter country code, for example, "RU", "US".
  • $geoip_city_country_code3 - three-letter country code, for example, "RUS", "USA".
  • $geoip_city_country_name - the name of the country, for example, "Russian Federation", "United States" (if available).
  • $geoip_region - the name of region (province, region, state, province, federal land, and the like), for example, "Moscow City", "DC" (if available).
  • $geoip_city - the name of the city, for example, "Moscow", "Washington" (if available).
  • $geoip_postal_code - postal code (if available).
  • $geoip_city_continent_code (if available)
  • $geoip_latitude - latitude (if available)
  • $geoip_longitude - longitude (if available)

Edit section: References References

Original Documentation