chaihongjun.me

Geoip2在nginx服务器上的应用

很久之前写过一篇文章《nginx搭配GeoIP数据屏蔽访问》,最近在整理服务器查看访客日志的时候,发现参照那篇文章读起来有点不顺畅,当时,可能记录的比较的仓促,于是又重新梳理了一遍,以便以后查阅。

0. 注册maxmind账户

注册

https://www.maxmind.com/en/geolite2/signup

注册的邮箱将会作为登录账户名,注册通过后会收到一封邮件提醒设置账户密码(可重置密码)

 

1. 获取License

登录账户

https://www.maxmind.com/en/account/login

利用前面注册的账户密码登录 maxmind

申请License

在登录的账户后台点击左侧菜单Manage License Keys,位于【Account】下,然后再点击右侧的Generate new license key,根据提示操作,最后确认即可获得License,这里的License和【Account/User ID】后续配置升级数据库配置脚本会用到

 

2. 下载数据文件并上传到服务器

下载

依然是在账户后台点击左侧菜单Download Files,位于【GeoIP2 / GeoLite2】下,然后依次下载【GeoLite2 City】和【GeoLite2 Country】,建议Download GZIP。下载得到的是两个压缩文件,分别解压之后分别得到文件GeoLite2-City.mmdbGeoLite2-Country.mmdb

上传

将以上两个文件上传至服务器某个目录,比如/alidata1/GeoDatabases,两个文件名建议不要修改,因为后续配置需要引用

以上存放的地址一定记住路径,后续需要使用,如果你配置了其他的路径也注意后续使用的时候保持一致

3. 安装nginx的模块ngx_http_geoip2_module

下载模块

https://github.com/leev/ngx_http_geoip2_module/release

选择最新版本下载

安装模块

./configure {之前nginx的配置参数 } --add-module=/path/to/ngx_http_geoip2_module
 make

这里后面操作请参考《WEB不停服nginx平滑静默升级和跨版本升级注意事项》,实际相当于Nginx的平滑升级。

4. 安装maxmind支持库

下载支持库

https://github.com/maxmind/libmaxminddb/releases

选择最新版本下载

安装支持库

建议root模式下,解压进入目录之后

$ ./configure
$ make
$ make check
$ make install
$ ldconfig

5. 配置生效

创建IP地址库解析映射配置文件

该配置文件名称和路径/usr/local/nginx/conf/Geoip2.conf,内容如下:

#IP地址库解析映射
geoip2 /alidata1/GeoDatabases/GeoLite2-Country.mmdb {
     auto_reload 5m;
     $geoip2_metadata_country_build metadata build_epoch; 
     $geoip2_country_code country iso_code;
     $geoip2_country_name country names en;
    }
geoip2 /alidata1/GeoDatabases/GeoLite2-City.mmdb {
      $geoip2_metadata_city_build metadata build_epoch;
      $geoip2_data_city_name city names en;
      $geoip2_data_continent_code continent code;
      $geoip2_data_continent_name continent names en;
      $geoip2_data_country_code country iso_code;
      $geoip2_data_country_name country names en;
      $geoip2_data_region_iso subdivisions 0 iso_code;
      $geoip2_data_region_name subdivisions 0 names en;
    }

以上配置,后期读取nginx日志的时候,中文地名将会以拼音形式显示,如果想以中文方式显示,则使用如下配置:

#IP地址库解析映射 
geoip2 /alidata1/GeoDatabases/GeoLite2-Country.mmdb {
     auto_reload 5m;
     $geoip2_metadata_country_build metadata build_epoch; 
     $geoip2_country_code country iso_code;
     $geoip2_country_name country names zh-CN;
    }
geoip2 /alidata1/GeoDatabases/GeoLite2-City.mmdb {
    $geoip2_metadata_city_build metadata build_epoch;
    $geoip2_data_city_name city names zh-CN;
    $geoip2_data_continent_code continent code;
    $geoip2_data_continent_name continent names zh-CN;
    $geoip2_data_country_code country iso_code;
    $geoip2_data_country_name country names zh-CN;
    $geoip2_data_region_iso subdivisions 0 iso_code;
    $geoip2_data_region_name subdivisions 0 names zh-CN;
   }

以上配置二选一

修改fastcgi.conf

# 在文件末尾添加如下
# Geoip2
 fastcgi_param GEOIP2_CITY_BUILD_DATE $geoip2_metadata_city_build;
 fastcgi_param GEOIP2_CITY $geoip2_data_city_name;
 fastcgi_param GEOIP2_CONTINENT_CODE $geoip2_data_continent_code;
 fastcgi_param GEOIP2_CONTINENT_NAME $geoip2_data_continent_name;
 fastcgi_param GEOIP2_COUNTRY_CODE $geoip2_data_country_code;
 fastcgi_param GEOIP2_COUNTRY_NAME $geoip2_data_country_name;
 fastcgi_param GEOIP2_REGION $geoip2_data_region_iso;
 fastcgi_param GEOIP2_REGION_NAME $geoip2_data_region_name;

以上配置完成,可以方便在nginx配置文件中使用自定义的变量,比如$geoip2_data_country_name,$geoip2_data_region_name,后续实际可使用上

6. 数据文件升级

下载数据文件升级工具

/uploads/allimg/211119/234R13429-2.jpg

选择最新版本下载,解压得到文件geoipupdate,将文件放入目录/usr/local/bin

配置数据文件升级脚本

创建文件/usr/local/etc/GeoIP.conf,内容如下:

 # The following AccountID and LicenseKey are required placeholders.
 # For geoipupdate versions earlier than 2.5.0, use UserId here instead of AccountID.
 # 下面两项可以在 Manage License Keys 获取
 AccountID 0
 LicenseKey 000000000000
    
 # Include one or more of the following edition IDs:
 # * GeoLite2-City - GeoLite 2 City
 # * GeoLite2-Country - GeoLite2 Country
 # For geoipupdate versions earlier than 2.5.0, use ProductIds here instead of EditionIDs.
 EditionIDs GeoLite2-City GeoLite2-Country
 
 # 自定义的数据库目录
 DatabaseDirectory /alidata1/GeoDatabases

建议不要修改这个文件的文件名和保存路径,因为升级程序默认读取/usr/local/etc/GeoIP.conf作为升级配置脚本

定时升级配置

使用Linux自带的crontab,创建定时升级任务

32 6 * * 3 /usr/local/bin/geoipupdate

7. 实际应用

1. nginx访问日志显示每个IP的物理地址

配置nginx主配置文件

# /usr/local/nginx/conf/nginx.conf
...
# 加载IP地址库解析映射配置文件
include  /usr/local/nginx/conf/Geoip2.conf;

修改日志记录格式

# 依然是在nginx.conf文件内
# 加载IP地址库解析映射配置文件
include  /usr/local/nginx/conf/Geoip2.conf;
# 修改日志格式
log_format main escape=json
   [$remote_addr]-[$time_local]-[$request]-[$status]-[$http_user_agent]-[$uri]-[$geoip2_data_country_name]-[$geoip2_data_region_name]-[$geoip2_data_city_name];

注意上面配置的以$geoip2开头的变量,都是在fastcgi.conf文件内自定义的变量名,这里要和前面的保持一致,另外这里的main要和站点access_log后的配置名称一致比如:

access_log /alidata1/logs/www.samplesites.com_nginx.log main;

重新加载nginx,刷新nginx日志就可以看到IP对应的物理地址了

2. 禁止某地访问站点

# 禁止河北省访问
if ($geoip2_data_region_name ~* Hebei){
    return 444;
}
# 禁止广州访问
if ($geoip2_data_city_name ~* Guangzhou){
    return 444;
}


知识共享许可协议本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。作者:chaihongjun»