

























| 手把手教你为 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
# 备份旧版本(以日期命名) 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
cd /etc/nginx/common/ wget https://ghp.arslantu.xyz/https://github.com/P3TERX/GeoLite.mmdb/releases/download/2026.04.07/GeoLite2-City.mmdb
nginx.conf 顶部)load_module modules/ngx_http_geoip2_module.so;
http 块内)include common/geolite2.conf;
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
server 块内)include /etc/nginx/common/block_geoip.conf;
nginx -t # 测试配置是否正确
nginx -s reload # 平滑重载
✅ 国内 IP 访问 → 正常
🚫 国外 IP 访问 → 返回 403 Forbidden
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。