惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
T
Tenable Blog
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hacker News: Front Page
S
Secure Thoughts
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
MIT News - Artificial intelligence
T
Tor Project blog
S
Schneier on Security
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
D
Docker
J
Java Code Geeks
GbyAI
GbyAI
H
Heimdal Security Blog
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
T
Tailwind CSS Blog
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
The Register - Security
The Register - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 一起走过的路

MySQL查询当前连接数的语句 Apollo批量给新创建的用户授可编辑权限 物理机Jenkins接入K8s环境 ingress配置https报错certificate.lua:259: call(): failed to set DER private key: d2i_PrivateKey_bio() failed, context: ssl_certificate_by_lua* elk收集分析nginx日志,并绘制图形 Zabbix Scheduled reports中文乱码 kubernetes mysql-StatefulSet报错处理 Jenkins合并代码Git报错处理过程 nginx集群同步方案 zabbix密码复杂度有效期安全增强,符合三级等保要求。 archery关闭导出功能 自动同步bing壁纸 CentOS7更新OpenSSH8 elk监听Java日志发送微信报警 OPENVPN 同时连接多个VPN - 一起走过的路 nginx拒绝国外IP访问 keepalived健康检查及双主MySQL健康检查脚本 执迷不悟 splunk设置索引周期和索引大小
Nginx GeoIP2 模块编译安装与配置指南(附防国外访问实战)
一起走过的路 · 2026-04-08 · via 博客园 - 一起走过的路

| 手把手教你为 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

📚 补充说明