Nginx GeoIP2 模块编译安装与配置指南(附防国外访问实战)
| 手把手教你为 Nginx 添加 GeoIP2 模块,实现基于地理位置的访问控制 🛡️
📌 前言
在某些场景下,我们需要限制某些国家/地区的 IP 访问网站(例如仅允许国内访问)。Nginx 官方已经停止维护 geoip 模块(基于旧版 GeoIP),新版推荐使用 GeoIP2 模块。
本文将详细介绍如何从源码编译 Nginx,动态加载 ngx_http_geoip2_module,并配置基于国家的访问控制 🚧
🧰 环境准备
# 创建工具目录 mkdir /root/tools/ cd /root/tools/ # 安装依赖库 yum install libmaxminddb libmaxminddb-devel -y
📥 下载源码包
# 下载 Nginx 源码 wget https://nginx.org/download/nginx-1.26.3.tar.gz # 下载 GeoIP2 模块源码 wget https://ghp.arslantu.xyz/https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.zip # 解压并清理 unzip 3.4.zip && rm -f 3.4.zip tar xfz nginx-1.26.3.tar.gz
🔧 编译配置(含动态模块)
cd nginx-1.26.3 ./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-openssl=/opt/tools/openssl-1.1.1w \ --with-zlib=/opt/tools/zlib-1.2.11 \ --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 \ --add-module=/opt/tools/nginx_upstream_check_module-master/ \ --add-module=/opt/modsecurity-nginx \ --add-dynamic-module=/root/tools/ngx_http_geoip2_module-3.4 make
🔄 替换 Nginx 二进制文件
# 备份旧版本(以日期命名) mv /usr/sbin/nginx /usr/sbin/nginx_20260408 # 复制新编译的 Nginx cp objs/nginx /usr/sbin/ # 复制动态模块到标准目录 cp objs/ngx_http_geoip2_module.so /usr/lib64/nginx/modules/ # 验证版本及模块 nginx -V
📦 下载 GeoIP2 数据库
cd /etc/nginx/common/ wget https://ghp.arslantu.xyz/https://github.com/P3TERX/GeoLite.mmdb/releases/download/2026.04.07/GeoLite2-City.mmdb
⚙️ Nginx 配置修改
1️⃣ 加载动态模块(nginx.conf 顶部)
load_module modules/ngx_http_geoip2_module.so;
2️⃣ 添加 GeoIP2 配置(http 块内)
include common/geolite2.conf;
3️⃣ 创建访问控制文件
cat > /etc/nginx/common/geolite2.conf << 'EOF'
geoip2 common/GeoLite2-City.mmdb {
auto_reload 5m;
$geoip2_country country iso_code;
}
geo $is_internal {
default 0;
127.0.0.1 1;
43.254.89.5 1;
10.128.0.0/24 1;
10.0.2.0/23 1;
}
map "$is_internal:$geoip2_country" $block_foreign {
"1:" 0;
"0:CN" 0;
default 1;
}
EOF
cat > /etc/nginx/common/block_geoip.ini << 'EOF'
if ($block_foreign) {
return 403;
}
EOF
4️⃣ 在主配置中引入(server 块内)
include /etc/nginx/common/block_geoip.conf;
✅ 测试并重载配置
nginx -t # 测试配置是否正确
nginx -s reload # 平滑重载
🧪 验证效果
-
✅ 国内 IP 访问 → 正常
-
🚫 国外 IP 访问 → 返回
403 Forbidden
📚 补充说明
| 项目 | 说明 |
|---|---|
| 🔁 自动更新 | auto_reload 5m 每 5 分钟检查数据库更新 |
| 🧩 动态模块 | 无需重新编译即可加载 |
| 🗺️ 数据库来源 | GeoLite2 免费版,建议定期更新 |
| ⚠️ 注意 | if 在 location 中使用时需谨慎,推荐结合 return 使用 |
🎯 结语
通过以上步骤,你已经成功为 Nginx 添加了 GeoIP2 支持,并实现了基于国家代码的访问控制。
这种方式性能好、配置灵活,非常适合有地域限制需求的业务场景 🌐
📌 如果觉得有用,欢迎收藏 / 转发~
🐛 遇到问题?欢迎在评论区留言交流!

浙公网安备 33010602011771号