ngx_http_geoip_module

Client country, city and organization from legacy GeoIP databases

dynamic moduleBSD-2-Clausesource docs

Overview

ngx_http_geoip_module creates variables whose values depend on the client IP address, looked up in the precompiled MaxMind databases of the legacy GeoIP format. A country database yields $geoip_country_code, $geoip_country_code3 and $geoip_country_name; a city database adds region, city, postal code, latitude and longitude variables; an organization database yields $geoip_org.

The module requires the MaxMind GeoIP C library. With IPv6-capable databases (1.3.12, 1.2.7) IPv4 addresses are looked up as IPv4-mapped IPv6 addresses, and geoip_proxy lets requests from trusted addresses use the X-Forwarded-For header instead. MaxMind has discontinued the legacy GeoIP and GeoLite databases, so new deployments should use the GeoIP2 module at /modules/geoip2/ with .mmdb files instead.

It is an official nginx module that upstream does not build by default, so it needs the --with-http_geoip_module configure parameter. The n.wtf packages build it as a dynamic module shipped in libnginx-mod-http-geoip, mainly for existing configurations that still map $geoip_country_code to access rules or log fields.

Key directives

geoip_countryCountry database; provides $geoip_country_code, $geoip_country_code3 and $geoip_country_name. http context.
geoip_cityCity database; adds $geoip_city, $geoip_region, $geoip_postal_code, $geoip_latitude and more.
geoip_orgOrganization database (1.0.3); exposes $geoip_org with the organization name.
geoip_proxyTrusted address or CIDR; for requests from it the address in X-Forwarded-For is used instead.
geoip_proxy_recursiveUse the last non-trusted X-Forwarded-For address rather than the last one, default off.

Example

nginx.conf
# legacy .dat databases; see /modules/geoip2/ for the current format geoip_country /usr/share/GeoIP/GeoIP.dat; geoip_city /usr/share/GeoIP/GeoLiteCity.dat; geoip_proxy 192.168.100.0/24; geoip_proxy_recursive on; map $geoip_country_code $blocked { default 0; XX 1; } server { listen 80; if ($blocked) { return 403; } add_header X-Country $geoip_country_code; }

Availability

Ships as a dynamic module in libnginx-mod-http-geoip, installed with nginx-extras. The package drops a load_module config into /etc/nginx/modules-enabled/, so it loads on start.