Zum Inhalt

geoip2

The geoip2 directive is used to define a block for specifying GeoIP2 database files and associated variables for client IP lookups. It allows you to create variables with values from the MaxMind GeoIP2 databases based on the client IP address, supporting both IPv4 and IPv6.

Syntax

geoip2 <path_to_mmdb> { ... }

Context

  • http
  • stream

Example

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

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

    server {
        location / {
            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;
    }
}

In this example, the geoip2 directive is used to load GeoIP2 databases for both HTTP and stream contexts. It defines variables for country code and name, as well as city name, which can be used in server configurations. The auto_reload option is set to check for database updates every 5 minutes.