Nginx GeoIP2 module
Go to file
Lee 445df24ef3
Merge pull request #118 from MohamedLamineAllal/patch-1
doc: fix dynamic module compilation commands in
2024-04-02 16:25:17 -05:00
LICENSE Initial commit 2014-01-30 23:00:34 +00:00 doc: fix dynamic module compilation commands in 2023-10-01 01:55:03 +01:00
config Added stream support 2016-10-27 18:09:39 +03:00
ngx_http_geoip2_module.c Fix #90: segfault on bad auto reload value 2023-06-14 10:17:41 -05:00
ngx_stream_geoip2_module.c Fix segfault when using auto_reload with multiple databases. 2019-10-25 11:23:21 -07:00


ngx_http_geoip2_module - creates variables with values from the maxmind geoip2 databases based on the client IP (default) or from a specific variable (supports both IPv4 and IPv6)

The module now supports nginx streams and can be used in the same way the http module can be used.


First install libmaxminddb as described in its file.

Download nginx source

tar zxvf nginx-VERSION.tar.gz
cd nginx-VERSION
To build as a dynamic module (nginx 1.9.11+):
./configure --with-compat --add-dynamic-module=/path/to/ngx_http_geoip2_module
make modules

This will produce objs/ It can be copied to your nginx module path manually if you wish.

Add the following line to your nginx.conf:

load_module modules/;
To build as a static module:
./configure --add-module=/path/to/ngx_http_geoip2_module
make install
If you need stream support, make sure to compile with stream:
./configure --add-dynamic-module=/path/to/ngx_http_geoip2_module --with-stream
./configure --add-module=/path/to/ngx_http_geoip2_module --with-stream

Download Maxmind GeoLite2 Database (optional)

The free GeoLite2 databases are available from Maxminds website (requires signing up)

Example Usage:

http {
    geoip2 /etc/maxmind-country.mmdb {
        auto_reload 5m;
        $geoip2_metadata_country_build metadata build_epoch;
        $geoip2_data_country_code default=US source=$variable_with_ip country iso_code;
        $geoip2_data_country_name country names en;

    geoip2 /etc/maxmind-city.mmdb {
        $geoip2_data_city_name default=London city names en;

    fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
    fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
    fastcgi_param CITY_NAME    $geoip2_data_city_name;

stream {
    geoip2 /etc/maxmind-country.mmdb {
        $geoip2_data_country_code default=US source=$remote_addr country iso_code;

Retrieve metadata regarding the geoip database.

$variable_name metadata <field>

Available fields:

  • build_epoch: the build timestamp of the maxmind database.
  • last_check: the last time the database was checked for changes (when using auto_reload)
  • last_change: the last time the database was reloaded (when using auto_reload)
Autoreload (default: disabled):

Enabling auto reload will have nginx check the modification time of the database at the specified interval and reload it if it has changed.

auto_reload <interval>
$variable_name [default=<value] [source=$variable_with_ip] path ...

If default is not specified, the variable will be empty if not found.

If source is not specified, $remote_addr will be used to perform the lookup.

To find the path of the data you want (eg: country names en), use the mmdblookup tool:

$ mmdblookup --file /usr/share/GeoIP/GeoIP2-Country.mmdb --ip

          6252001 <uint32>
          "US" <utf8_string>
              "USA" <utf8_string>
              "United States" <utf8_string>

$ mmdblookup --file /usr/share/GeoIP/GeoIP2-Country.mmdb --ip country names en

  "United States" <utf8_string>

This translates to:

$country_name "default=United States" source=$remote_addr country names en
Additional Commands:

These commands works the same as the original ngx_http_geoip_module documented here:

However, if you provide the source=$variable_with_ip option on a variable, these settings will be ignored for that particular variable.

geoip2_proxy < cidr >

Defines trusted addresses. When a request comes from a trusted address, an address from the "X-Forwarded-For" request header field will be used instead.

geoip2_proxy_recursive < on | off >

If recursive search is disabled then instead of the original client address that matches one of the trusted addresses, the last address sent in "X-Forwarded-For" will be used. If recursive search is enabled then instead of the original client address that matches one of the trusted addresses, the last non-trusted address sent in "X-Forwarded-For" will be used.