Nginx----GeoIP2模块使用

本机环境

  • CentOS Linux release 7.9

安装libmaxminddb-devel

yum install libmaxminddb-devel -y

拉取geoip2的模块文件

cd /usr/local/
git clone https://github.com/leev/ngx_http_geoip2_module.git

下载Nginx包

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxf nginx-1.20.1.tar.gz
cd nginx-1.20.1

编译Nginx执行文件

  • 因为事先本机用的是yum安装的nginx1.20.1,所以借鉴原先编译的模块,再做些许调整即可
    可用nginx -V查看原先编译了哪些模块

  • 编译命令如下:

    ./configure --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --add-module=/usr/local/ngx_http_geoip2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
    make
    make install
    
  • ./configure

    1. 执行时可能会报错,主要原因通常是缺少依赖,具体的可以根据报错在网络上找到解决方法。

    2. 主要加入这一段“--add-module=/usr/local/ngx_http_geoip2_module”,参考GitHub上的ngx_http_geoip2_module

    3. 原先的“--with-stream=dynamic” 改为“--with-stream”,否则编译也是会报错的。

获取maxmind的GeoLite2 Country包

  • 官网:https://www.maxmind.com/

  • 需要自行注册一个账号,找到下图对应的路径下载即可。
    image

  • 下载后上传到服务器,使用tar 命令解压,后续nginx配置文件中会引用到其中的数据库文件。

Nginx配置

  • 举个例子,除CN以外国家的ip不允许访问本站点
    • http 块
      http{
      ……
      	# 获取来源真实IP
      	map $http_x_forwarded_for  $clientRealIp {
      		"" $remote_addr;
      		~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
      	}
      	# 根据来源真实IP,调用geoip2,得到ip的来源国家
      	geoip2 /etc/nginx/geoip/GeoLite2-Country/GeoLite2-Country.mmdb {
      		auto_reload 5m;
      		$geoip2_metadata_country_build metadata build_epoch;
      		$geoip2_data_country_code default=US source=$clientRealIp country iso_code;
      		$geoip2_data_country_name country names en;
      	}
      	# 根据ip的来源国家,做匹配
      	map $geoip2_data_country_code $allowed_country {
      		CN yes;
      		default no;
      	}
      ……
      }
      
    • location 块
      location / {
      	if ($allowed_country = no) {
      		return 404;
      	}
      	……
      }
      

mmdb命令行工具

  • 借助此工具可基于.mmdb查询对应ip的地理信息,可在整个调试过程中起到一个辅助查错的作用,后续调整nginx策略时也需要用到,可以明确需要哪些字段。
  • 命令使用,举例如下:
    mmdblookup  --file /etc/nginx/geoip/GeoLite2-Country/GeoLite2-Country.mmdb  --ip $ip
    
posted @   ヾ(o◕∀◕)ノヾ  阅读(252)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示