1.0 geoip2核心识别库

安装geoip2 lib步骤:

cd /usr/local/src
rm -f libmaxminddb-1.4..tar.gz
wget https://github.com/maxmind/libmaxminddb/releases/download/1.4.2/libmaxminddb-1.4.2.tar.gz
tar -xzf libmaxminddb-1.4..tar.gz
cd libmaxminddb-1.4.
yum install gcc gcc-c++ make -y
./configure
make
make check
sudo make install echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
sudo ldconfig

2.0 下载ngx_http_geoip2_module 模块

cd /usr/local/src
wget https://github.com/leev/ngx_http_geoip2_module/archive/3.3.tar.gz
tar -xzf 3.3.tar.gz
mv ngx_http_geoip2_module-3.3 ngx_http_geoip2_module 

nginx集成步骤:

cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxf nginx-1.16..tar.gz
cd nginx-1.16.1
useradd -M -s /sbin/nologin www yum install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel -y
./configure --user=www --group=www --prefix=/usr/local/nginx \
--with-ld-opt="-Wl,-rpath -Wl,/usr/local/lib" \
--with-http_sub_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-http_v2_module \
--add-module=/usr/local/src/ngx_http_geoip2_module make
make install

geoip2 IP地址库下载:

2020年最新GeoLite2-City.mmdb 无法直接下载,必须注册maxmind账号

. 需要在maxmind 后台注册账号,并且生成Account/User ID 和 License key
. 安装geoipupdate, 下载地址https://github.com/maxmind/geoipupdate/releases
. 配置geoipupdate的GeoIP.conf , 填写maxmind账号的User ID 和 License key 和 EditionIDs 博主使用的是centos
安装如下
cd /usr/local/src/
wget https://github.com/maxmind/geoipupdate/releases/download/v4.2.0/geoipupdate_4.2.0_linux_amd64.rpm
rpm -ivh geoipupdate_4..0_linux_amd64.rpm
rpm -ql geoipupdate
vi /etc/GeoIP.conf
#填写AccountID XXXX
#填写LicenseKey XXXX
#EditionIDs可以不修改,系统默认有填 GeoLite2-Country GeoLite2-City
#笔者EditionIDs只保留GeoLite2-City
#保存退出 运行geoipupdate
/usr/bin/geoipupdate
cd /usr/share/GeoIP/
会看到GeoLite2-City.mmdb
把GeoLite2-City.mmdb文件cp到需要使用的目录
sudo mkdir -p /usr/local/nginx/geoip/
\cp -rf /usr/share/GeoIP/GeoLite2-City.mmdb /usr/local/nginx/geoip/maxmind-city.mmdb

注意GeoLite2 City 和GeoLite2 Country 两个IP库,请下载City的mmdb数据文件,较于其他两者信息更丰富

nginx 配置geoip2 样例,geoip2的配置字段在http

http {
...
geoip2 /usr/local/nginx/geoip/maxmind-city.mmdb {
$geoip2_data_country_code default=US source=$remote_addr country iso_code;
$geoip2_data_country_name country names en;
$geoip2_data_city_name default=London city names en;
$geoip2_data_province_name subdivisions names en;
$geoip2_data_province_isocode subdivisions iso_code;
}
.... fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
fastcgi_param CITY_NAME $geoip2_data_city_name;
....
} stream {
...
geoip2 /usr/local/nginx/geoip/maxmind-city.mmdb {
$geoip2_data_country_code default=US source=$remote_addr country iso_code;
$geoip2_data_country_name country names en;
$geoip2_data_city_name default=London city names en;
$geoip2_data_province_name subdivisions names en;
$geoip2_data_province_isocode subdivisions iso_code;
}
...
}

nginx配置中的变量名如,geoip2_data_country_code,geoip2_data_country_name 等等,都是自定义的名称,可以加在日志字段中

3.0 在nginx 中配置黑名单国家的变量 $blacklist_country

在http{} 字段,任何include之前,添加如下配置

[...]
map geoip2_data_country_code $allowed_country {
default yes;
US no;
JP no;
SG no;
}
[...]

以上配置将允许所有的国家,除了美国,日本,和新加坡 (您可以查看所有国家代码的列表,点这里

相反的,如果你想阻止所有国家,除了少数几个国家可以访问,请参考如下的配置

[...]
map geoip2_data_country_code $allowed_country {
default no;
CN yes;
HK yes;
US yes;
}
[...]

现在,你做了如上配置,并不会阻止任何的国家,那只是设置了一个变量$allowed_country
想要实际阻止国家/地区,必须修改vhost的配置
把下面的代码放到server{}字段, 这个代码也可以放到location{}字段

[...]
if ($allowed_country = no) {
return 403;
}
[...]

任何从黑名单国家的用户访问,都会收到403错误代码,

修改完配置,不要忘记reload nginx

/usr/local/nginx/sbin/nginx -s reload

4.0 通过如下命令可以直接本地查询IP信息

/usr/local/bin/mmdblookup --file /usr/local/nginx/geoip/maxmind-city.mmdb --ip 8.8.8.8

会出来许多信息,用json格式展示

  {
"country":
{
"geoname_id":
<uint32>
"iso_code":
"US" <utf8_string>
"names":
{
"de":
"USA" <utf8_string>
"en":
"United States" <utf8_string>
}
}
}

如果IP 后面可以跟不同的查询,如下面查询了国家的英文名

/usr/local/bin/mmdblookup --file /usr/local/nginx/geoip/maxmind-city.mmdb --ip 8.8.8.8 country names en
"United States" <utf8_string>

最新文章

  1. express不是内部或外部命令
  2. jQuery实现鼠标经过图片变亮效果
  3. ASP.NET页面中去除VIEWSTATE视
  4. 自定义Button 的图片设置不显示问题。
  5. ASP.NET本质论第二章应用程序对象学习笔记1
  6. MDI窗体容器和权限设置.avi
  7. Java之循环练习2
  8. [转]高并发访问下避免对象缓存失效引发Dogpile效应
  9. bashrc的加载
  10. Javascript获取浏览器版本
  11. Array数组常用的5个方法
  12. MySQL(九)之数据表的查询详解(SELECT语法)二
  13. Android Jetpack之AppCompat(一)
  14. Centos6系列Bond配置方法
  15. ncm 让跨项目配置一致性简单的工具
  16. NIO[读]、[写]在同一线程(单线程)中执行,让CPU使用率最大化,提高处理效率
  17. Android 开源动画框架:NineOldAndroids
  18. Daily Scrum 11.14
  19. 一款基jquery超炫的动画导航菜单
  20. scala 时间格式转换(String、Long、Date)

热门文章

  1. PMP--2.1 商业论证(经济可行性研究报告)
  2. java设计模式学习笔记——里氏替换原则
  3. 获取域hash并破解
  4. 腾讯2020校园招聘-后台&amp;综合-第一次笔试 题解
  5. JAVA架构师眼中的高并发架构,分布式架构 应用服务器集群
  6. java动态拼接sql语句并且执行时给sql语句的参数赋值
  7. mssql sqlserver 如何将一个日期数据转换为&quot;年份-月份&quot;的格式呢?
  8. scanf 与fgets
  9. [Python]scatter_matrix报错 module &#39;pandas&#39; has no attribute &#39;scatter_matrix&#39;
  10. netty 的事件驱动