2014-01-31 00:00:34 +01:00
|
|
|
Description
|
|
|
|
===========
|
|
|
|
|
2014-03-11 11:15:11 +01:00
|
|
|
**ngx_http_geoip2_module** - creates variables with values from the maxmind geoip2 databases based on the client IP (supports both IPv4 and IPv6)
|
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
|
|
|
|
```
|
|
|
|
wget http://nginx.org/nginx-VERSION.tar.gz
|
|
|
|
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
|
|
|
|
make
|
|
|
|
make install
|
|
|
|
```
|
|
|
|
|
2014-03-13 13:46:27 +01:00
|
|
|
## Download Maxmind GeoLite2 Database (optional)
|
|
|
|
The free GeoLite2 databases are available from [Maxminds website](http://dev.maxmind.com/geoip/geoip2/geolite2/)
|
|
|
|
|
|
|
|
[GeoLite2 City](http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz)
|
|
|
|
[GeoLite2 Country](http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz)
|
|
|
|
|
2014-03-11 11:19:43 +01:00
|
|
|
## Example Usage:
|
|
|
|
```
|
|
|
|
http {
|
|
|
|
...
|
2014-04-26 12:26:37 +02:00
|
|
|
geoip2 /etc/maxmind-country.mmdb {
|
|
|
|
$geoip2_data_country_code default=US country iso_code;
|
|
|
|
$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-02-10 21:43:08 +01:00
|
|
|
|
|
|
|
To find the path of the data you want (eg: city names en), use the [mmdblookup tool](https://maxmind.github.io/libmaxminddb/mmdblookup.html):
|
|
|
|
|
|
|
|
```
|
|
|
|
$ mmdblookup --file /usr/share/GeoIP/GeoIP2-Country.mmdb --ip 8.8.8.8
|
|
|
|
|
|
|
|
{
|
|
|
|
"country":
|
|
|
|
{
|
|
|
|
"geoname_id":
|
|
|
|
6252001 <uint32>
|
|
|
|
"iso_code":
|
|
|
|
"US" <utf8_string>
|
|
|
|
"names":
|
|
|
|
{
|
|
|
|
"de":
|
|
|
|
"USA" <utf8_string>
|
|
|
|
"en":
|
|
|
|
"United States" <utf8_string>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$ mmdblookup --file /usr/share/GeoIP/GeoIP2-Country.mmdb --ip 8.8.8.8 country names en
|
|
|
|
|
|
|
|
"United States" <utf8_string>
|
|
|
|
```
|