业界动态
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
最新文章
英特尔CEO陈立武:剥离非核心业务,建立世界一流晶圆代工厂!
当地时间3月31日,英特尔在美国拉斯维加斯召开了“Intel Vision”(英特尔愿景)大会,英特尔首席执行官陈立武(Lip-Bu Tan)首
合金弹头游戏合金弹头手机版下载「合金弹头游戏」
相信很多小伙伴们比较喜欢合金弹头游戏吧,那么下面给大家带来2022和合金弹头画风很像的游戏叫什么,这几款游戏给小编带来的整体
广东:商贩偷奸耍滑,缺斤短两,男子气不过,拿锤子怒砸猪肉摊锤子手机老板「广东:商贩偷奸耍滑,缺斤短两,男子气不过,拿锤子怒砸猪肉摊」
广东佛山曾经发生一起事件,男子乘车到菜市场买猪肉,猪肉摊老板告知男子:猪肉36元一斤。男子嫌猪肉太贵,与老板发生冲突,并抢
小米手机会出现漏电和手麻问题?手机漏电「小米手机会出现漏电和手麻问题?」
小米手机漏电和手麻问题解析引言:近期,关于小米手机存在漏电和手麻问题的传闻在社交媒体上广泛流传。用户对于手机安全和健康的
SIM卡成本价1.5-5元 有营业厅补换卡收费30元补手机卡要多少钱「SIM卡成本价1.5-5元 有营业厅补换卡收费30元」
  手机里必需的SIM卡(电话卡)难免出现损坏、丢失的情况。  近日,南都湾财社记者在社交平台上发现,不少用户吐槽运营商补换
【诊疗攻略】吉兰-巴雷综合征诊断+治疗
吉兰-巴雷综合征(Guillain⁃Barré syndrome,GBS)系一类免疫介导的急性炎性周围神经病,包括经典型GBS和变异型 GBS 两大类,
专题 | 2025年地方供地计划探析:规模压降、结构优化与市场效应
2025年初,中国房地产市场延续上年四季度企稳态势,一二手房成交规模与房价指数持续修复,土地市场率先显现积极信号——月度溢价
iPhone9什么时候发售 新iPhone发布时间及上市时间一览苹果手机9「iPhone9什么时候发售 新iPhone发布时间及上市时间一览」
马上就到九月份了,根据往年惯例,全球最受欢迎的手机品牌,苹果将在九月中旬发布2018年度旗舰新iPhone,因此当前关于新iPhone的
QQ支持与TM双向切换吗手机qq下载2012「QQ支持与TM双向切换吗」
  TM是一款面向办公环境中有IM使用需求的用户,较侧重熟人间的沟通和联系的个人即时通讯软件。可与QQ一键双向切换。  在QQ中
这些原研药,在新渠道爆红
文 | vb动脉网随着医改各项措施的全面推进,院内院外药品市场格局重塑,原研药战略重心正从院内市场走向院外。据米内网近期发布