• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

LICENSEH A D29-Oct-20191.3 KiB2419

README.mdH A D29-Oct-20193.7 KiB134108

configH A D29-Oct-20191.2 KiB4440

ngx_http_geoip2_module.cH A D29-Oct-201923 KiB794628

ngx_stream_geoip2_module.cH A D29-Oct-201920.5 KiB695548

README.md

1Description
2===========
3
4**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)
5
6The module now supports nginx streams and can be used in the same way the http module can be used.
7
8## Installing
9First install [libmaxminddb](https://github.com/maxmind/libmaxminddb) as described in its [README.md
10file](https://github.com/maxmind/libmaxminddb/blob/master/README.md#installing-from-a-tarball).
11
12#### Download nginx source
13```
14wget http://nginx.org/download/nginx-VERSION.tar.gz
15tar zxvf nginx-VERSION.tar.gz
16cd nginx-VERSION
17```
18
19##### To build as a dynamic module (nginx 1.9.11+):
20```
21./configure --add-dynamic-module=/path/to/ngx_http_geoip2_module
22make
23make install
24```
25
26This will produce ```objs/ngx_http_geoip2_module.so```. It can be copied to your nginx module path manually if you wish.
27
28Add the following line to your nginx.conf:
29```
30load_module modules/ngx_http_geoip2_module.so;
31```
32
33##### To build as a static module:
34```
35./configure --add-module=/path/to/ngx_http_geoip2_module
36make
37make install
38```
39
40## Download Maxmind GeoLite2 Database (optional)
41The free GeoLite2 databases are available from [Maxminds website](http://dev.maxmind.com/geoip/geoip2/geolite2/)
42
43[GeoLite2 City](http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz)
44[GeoLite2 Country](http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz)
45
46## Example Usage:
47```
48http {
49    ...
50    geoip2 /etc/maxmind-country.mmdb {
51        auto_reload 5m;
52        $geoip2_metadata_country_build metadata build_epoch;
53        $geoip2_data_country_code default=US source=$variable_with_ip country iso_code;
54        $geoip2_data_country_name country names en;
55    }
56
57    geoip2 /etc/maxmind-city.mmdb {
58        $geoip2_data_city_name default=London city names en;
59    }
60    ....
61
62    fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
63    fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
64    fastcgi_param CITY_NAME    $geoip2_data_city_name;
65    ....
66}
67
68stream {
69    ...
70    geoip2 /etc/maxmind-country.mmdb {
71        $geoip2_data_country_code default=US source=$remote_addr country iso_code;
72    }
73    ...
74}
75```
76
77##### Metadata:
78Retrieve metadata regarding the geoip database.
79```
80$variable_name metadata <field>
81```
82Available fields:
83  - build_epoch: the build timestamp of the maxmind database.
84  - last_check: the last time the database was checked for changes (when using auto_reload)
85  - last_change: the last time the database was reloaded (when using auto_reload)
86
87##### Autoreload (default: disabled):
88Enabling auto reload will have nginx check the modification time of the database at the specified
89interval and reload it if it has changed.
90```
91auto_reload <interval>
92```
93
94##### GeoIP:
95```
96$variable_name [default=<value] [source=$variable_with_ip] path ...
97```
98If default is not specified, the variable will be empty if not found.
99
100If source is not specified, $remote_addr will be used to perform the lookup.
101
102To find the path of the data you want (eg: country names en), use the [mmdblookup tool](https://maxmind.github.io/libmaxminddb/mmdblookup.html):
103
104```
105$ mmdblookup --file /usr/share/GeoIP/GeoIP2-Country.mmdb --ip 8.8.8.8
106
107  {
108    "country":
109      {
110        "geoname_id":
111          6252001 <uint32>
112        "iso_code":
113          "US" <utf8_string>
114        "names":
115          {
116            "de":
117              "USA" <utf8_string>
118            "en":
119              "United States" <utf8_string>
120          }
121      }
122  }
123
124$ mmdblookup --file /usr/share/GeoIP/GeoIP2-Country.mmdb --ip 8.8.8.8 country names en
125
126  "United States" <utf8_string>
127```
128
129This translates to:
130
131```
132$country_name "default=United States" source=$remote_addr country names en
133```
134