当前位置: 首页 > news >正文

nginx的GeoIP模块

使用场景

过滤指定地区/国家的IP,一般是国外IP禁止请求。
使用geoip模块实现不同国家的请求被转发到不同国家的nginx服务器,也就是根据国家负载均衡。

前置知识

GeoIP是什么?
官网地址

https://www.maxmind.com/en/home

包含IP地址的地理位置的数据库。

分为收费版本和免费版本
收费版本为GeoIP2,免费版本为GeoIPlite

nginx plus版本,也就是收费版的配置较为简单,geoip库已经被内置到yum仓库中。

nginx开源版本则没有。

实现

下载geoip模块
最新的release中作者说支持1.20版本,实测nginx1.24版本也支持

https://github.com/leev/ngx_http_geoip2_module

配置编译nginx的依赖。
我的做法是先配置nginx的官方源,
yum安装最新版stable的nginx,
nginx -V获取默认的编译参数,
然后再下载nginx对应版本的源码包进行编译替换nginx二进制文件即可。

下载geo模块到指定目录,解压,编译参数指定即可

./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_geoip_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 --add-dynamic-module=/opt/nginx/geo/ngx_http_geoip2_module-3.4 --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

注意参数--add-dynamic-module=/opt/nginx/geo/ngx_http_geoip2_module-3.4

之后执行make编译命令,编译之后nginx二进制文件会在源码包的objs目录中,验证是否编译成功

[root@localhost nginx-1.24.0]# cd objs/
[root@localhost nginx-1.24.0]#  ./nginx -V
nginx version: nginx/1.24.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
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_geoip_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 --add-dynamic-module=/opt/nginx/geo/ngx_http_geoip2_module-3.4 --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

替换掉原来的nginx文件,nginx支持不停机升级,我这里由于没有服务在运行,就直接停机替换文件升级了。

# 找到nginx的执行路径
whereis nginx
cp 备份
cp ./objs/nginx 到原有执行路径

配置文件编写

load_module /opt/nginx/nginx-1.24.0/objs/ngx_http_geoip2_module.so;# 以下内容位于http模块中server_tokens off;charset utf-8;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"''"$geoip2_country_code" $geoip2_country_name_cn';  # 日志中显示访问国家,地区access_log  /var/log/nginx/access.log  main;sendfile            on;tcp_nopush          on;tcp_nodelay         on;real_ip_header X-Forwarded-For;map $http_x_forwarded_for $realip {~^(\d+\.\d+\.\d+\.\d+) $1;default $remote_addr;}geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {  # 加载模块$geoip2_country_code source=$realip country iso_code;$geoip2_country_name_en source=$realip country names en;$geoip2_country_name_cn source=$realip country names zh-CN;}

未完,有空再写。

http://www.lryc.cn/news/235959.html

相关文章:

  • mac控制台命令小技巧
  • Postman:API测试之Postman使用完全指南
  • Flume学习笔记(3)—— Flume 自定义组件
  • go的字符切片和字符串互转
  • 所见即所得的动画效果:Animate.css
  • ERR:Navicat连接Sql Server报错
  • python算法例10 整数转换为罗马数字
  • springboot引入第三方jar包放到项目目录中,添加web.xml
  • 大数据研发工程师课前环境搭建
  • Qt图形视图框架:QGraphicsItem详解
  • defer和async
  • 数电实验-----实现74LS139芯片扩展为3-8译码器以及应用(Quartus II )
  • 洋葱架构、三层架构及两者区别
  • JavaEE进阶学习:Spring 的创建和使用
  • 音视频项目—基于FFmpeg和SDL的音视频播放器解析(十四)
  • Tomcat无法映射到activiti-app导致activiti无法启动页面
  • c语言常见的面试问题
  • image图片之间的间隙消除
  • asp.net心理健康管理系统VS开发sqlserver数据库web结构c#编程计算机网页项目
  • CnosDB有主复制演进历程
  • 【前沿学习】美国零信任架构发展现状与趋势研究
  • Toolformer论文阅读笔记(简略版)
  • Pytorch torch.dot、torch.mv、torch.mm、torch.norm的用法详解
  • Jave 定时任务:使用Timer类执行定时任务为何会发生任务阻塞?如何解决?
  • Visual Studio Code配置c/c++环境
  • 漏洞利用工具的编写
  • ChatGPT之父被OpenAI解雇
  • linux中利用fork复制进程,printf隐藏的缓冲区,写时拷贝技术,进程的逻辑地址与物理地址
  • java游戏制作-拼图游戏
  • 使用sklearn报AttributeError: ‘NoneType‘ object has no attribute ‘split‘