业界动态
Nginx之IP国家代码ngx_http_geoip2_module模块简介和使用
2024-11-14 21:43

  ngx_http_geoip2_module模块使用预编译的MaxMind数据库创建变量,其值取决于客户端 IP 地址 ,通过此模块我们可以精准的限制或者允许某个国家的IP地址访问WEB站点。MaxMind的GeoIP2和GeoLite2 IP智能产品和服务用于发现有关特定IP地址的信息。我们提供免费和付费网络服务、基于订阅的可下载数据库和免费可下载数据库。我们学习使用免费版本的就可以,当然付费版本的更新更及时,内容更详细(包括国家、城市、经纬度、运营商、甚至企业信息)。

Nginx之IP国家代码ngx_http_geoip2_module模块简介和使用

[root@s141 local]# wget https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz

[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]#

[root@s141 libmaxminddb-1.6.0]# https://blog.csdn.net/carefree2005/article/details/configure [root@s141 libmaxminddb-1.6.0]# make [root@s141 libmaxminddb-1.6.0]# make install

[root@s141 local]# echo ‘/usr/local/lib’ > /etc/ld.so.conf.d/geoip.conf [root@s141 local]# ldconfig

[root@s141 local]# wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/heads/master.zip

[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

[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

[root@s141 opt]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

[root@s141 nginx-1.20.1]# https://blog.csdn.net/carefree2005/article/details/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

[root@s141 nginx-1.20.1]# cp -f objs/nginx /usr/sbin/nginx cp: overwrite ‘/usr/sbin/nginx’? y

[root@s141 nginx-1.20.1]# systemctl stop nginx [root@s141 nginx-1.20.1]# systemctl start nginx

  访问官网下载免费版本的GeoIP2库,我们下载压缩包,里面是二进制mmdb格式的库文件。下载链接,需要注册登录下载。如果无法登录官网可以通过CSDN下载,CSDN下载地址。 在这里插入图片描述 在这里插入图片描述

将二进制库文件上传到/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

[root@s141 GeoIP]# cd /etc/nginx/ [root@s141 nginx]# vim nginx.conf

在http模块中加入如下内容

 

日志格式配置中加入国家代码 在这里插入图片描述

使用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

 

(base) [root@sun-site nginx]# nginx -s reload

在这里插入图片描述

[root@jiujiu ~]# curl https://download.sun-site.com/myip 152.32.150.199 United States NA

添加如下配置在http块内

 

黑名单国家地址访问URL返回403 在这里插入图片描述 其他国家访问返回正常信息 在这里插入图片描述

  • 原因:libmaxminddb动态库未加载
  • 解决方案: #加载libmaxminddb动态库 #echo ‘/usr/local/lib’ > /etc/ld.so.conf.d/geoip.conf #ldconfig
最新文章
【tr1.4】泰拉瑞亚最详尽的钓鱼攻略1.4版 金鱼杆不是梦!泰拉瑞亚手机合成表「【tr1.4】泰拉瑞亚最详尽的钓鱼攻略1.4版 金鱼杆不是梦!」
(前排提醒 本文内容来自泰拉瑞亚wiki 如有错误 欢迎指出)hello大家好,我是赛奇好久不见,不知道多久没写专栏了 之前一直说的
『轻小说分享』带着智慧型手机闯荡异世界智慧型手机「『轻小说分享』带着智慧型手机闯荡异世界」
『轻小说』带着智慧型手机闯荡异世界 第二季动画来袭!剧情简介       手拿智慧型手机,在异世界展开的温馨冒险故事!  因
各大媒体优劣对比_手机屏幕防忽悠指南:LCD与OLED屏幕优劣势分析手机媒体「各大媒体优劣对比_手机屏幕防忽悠指南:LCD与OLED屏幕优劣势分析」
“买手机当然要买带OLED屏幕的手机,显示清楚、颜色好看。LCD屏幕早已是淘汰技术,没必要买。”如果你常去线下门店
B&O铂傲旗舰头戴降噪耳机Beoplay H100开售,全新模块化设计模块化手机「B&O铂傲旗舰头戴降噪耳机Beoplay H100开售,全新模块化设计」
9月3日,我爱音频网获悉,来自丹麦的世界知名视听品牌BO铂傲,其全新旗舰款头戴式耳机Beoplay H100正式上市。BO铂傲Beoplay H100
Scratch for Windowsscratch手机版下载「Scratch for Windows」
MIT Scratch is a free application designed to help kids learn basic concepts of programming using visual tools. Designed
模块化手机!HMD推出配件可随心定制的手机模块手机「模块化手机!HMD推出配件可随心定制的手机」
朋友们,你是否曾想过拥有一部完全按照自己需求定制的手机?一部可以随意更换配件,适应各种使用场景的设备?今天,我要和你分享
促消费!开封有大动作!
一、延期“开封有礼 惠享全城”消费券促消费活动开封市于2025年3月份在全市范围内围绕零售、餐饮领域共投放了144000张消费券,为
洪璟:“山中腊梅”曹诚英
1902年,农历正月二十六,年刚刚过完,古徽州绩溪县(今属安徽宣城)一个叫作旺川的小村落,年的喜庆还未完全散净,村中富贾曹耆