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
-
执行时可能会报错,主要原因通常是缺少依赖,具体的可以根据报错在网络上找到解决方法。
-
主要加入这一段“--add-module=/usr/local/ngx_http_geoip2_module”,参考GitHub上的ngx_http_geoip2_module。
-
原先的“--with-stream=dynamic” 改为“--with-stream”,否则编译也是会报错的。
-
获取maxmind的GeoLite2 Country包
-
需要自行注册一个账号,找到下图对应的路径下载即可。
-
下载后上传到服务器,使用
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; } …… }
- http 块
mmdb命令行工具
- 借助此工具可基于.mmdb查询对应ip的地理信息,可在整个调试过程中起到一个辅助查错的作用,后续调整nginx策略时也需要用到,可以明确需要哪些字段。
- 命令使用,举例如下:
mmdblookup --file /etc/nginx/geoip/GeoLite2-Country/GeoLite2-Country.mmdb --ip $ip
本文来自博客园,作者:ヾ(o◕∀◕)ノヾ,转载请注明原文链接:https://www.cnblogs.com/Jupiter-blog/p/16849629.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构