2014-01-31 00:00:34 +01:00
Description
===========
2016-11-04 00:48:10 +01: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.
2014-03-11 11:19:43 +01:00
2014-03-13 13:39:00 +01:00
## Installing
First install [libmaxminddb ](https://github.com/maxmind/libmaxminddb ) as described in its [README.md
file](https://github.com/maxmind/libmaxminddb/blob/master/README.md#installing-from-a-tarball).
2016-02-10 21:43:08 +01:00
#### Download nginx source
```
2016-05-11 13:59:16 +02:00
wget http://nginx.org/download/nginx-VERSION.tar.gz
2016-02-10 21:43:08 +01:00
tar zxvf nginx-VERSION.tar.gz
cd nginx-VERSION
```
##### To build as a dynamic module (nginx 1.9.11+):
```
./configure --add-dynamic-module=/path/to/ngx_http_geoip2_module
make
make install
```
This will produce ```objs/ngx_http_geoip2_module.so```. It can be copied to your nginx module path manually if you wish.
Add the following line to your nginx.conf:
```
load_module modules/ngx_http_geoip2_module.so;
```
##### To build as a static module:
2014-03-13 13:39:00 +01:00
```
./configure --add-module=/path/to/ngx_http_geoip2_module
2018-06-26 02:14:54 +02:00
make
2014-03-13 13:39:00 +01:00
make install
```
2014-03-13 13:46:27 +01:00
## Download Maxmind GeoLite2 Database (optional)
2020-01-21 19:15:54 +01:00
The free GeoLite2 databases are available from [Maxminds website ](http://dev.maxmind.com/geoip/geoip2/geolite2/ ) (requires signing up)
2014-03-13 13:46:27 +01:00
2014-03-11 11:19:43 +01:00
## Example Usage:
```
http {
...
2014-04-26 12:26:37 +02:00
geoip2 /etc/maxmind-country.mmdb {
2018-06-29 22:20:55 +02:00
auto_reload 5m;
2018-06-26 02:14:54 +02:00
$geoip2_metadata_country_build metadata build_epoch;
2016-06-17 19:52:58 +02:00
$geoip2_data_country_code default=US source=$variable_with_ip country iso_code;
2014-04-26 12:26:37 +02:00
$geoip2_data_country_name country names en;
}
geoip2 /etc/maxmind-city.mmdb {
$geoip2_data_city_name default=London city names en;
}
2014-03-11 11:19:43 +01:00
....
2016-02-10 21:43:08 +01:00
fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
fastcgi_param CITY_NAME $geoip2_data_city_name;
....
2014-03-11 11:19:43 +01:00
}
2016-11-04 00:48:10 +01:00
stream {
...
geoip2 /etc/maxmind-country.mmdb {
$geoip2_data_country_code default=US source=$remote_addr country iso_code;
}
...
}
2014-03-11 11:19:43 +01:00
```
2016-02-10 21:43:08 +01:00
2018-06-26 02:14:54 +02:00
##### Metadata:
Retrieve metadata regarding the geoip database.
```
$variable_name metadata < field >
```
2018-06-29 22:20:55 +02:00
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)
2018-10-13 22:36:52 +02:00
- last_change: the last time the database was reloaded (when using auto_reload)
2018-06-29 22:20:55 +02:00
##### 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 >
```
2018-06-26 02:14:54 +02:00
##### GeoIP:
```
$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 ](https://maxmind.github.io/libmaxminddb/mmdblookup.html ):
2016-02-10 21:43:08 +01:00
```
$ mmdblookup --file /usr/share/GeoIP/GeoIP2-Country.mmdb --ip 8.8.8.8
{
2018-06-26 02:14:54 +02:00
"country":
2016-02-10 21:43:08 +01:00
{
2018-06-26 02:14:54 +02:00
"geoname_id":
2016-02-10 21:43:08 +01:00
6252001 < uint32 >
2018-06-26 02:14:54 +02:00
"iso_code":
2016-02-10 21:43:08 +01:00
"US" < utf8_string >
2018-06-26 02:14:54 +02:00
"names":
2016-02-10 21:43:08 +01:00
{
2018-06-26 02:14:54 +02:00
"de":
2016-02-10 21:43:08 +01:00
"USA" < utf8_string >
2018-06-26 02:14:54 +02:00
"en":
2016-02-10 21:43:08 +01:00
"United States" < utf8_string >
}
}
}
$ mmdblookup --file /usr/share/GeoIP/GeoIP2-Country.mmdb --ip 8.8.8.8 country names en
"United States" < utf8_string >
```
2018-06-26 02:14:54 +02:00
This translates to:
```
$country_name "default=United States" source=$remote_addr country names en
```
2020-12-02 00:48:13 +01:00
##### Additional Commands:
These commands works the same as the original ngx_http_geoip_module documented here: http://nginx.org/en/docs/http/ngx_http_geoip_module.html#geoip_proxy.
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.