geoip2
The `geoip2` directive enables the use of GeoIP2 databases to create variables based on client IP address geolocation.
Description
The geoip2 directive in NGINX's GeoIP2 module allows users to configure how geolocation data from MaxMind's GeoIP2 databases is accessed and utilized. It supports both IPv4 and IPv6, enabling the definition of variables that correspond to specific geographic data such as country code or city name based on the client's IP address. Additionally, the directive allows for auto-reloading of the database at specified intervals, which ensures that changes to the underlying data are reflected without needing to restart NGINX.
Parameters within the geoip2 block can specify the path to the GeoIP2 database file, options for metadata retrieval, and variable definitions for geographic data. The $geoip2_metadata_country_build and similar variables provide access to metadata such as the build time of the database, while data retrieval variables can include defaults for when no data is available or a source variable that designates an alternative IP for lookup. This flexibility enables the directive to adapt to various configuration requirements and allows for efficient geolocation handling in HTTP and stream contexts.
Errors can occur if the database file path is incorrect or if the required MaxMind libraries are not installed. It's crucial to validate the configuration and ensure the database is properly formatted to avoid runtime issues.
Config Example
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;
}
}Make sure the MaxMind database file path is correct to avoid errors.
If the database is not in the correct format, variable retrieval will fail.
Auto-reloading is disabled by default; set it explicitly if needed.