业界动态
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
最新文章
明确0到2级辅助驾驶系统 “人机共驾”法律属性
公安部交通管理局局长王强7月23日在国新办举行的“高质量完成‘十四五’规划”系列主题新闻发布会上表示,为进一步加强规范管理
iPhone 17系列价格曝光:仅一款不涨价 Pro版更具性价比
【TechWeb】近段时间以来,全新iPhone 17系列尤为吸引外界的目光,而根据供应链最新爆料,iPhone 17系列距离亮相仅剩差不多一个
管庄回收茅台酒!管庄茅台酒回收!
137=1888=0048 回收茅台礼盒15年 30年 50年 80年茅台礼盒回收洋酒系列 回收路易十三,轩尼诗,拉菲,拉图,马爹利,人头马
北京到陶乐物流专线价格,货到付款
长胜物流24 小时服/务热线:||长途搬家|家电托运|电脑托运|液晶电视托运|冰箱托运|洗衣机托运|行李托运|电动车托运|摩托车托运|
解读Solana最新技术路线图:锚定“互联网资本市场”,打造链上华尔街
昨天 Solana 宣布了新的路线图。本质上,由于各链改进都已进入深水区,导致确实有些名词堆砌的情况,我试着用大家可以理解的方式
国泰海通发布新一代全AI智能APP灵犀
央广网北京7月26日消息(记者樊瑞)当前,人工智能技术正加速重构证券行业服务业态,加快发展新一代人工智能是推动证券行业高质
新加坡地道特色平价美食,好吃不踩雷
新加坡太多美食了~亚坤-咖椰吐司$5左右,这是我在新加坡吃的第一顿早餐。不知道好不好吃,但看着一大早就好多人排队,那就没错
上海UPS蓄电池回收
上海UPS回收,UPS电源回收,UPS电池回收:上海APC不间断电源回收,UPS主机回收,机房设备回收,回收UPS设备,UPS蓄电池回收,UPS
云边端协同:虚拟电厂的“神经网”革命
上海某高校586台空调在50秒内无感降负300kW,精度96.57%;重庆虚拟电厂调度17MW可调资源,延迟压缩至100ms级——这组数据的背后
高金食品与远方好物达成战略合作,让高品质“真黑猪”走进千家万户的餐桌
中国黑猪看四川,四川黑猪看高金。高金食品正用“真黑猪”,破局本土黑猪市场占有率困境,开创中国黑猪产业新篇章。7月23日,在