The mod_geoip2 module for Nginx
1. About GEOIP
Official Website: http://www.maxmind.com/zh/home
With MaxMind's GeoIP product, you can identify the locations, organizations, connection speeds, and user types of network visitors.
GeoIP database is the most common and accurate IP location database.
This article describes how to install and use Geoip2 in combination with Nginx. For more information about how to use Geoip (Generation 1), see the official website.
2. Install the dependency library libmaxminddb of ngx_http_geoip2_module
Github address: https://github.com/maxmind/libmaxminddb
Note: according to the method on README. md, the source code cloned by git will report an error during compilation. The reason is unknown. It is no problem to download the tar package.
Download and install libmaxminddb
yum -y install autoconf automake libtoolwget https://github.com/maxmind/libmaxminddb/releases/download/0.5.5/libmaxminddb-0.5.5.tar.gztar -zxvf libmaxminddb-0.5.5.tar.gzcd libmaxminddb-0.5.5./configuremake checkmake installldconfig
3. Compile the Nginx geoip2 Module
Github address: https://github.com/leev/ngx_http_geoip2_module
Wget https://github.com/leev/ngx_http_geoip2_module/archive/master.zip
Unzip master
Mv ngx_http_geoip2_module/user/local/src/
Recompile Nginx
./Configure -- prefix =/user/local/nginx -- add-module =/usr/local/src/ngx_http_geoip2_module
Iv. Application
Free version of Geoip Database
Http://dev.maxmind.com/geoip/geoip2/geolite2/
The following is an example of the structure and content of the maxmind-city.mmdb output by the Geoip Website Based on ip:
For more information, see: http://dev.maxmind.com/geoip/geoip2/web-services/
{"City": {"confidence": 25, "geoname_id": 54321, "names": {"de": "Los Angeles", "en ": "Los Angeles", "es": "Los ángeles", "fr": "Los Angeles", "ja": "Los angeleles City ", "pt-BR": "Los Angeles", "ru": "when there are too many bytes in the queue", "zh-CN ": "Los Angeles" }}, "continent": {"code": "NA", "geoname_id": 123456, "names": {"de": "Nordamerika ", "en": "North America", "es": "amé rica del Norte", "fr": "amé rique du Nord", "ja ": "Bei Jing", "pt-BR": "amé rica do Norte", "ru ": "zookeeper", "zh-CN": "North America" }}, "country": {"confidence ": 75, "geoname_id": "6252001", "iso_code": "US", "names": {"de": "USA", "en": "United States ", "es": "Estados Unidos", "fr": "É tats-Unis", "ja": "アメ ", "pt-BR ": "Estados Unidos", "ru": "zookeeper", "zh-CN": "USA" }}, "location": {"accuracy_radius": 20, "latitude": 37.6293, "longpolling":-122.1163, "metro_code": 807, "time_zone": "America/Los_Angeles"}, "postal": {"code ": "90001", "confidence": 10}, "registered_country": {"geoname_id": "6252001", "iso_code": "US", "names ": {"de": "USA", "en": "United States", "es": "Estados Unidos", "fr": "É tats-Unis ", "ja": "アメ ", "pt-BR": "Estados Unidos", "ru": "С Ш А ", "zh-CN": "USA" }}, "represented_country": {"geoname_id": "6252001", "iso_code": "US", "names ": {"de": "USA", "en": "United States", "es": "Estados Unidos", "fr": "É tats-Unis ", "ja": "アメ ", "pt-BR": "Estados Unidos", "ru": "С Ш А ", "zh-CN": "USA"}, "type": "military"}, "subdivisions": [{"confidence": 50, "geoname_id": 5332921, "iso_code": "CA", "names": {"de": "Kalifornien", "en": "California", "es": "California ", "fr": "Californie", "ja": "There are too many other large numbers of numbers", "ru": "There are too many numbers of numbers ", "zh-CN": "California" }}], "traits": {"autonomous_system_number": "1239", "autonomous_system_organization": "Linkem IR WiMax Network ", "domain": "example.com", "is_anonymous_proxy": true, "is_satellite_provider": true, "isp": "Linkem spa", "ip_address": "1.2.3.4 ", "organization": "Linkem IR WiMax Network", "user_type": "traveler",}, "maxmind": {"queries_remaining": "54321 "}}
The following is an official application example of ngx_http_geoip2_modul:
# Example Usage:
'''
http { ... geoip2_mmdb /etc/maxmind-city.mmdb; geoip2_data $geoip2_data_country_code country iso_code; geoip2_data $geoip2_data_country_name country names en; geoip2_data $geoip2_data_city_name city names en; geoip2_data $geoip2_data_geoname_id country geoname_id; geoip2_data $geoip2_data_latitude location latitude; geoip2_data $geoip2_data_country_code default=US country iso_code; ....}
The first parameter of the geoip2_data method is the custom variable name. The variable value can be directly written to the log file in Nginx or transmitted to the php variable.
The second and subsequent parameters are field names at all levels in the geoip2 mmdb database.
Note: When obtaining province information, note that the subdivisions field is an array. The method is as follows:
geoip2_data $privince subdivisions 0 names en
Based on the preceding examples and data structures, you can obtain information about the country, province, and city as needed.