2020年,最新NGINX的ngx_http_geoip2模块以精准禁止特定国家或者地区IP访问
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>
2020年,最新NGINX的ngx_http_geoip2模块以精准禁止特定国家或者地区IP访问的更多相关文章
- nginx的ngx_http_geoip2模块以精准禁止特定地区IP访问
要求:对网站的信息,比如某个访问节点不想国内或者国外的用户使用,禁止国内或者国外或者精确到某个城市的那种情况. 解决方式:1.Cloudfalre来实现禁止特定国家的ip访问,比较简单,但是需要mon ...
- 企业运维实践-Nginx使用geoip2模块并利用MaxMind的GeoIP2数据库实现处理不同国家或城市的访问最佳实践指南
关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x00 前言 ...
- nginx禁止特定UA访问
一.UA是什么? User Agent 简称UA,就是用户代理.通常我们用浏览器访问网站,在网站的日志中,我们的浏览器就是一种UA. 二.禁止特定UA访问 最近有个网站(www.C.com)抄袭公司主 ...
- Nginx安装echo模块
echo-nginx-module 模块可以在Nginx中用来输出一些信息,可以用来实现简单接口或者排错. 项目地址:https://github.com/openresty/echo-nginx-m ...
- 开始Nginx的SSL模块
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/n ...
- nginx內建模块使用
目录 nginx內建模块使用 1. 內建模块的引入 1.1 查看安装信息 1.2 重新指定配置信息 2. 內建模块的使用 2.1 http_stub_status_module 2.2 http_ra ...
- nginx 安装第三方模块(lua)并热升级
需求: nginx上将特定请求拒绝,并返回特定值. 解决办法: 使用lua脚本,实现效果. 操作步骤: 安装Luajit环境 重新编译nginx(目标机器上nginx -V 配置一致,并新增两个模块n ...
- 2020年最新ZooKeeper面试题(附答案)
2020年最新ZooKeeper面试题 1. ZooKeeper 是什么? ZooKeeper 是一个开源的分布式协调服务.它是一个为分布式应用提供一致性服务的软件,分布式应用程序可以基于 Zooke ...
- Nginx 编译 echo 模块
Nginx 编译 echo 模块 echo模块下载地址:https://github.com/openresty/echo-nginx-module 查看nginx已经编译的模块, nginx -V ...
随机推荐
- .NET知识梳理——1.泛型Generic
1. 泛型Generic 1.1 引入泛型:延迟声明 泛型方法声明时,并未写死类型,在调用的时候再指定类型. 延迟声明:推迟一切可以推迟的. 1.2 如何声明和使用泛型 泛 ...
- hadoop3自学入门笔记(3)-java 操作hdfs
1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> <val ...
- 使用jdbc将mysql数据库中的内容封装为指定对象的list集合
使用jdbc将mysql数据库中的内容封装为指定对象的list集合 public List<User> findAll() { private JdbcTemplate template ...
- Java数据结构--双向链表的实现
#java学习经验总结------双向链表的实现 双向链表的建立与单链表类似,只是需要使用pre指针指向前一个结点,并且在删除添加时不仅仅考虑next package datastructure; p ...
- Linux下VIM编译器的使用以及shell编程基础
VIM编译器的安装与使用 vim编辑器安装 在CentOS中,执行:yum -y install vim 普通模式 h: 左移一个字符 j: 下移一行 k: 上移一行 l: 右移一个字符 PageDo ...
- RPC远程过程调用(Remote Procedure Call)
RPC,就是Remote Procedure Call,远程过程调用 远程过程调用,自然是相对于本地过程调用 本地过程调用,就好比你现在在家里,你要想洗碗,那你直接把碗放进洗碗机,打开洗碗机开关就可以 ...
- C# 如何获取日期时间各种方法
我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2019-01-09).时间(16:02:12).日期+时间(2019-01-09 16 ...
- Network Emulator for Windows Toolkit(模拟弱网络环境的软件)
前言和下载地址 用户会在各种网络环境下使用我们的app,pc应用,我们决不能祈求用户的网络环境都是稳定的,因此我们需要模拟出弱网络的情况,用来测试我们的APP在弱网络环境下的表现如何. Network ...
- sqlserver with 递归用法
DECLARE @companyid TABLE ( [Id] [int] ); with cte as( union all select a.Id from [base].[Company] a, ...
- 【UWP】在 UWP 中使用 Exceptionless 进行遥测
2020年1月17日更新: nightly build 版本已发布 https://www.myget.org/feed/exceptionless/package/nuget/Exceptionle ...