The mod_geoip2 module for Nginx

The mod_geoip2 module for Nginx

一,关于GEOIP

官网:http://www.maxmind.com/zh/home

通过MaxMind的GeoIP产品,您可以对网络访问者的地点、机构、连网速度 及用户类型加以识别。
GeoIP数据库是现有的最常见、 最准确的IP地理定位数据库。 

本文主要介绍的是Geoip2结合Nginx的安装和应用,Geoip(1代)使用更为简单,请参考官方网站。


二,安装ngx_http_geoip2_module的依赖库libmaxminddb

github地址:https://github.com/maxmind/libmaxminddb

注意:按照README.md上的方法,用git clone下来的源码,在编译的时候会报错,原因不明,下载tar包就没有问题。

下载并安装libmaxminddb
yum -y install autoconf automake libtool
wget https://github.com/maxmind/libmaxminddb/releases/download/0.5.5/libmaxminddb-0.5.5.tar.gz
tar -zxvf libmaxminddb-0.5.5.tar.gz
cd libmaxminddb-0.5.5
./configure
make check
make install
ldconfig

三,编译Nginx geoip2模块

github地址: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/

重新编译Nginx
./configure --prefix=/user/local/nginx --add-module=/usr/local/src/ngx_http_geoip2_module


四,应用

免费版Geoip数据库下载地址
http://dev.maxmind.com/geoip/geoip2/geolite2/

以下是Geoip官网根据ip输出maxmind-city.mmdb结构及内容的一个示例:
更多介绍请参考: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":    "ロサンゼルス市",
            "pt-BR":  "Los Angeles",
            "ru":    "Лос-Анджелес",
            "zh-CN": "洛杉矶"
        }
    },
    "continent":  {
        "code":       "NA",
        "geoname_id": 123456,
        "names":  {
            "de":    "Nordamerika",
            "en":    "North America",
            "es":    "América del Norte",
            "fr":    "Amérique du Nord",
            "ja":    "北アメリカ",
            "pt-BR": "América do Norte",
            "ru":    "Северная Америка",
            "zh-CN": "北美洲"


        }
    },
    "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":     "США",
            "zh-CN":  "美国"
        }
    },
    "location":  {
        "accuracy_radius":   20,
        "latitude":          37.6293,
        "longitude":         -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":  "美国"
        }
    },
    "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":  "美国"
        },
        "type": "military"
    },
    "subdivisions":  [
        {
            "confidence":  50,
            "geoname_id": 5332921,
            "iso_code":    "CA",
            "names":  {
                "de":    "Kalifornien",
                "en":    "California",
                "es":    "California",
                "fr":    "Californie",
                "ja":    "カリフォルニア",
                "ru":    "Калифорния",
                "zh-CN": "加州"
            }
        }
    ],
    "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"
    }
}

以下是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;
    ....
}


其中geoip2_data方法的第一个参数为自定义的变量名称,变量值可以在Nginx直接写入日志文件或是传递给php变量。
第二个及后面的参数为geoip2 mmdb数据库中的各级字段名称。
注意:在获取省份信息的时候,注意subdivisions字段下面是个数组,获取方法如下:

geoip2_data        $privince subdivisions 0 names en

结合以上例子和数据结构,可根据自己的需要来获得国家、省份、城市等信息。

你可能感兴趣的:(nginx,geoip2)