centos 环境下使用 nginx geoip2 模块屏蔽特定范围ip

前言:maxmind(lite)已经停止geoip的相关数据库下载了,取而代之的是geoip2数据库,网络上大多数的教程已经过时了,虽然有一些博客有备份数据库,但是ip数据库是动态变化的,因此学习过时的geoip相关教程没有意义。下午整理了一下geoip2相关的教程,不得不说网络上大多数的nginx geoip2相关教程都是误导的,一步步的走下nginx -t时会出错,后来终于找到了一篇靠谱的教程,不过是bt环境下的教程,本篇博客记录了一下lnmp环境下的操作流程。

一:安装geoip2扩展依赖
yum install libmaxminddb-devel -y

二:下载ngx_http_geoip2_module
git clone https://github.com/leev/ngx_http_geoip2_module.git

三:编译Nginx
vi /www/server/panel/install/nginx.sh
--add-module=/root/ngx_http_geoip2_module

四:修改nginx配置
1.在nginx.conf中添加以下代码
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default no;
CN yes;
}

2.在vhost目录下的domain.conf中添加以下代码:
if ($allowed_country = no) {
return 403;
}

centos 环境下使用 nginx geoip2 模块屏蔽特定范围ip》有1个想法

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据