Nginx之IP国家代码ngx_http_geoip2_module模块简介和使用
一、ngx_http_geoip2_module模块简介
ngx_http_geoip2_module模块使用预编译的MaxMind数据库创建变量,其值取决于客户端 IP 地址 ,通过此模块我们可以精准的限制或者允许某个国家的IP地址访问WEB站点。MaxMind的GeoIP2和GeoLite2 IP智能产品和服务用于发现有关特定IP地址的信息。我们提供免费和付费网络服务、基于订阅的可下载数据库和免费可下载数据库。我们学习使用免费版本的就可以,当然付费版本的更新更及时,内容更详细(包括国家、城市、经纬度、运营商、甚至企业信息)。
二、安装步骤
1、下载libmaxminddb软件包
[root@s141 local]# wget https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz
2、解压libmaxminddb软件包
[root@s141 local]# tar -zxvf libmaxminddb-1.6.0.tar.gz
[root@s141 local]# cd libmaxminddb-1.6.0
[root@s141 libmaxminddb-1.6.0]#
3、编译并安装libmaxminddb软件包
[root@s141 libmaxminddb-1.6.0]# ./configure
[root@s141 libmaxminddb-1.6.0]# make
[root@s141 libmaxminddb-1.6.0]# make install
4、加载libmaxminddb库
[root@s141 local]# echo ‘/usr/local/lib’ > /etc/ld.so.conf.d/geoip.conf
[root@s141 local]# ldconfig
5、下载ngx_http_geoip2_module模块
[root@s141 local]# wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/heads/master.zip
6、解压ngx_http_geoip2_module模块
[root@s141 local]# unzip master.zip
Archive: master.zip
a26c6beed77e81553686852dceb6c7fdacc5970d
creating: ngx_http_geoip2_module-master/
inflating: ngx_http_geoip2_module-master/LICENSE
inflating: ngx_http_geoip2_module-master/README.md
inflating: ngx_http_geoip2_module-master/config
inflating: ngx_http_geoip2_module-master/ngx_http_geoip2_module.c
inflating: ngx_http_geoip2_module-master/ngx_stream_geoip2_module.c
7、检查nginx版本并确认编译参数
[root@s141 opt]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC’ --with-ld-opt=’-Wl,-z,relro -Wl,-z,now -pie’ --add-module=/opt/echo-nginx-module-0.62
8、下载对应版本nginx
[root@s141 opt]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
9、静态编译安装ngx_http_geoip2_module模块
[root@s141 nginx-1.20.1]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC’ --with-ld-opt=’-Wl,-z,relro -Wl,-z,now -pie’ --add-module=/opt/echo-nginx-module-0.62 --add-module=/usr/local/ngx_http_geoip2_module-master
…
[root@s141 nginx-1.20.1]# make
10、覆盖原nginx二进制文件
[root@s141 nginx-1.20.1]# cp -f objs/nginx /usr/sbin/nginx
cp: overwrite ‘/usr/sbin/nginx’? y
11、重启nginx
[root@s141 nginx-1.20.1]# systemctl stop nginx
[root@s141 nginx-1.20.1]# systemctl start nginx
13、官网下载免费的
访问官网下载免费版本的GeoIP2库,我们下载压缩包,里面是二进制mmdb格式的库文件。下载链接,需要注册登录下载。如果无法登录官网可以通过CSDN下载,CSDN下载地址。
14、将下载的文件上传到服务器
将二进制库文件上传到/usr/share/GeoIP目录
[root@s141 GeoIP]# pwd
/usr/share/GeoIP
[root@s141 GeoIP]# ll
total 78364
-rw-r–r-- 1 root root 74230321 Nov 24 15:57 GeoLite2-City.mmdb
-rw-r–r-- 1 root root 6011025 Nov 24 15:57 GeoLite2-Country.mmdb
15、编辑nginx.conf文件
[root@s141 GeoIP]# cd /etc/nginx/
[root@s141 nginx]# vim nginx.conf
在http模块中加入如下内容:
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {$geoip2_country_code country names en;}geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {$geoip2_data_country_code default=China source=$remote_addr country iso_code;$geoip2_data_country_name country names en;$geoip2_data_city_name city names en;$geoip2_data_province_name subdivisions 0 names en;$geoip2_data_province_isocode subdivisions 0 iso_code;$geoip2_continent_code continent code;}
日志格式配置中加入国家代码
16、验证安装是否成功
使用nginx -t命令验证配置文件测试通过,说明模块以及安装成功
[root@s141 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
三、实践实验——自建公网IP查询站点
1、在启用的url中添加如下内容
location /myip {default_type text/plain;return 200 "$remote_addr $geoip2_data_city_name $geoip2_country_code $geoip2_continent_code\n";}
2、重新加载nginx配置文件
(base) [root@sun-site nginx]# nginx -s reload
3、使用域名访问验证
[root@jiujiu ~]# curl https://download.sun-site.com/myip
152.32.150.199 United States NA
4、配置国家地址黑名单
添加如下配置在http块内:
(base) [root@sun-site nginx]# vim nginx.conf
#include前添加如下内容
map $geoip2_data_country_code $allowed_country {US no;default yes; }
(base) [root@sun-site nginx]# vim conf.d/download.conf
#在server内或者location内添加如下内容if ($allowed_country = no) {return 403;} 重新加载nginx配置(base) [root@sun-site nginx]# nginx -s reload
5、黑名单效果验证
黑名单国家地址访问URL返回403
其他国家访问返回正常信息
四、QA
1、执行nginx -V时报错libmaxminddb.so.0文件找不到
- 原因:libmaxminddb动态库未加载
- 解决方案:
#加载libmaxminddb动态库
#echo ‘/usr/local/lib’ > /etc/ld.so.conf.d/geoip.conf
#ldconfig
2、常用变量有哪些?
- $geoip_country_code
双字符国家代码,比如 “RU”,“US”。 - $geoip_country_code3
三字符国家代码,比如 “RUS”,“USA”。 - $geoip_country_name
国家名称,比如 “Russian Federation”,“United States”。 - $geoip_city_country_code
双字符国家代码,比如 “RU”,“US”。 - $geoip_city_country_code3
三字符国家代码,比如 “RUS”,“USA”。 - $geoip_city_country_name
国家名称,比如 “Russian Federation”,“United States”。 - $geoip_region
国家行政区名(行政区、直辖区、州、省、联邦管辖区,诸如此类),比如 “Moscow City”,“DC”。 - $geoip_city
城市名称,比如 “Moscow”,“Washington”。 - $geoip_postal_code
邮编。 - $geoip2_continent_code
大洲代码,比如"AS" - $geoip2_data_province_name
省份名称
Nginx之IP国家代码ngx_http_geoip2_module模块简介和使用相关推荐
- python获取指定IP国家代码信息
今天在网络编程的时候发现了一个有意思的东西,可以利用http://freegeoip.net/json/ + IP的形式来查询IP来自于哪个国家,利用到 了python的urllib模块,查询之后得到 ...
- nginx利用ngx_http_geoip2_module模块对国外ip限制
1. 安装必要的环境 yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel ...
- Nginx之访问IP限制ngx_http_access_module模块简介和使用
一.ngx_http_access_module模块简介 ngx_http_access_module模块,可实现基于ip的访问控制功能,用于限制对某些客户端地址的访问,其功能类似于路由器上的基础 ...
- 用 cctld工具创建带有国家代码的IP地址表
用 cctld工具创建带有国家代码的IP地址表 cctld tools is creating IP addresses table with Country Code 项目地址 https://gi ...
- Nginx之负载均衡upstream模块简介和使用
一.upstream模块简介 Nginx的负载均衡功能依赖于ngx_http_upsteam_module模块,所支持的代理方式包括proxy_pass, fastcgi_pass, uwsgi_ ...
- Nginx安装配置(lua全模块、GEOIP、加入系统服务)---无坑安装
nginx安装一般两种:A下载安装包安装.B直接yum在线安装 A:在线安装是最简便的,如果只需基本的web和转发服务就别往下看,直接[yum install nginx],少折腾,稳定才是王道. B ...
- 避免重复造轮子,我们去哪找FPGA IP或是HDL功能模块?
一. 各FPGA厂家IP库 对于一些常用的IP,通常各FPGA厂商的开发软件中会自带,并长期免费提供给用户使用或者允许申请IP评估免费使用一段时间.这些IP通常是不开源的,但使用起来很方便,有助于开发 ...
- 利用Nginx正向代理加速代码编译外网依赖下载速度
一个典型场景: 之前在ci涉及项目代码构建过程中的依赖都需要到国外下载,例如GitHub,谷歌等等,最初的解决方案是在公司国外服务器节点上安装gitlab-runner,并registe ...
- collections模块简介
collections模块简介 除python提供的内置数据类型(int.float.str.list.tuple.dict)外,collections模块还提供了其他数据类型,使用如下功能需先导入c ...
最新文章
- 2022-2028年中国数据中台行业深度调研及投资前景预测报告(全卷)
- 【知识星球】重要小结与涨价预告
- Python图形之-tkinter与matplotlib结合案例
- 【机器视觉】 global算子
- spark streaming python实例_kafka+spark streaming代码实例(pyspark+python)
- premium导入后缀为db_Python 导入txt文件到mongo
- sql中根据逗号分隔,查出多行数据
- 使用OpenCV获取图像中某一点的像素值和修改某一点的像素值
- 解决Sublime Text 2中文显示乱码问题
- 公司简介ppt模板如何利用曲线时间轴提升质感
- amd vega56 ubuntu 下 tensorflow GPU rocm 运行情况记录及跑分
- 如何快速提高产品互动能力?
- EXCEL startup.exe 宏病毒
- stony大学计算机科学找工作,美国STEM专业毕业生薪资最高的院校有哪些?
- Kotlin重载运算符operator
- win10计算机升级系统,微软Win10升级助手
- BZOJ 1189 HNOI2007 紧急疏散evacuate
- WCF 项目应用连载[9] - 契约中的委托 事件参数处理
- 新版本微信分享sdk(1.8.3)踩坑实录
- 画了一个田径场,可以踢世界杯吗?
热门文章
- 2018普渡大学计算机科学本科,2018-2021QS美国普渡大学世界排名变化(附热门学科排名)...
- 简陋的CNN实现手写数字识别
- 学习笔记(2):华为工程师,带你实战C++视频精讲-Day1王桂林老师原创视频-C到C++类型安全增强...
- 网络:认识网络字节序
- 西门子PLC与Profinet工业以太网RFID读写器|读卡器的编程与配置说明
- CdlinuxU盘启动,小白教程,不要再看网上那些乱起八糟的了。
- 平方根(sqrt)算法
- 名词解释 —— 抄送(cc)、银弹(silver bullet)
- 如何根据英文地址查到国外的经纬度
- 新人第一次学C++的第七个项目