nginx添加Geoip2,根据访客IP解析国家、城市等信息


1.Nginx添加Geop2模块

#libmaxminddb(api库) https://github.com/maxmind/libmaxminddb/releases
#解压
sudo tar -xvf libmaxminddb-1.4.3.tar.gz
cd libmaxminddb-1.4.3
#安装
sudo ./configure && sudo make && sudo make install
#加入lib path 避免找不到
sudo sh -c "echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf"
sudo ldconfig

默认nginx安装未添加此模块需要添加,
模块下载地址:https://github.com/leev/ngx_http_geoip2_module

#nginx 编译添加模块
--add-module=/your_geoip2_module_path

2.下载Maxmind GeoLite2 Database

注册用户,并下载数据库文件
https://www.maxmind.com/en/accounts/728274/geoip/downloads

3.更新Geoip数据库

安装geoipupdate
https://dev.maxmind.com/geoip/updating-databases
https://github.com/maxmind/geoipupdate
获取geoipupdate配置文件(含有key等信息)

yum localinstall -y https://github.com/maxmind/geoipupdate/releases/download/v6.0.0/geoipupdate_6.0.0_linux_amd64.rpm

rpm -ql geoipupdate
/etc/GeoIP.conf #配置文件
/usr/bin/geoipupdate
/usr/share/GeoIP #更新的数据库文件位置
/usr/share/doc/geoipupdate/CHANGELOG.md
/usr/share/doc/geoipupdate/GeoIP.conf
/usr/share/doc/geoipupdate/GeoIP.conf.md
/usr/share/doc/geoipupdate/LICENSE-APACHE
/usr/share/doc/geoipupdate/LICENSE-MIT
/usr/share/doc/geoipupdate/README.md
/usr/share/doc/geoipupdate/geoipupdate.md
/usr/share/man/man1/geoipupdate.1
/usr/share/man/man5/GeoIP.conf.5


[root@GreenCloud GeoIP]# geoipupdate --help
Usage: geoipupdate <arguments>
  -f, --config-file string          Configuration file (default "/etc/GeoIP.conf")
  -d, --database-directory string   Store databases in this directory (uses config if not specified)
  -h, --help                        Display help and exit
  -o, --output                      Output download/update results in JSON format
      --parallelism int             Set the number of parallel database downloads
  -v, --verbose                     Use verbose output
  -V, --version                     Display the version and exit

[root@GreenCloud GeoIP]# geoipupdate -v
geoipupdate version 6.0.0 (62d961ef-modified, 2023-07-12T19:12:59Z, linux-amd64)
Using config file /etc/GeoIP.conf
Using database directory /usr/share/GeoIP
Initializing file lock at /usr/share/GeoIP/.geoipupdate.lock
Acquired lock file at /usr/share/GeoIP/.geoipupdate.lock
Calculated MD5 sum for /usr/share/GeoIP/GeoLite2-ASN.mmdb: 4a1303c9250ffcc1684817c8bcec8ac7
Requesting updates for GeoLite2-ASN: https://updates.maxmind.com/geoip/databases/GeoLite2-ASN/update?db_md5=4a1303c9250ffcc1684817c8bcec8ac7
No new updates available for GeoLite2-ASN
Database GeoLite2-ASN up to date
Calculated MD5 sum for /usr/share/GeoIP/GeoLite2-City.mmdb: 7daf839efacf2396c1fc846a338802f0
Requesting updates for GeoLite2-City: https://updates.maxmind.com/geoip/databases/GeoLite2-City/update?db_md5=7daf839efacf2396c1fc846a338802f0
No new updates available for GeoLite2-City
Database GeoLite2-City up to date
Calculated MD5 sum for /usr/share/GeoIP/GeoLite2-Country.mmdb: 5afa0d8215e376cf5cbebf2ec8e29c90
Requesting updates for GeoLite2-Country: https://updates.maxmind.com/geoip/databases/GeoLite2-Country/update?db_md5=5afa0d8215e376cf5cbebf2ec8e29c90
No new updates available for GeoLite2-Country
Database GeoLite2-Country up to date
Lock file /usr/share/GeoIP/.geoipupdate.lock successfully released

4.配置nginx

配置geoip2的nginx配置文件

vim /etc/nginx/conf.d/geoip2.conf
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
         auto_reload 5m;
         $geoip2_metadata_country_build metadata build_epoch;
         #国家编码
         $geoip2_country_code source=$realip country iso_code;
         #国家英文名
         $geoip2_country_name_en source=$realip country names en;
         #国家中文名
         $geoip2_country_name_cn source=$realip country names zh-CN;
}
geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
      $geoip2_metadata_city_build metadata build_epoch;
      #城市英文名,大多是拼音,有重复情况
      $geoip2_city_name_en source=$realip city names en;
      #城市中文名,部分城市没有中文名
      $geoip2_city_name_cn source=$realip city names zh-CN;
      #城市id,maxmaind 库里的id,非国际标准
      $geoip2_data_city_code source=$realip city geoname_id;
}


map $http_x_forwarded_for $realip {
        ~^(\d+\.\d+\.\d+\.\d+) $1;
        default $remote_addr;
}


#vim /etc/nginx/conf.d/ip.cqgyd.conf
server {
        listen 80;
        server_name ip.cqgyd.com;
        access_log  /var/log/nginx/ip_80.access.log  main;
        #default_type text/plain;
        #return 200 $remote_addr;

        location / {
        default_type application/json;
        #return 200 '{ yourIp:$remote_addr\n countryCode: $geoip2_country_code\n countryNameEn: $geoip2_country_name_e_name_en\n cityNameCn: $geoip2_city_name_cn\n cityCode: $geoip2_data_city_code\n';
        return 200 '{"yourIp":"$remote_addr", "countryCode": "$geoip2_country_code", "countryNameEn": "$geoip2_country_ngeoip2_city_name_en", "cityNameCn": "$geoip2_city_name_cn", "cityCode": "$geoip2_data_city_code"}';
    }

}

重载nginx

5.测试

curl ip.cqgyd.com
{"yourIp":"72.18.214.41", "countryCode": "US", "countryNameEn": "United States", "countryNameCn": "美国", "cityNameEn": "Kansas City", "cityNameCn": "", "cityCode": "4393217"}

参考连接:
https://blog.csdn.net/zdmoon/article/details/109514541
https://nginx.org/en/docs/http/ngx_http_geoip_module.html

声明:鹅石壳儿|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - nginx添加Geoip2,根据访客IP解析国家、城市等信息


Carpe Diem and Do what I like