目录
- 利用Nginx屏蔽指定IP及海外IP访问方案
- 第一步:部署geoip2扩展依赖环境
- 第二步:获取ngx_http_geoip2_module源码模块
- 第三步:将模块解压至指定工作目录
- 第四步:重新编译安装Nginx以集成模块
- 第五步:获取最新的IP地址归属地数据库文件
- 第六步:编辑并应用Nginx配置文件
- 第七步:模拟不同地域IP进行功能验证
- 总结
利用Nginx屏蔽指定IP及海外IP访问方案

实现针对特定IP或国外IP的访问限制有多种途径,本文将重点阐述基于Nginx及其ngx_http_geoip2模块的具体实施方法,以达到禁止海外IP访问网站的目的。
第一步:部署geoip2扩展依赖环境
[root@fxkj ~]# yum install libmaxminddb-devel -y
第二步:获取ngx_http_geoip2_module源码模块
[root@fxkj tmp]# git clone https://github.com/leev/ngx_http_geoip2_module.git
[ro tmp]#
第三步:将模块解压至指定工作目录
本例中将模块解压存放于/usr/local目录下,以便后续编译引用。
[root@fxkj tmp]# mv ngx_http_geoip2_module/ /usr/local/
[root@fxkj local]# ll ngx_http_geoip2_module/
total 60
-rw-r--r-- 1 root root 1199 Aug 13 17:20 config
-rw-r--r-- 1 root root 1311 Aug 13 17:20 LICENSE
-rw-r--r-- 1 root root 23525 Aug 13 17:20 ngx_http_geoip2_module.c
-rw-r--r-- 1 root root 21029 Aug 13 17:20 ngx_stream_geoip2_module.c
-rw-r--r-- 1 root root 3640 Aug 13 17:20 README.md

第四步:重新编译安装Nginx以集成模块
需明确当前运行环境,原Nginx版本为1.16。经查阅资料,ngx_http_geoip2模块要求Nginx版本至少为1.18或以上。因此,本次操作涉及将Nginx升级至1.18版本,并在编译过程中动态添加ngx_http_geoip2模块。
下载Nginx 1.18版本的源代码包。
[root@fxkj ~]# yum install libmaxminddb-devel -y
解压Nginx 1.18软件包,进入源码目录进行配置与编译,旨在升级至Nginx 1.18并集成ngx_http_geoip2模块。
关键注意事项:
- 在升级Nginx或添加新模块时,仅需执行编译(configure)和生成二进制文件(make)步骤,切勿执行make install,否则会导致线上现有的Nginx服务被新版本完全覆盖替换。
- 在执行编译前,务必检查当前Nginx已安装的模块列表,以确保在新编译中保留原有功能。
[root@fxkj tmp]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20260623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2026
TLS SNI support enabled
configure arguments: –with-http_stub_status_module –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_ssl_module –with-stream
执行编译安装流程。
[root@fxkj tmp]# tar -xf nginx-1.18.0.tar.gz
[root@fxkj tmp]# cd nginx-1.18.0/
[root@fxkj nginx-1.18.0]# ./configure --with-http_stub_status_module \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx --with-http_ssl_module --with-stream \
--add-module=/usr/local/ngx_http_geoip2_module
[root@fxkj nginx-1.18.0]# make
[root@fxkj nginx-1.18.0]# cp /usr/loca/nginx/sbin/nginx /usr/loca/nginx/sbin/nginx1.16 #备份
[root@fxkj nginx-1.18.0]# cp objs/nginx /usr/local/nginx/sbin/ #用新的去覆盖旧的
[root@fxkj nginx-1.18.0]# pkill nginx #杀死nginx
[root@fxkj nginx-1.18.0]# /usr/local/nginx/sbin/nginx #再次启动Nginx
查看Nginx版本号以及已加载的模块列表,确认升级及模块添加是否成功。
[root@fxkj nginx-1.18.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20260623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2026
TLS SNI support enabled
configure arguments: –with-http_stub_status_module –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_ssl_module –with-stream –add-module=/usr/local/ngx_http_geoip2_module
第五步:获取最新的IP地址归属地数据库文件
模块安装成功后,需在Nginx配置中指定IP数据库路径。在安装运行库时,默认会生成两个数据库文件,位于/usr/share/GeoIP/目录下,其中一个是仅支持IPv4的版本,另一个则同时包含IPv4和IPv6数据。
访问www.maxmind.com官网,注册并登录账户后,下载最新的库文件。(账户注册过程此处省略)在左侧导航栏点击Download Files:

选择GeoLite2 Country选项,点击Download GZIP按钮即可下载压缩文件:

将下载的文件上传至/usr/share/GeoIP/目录下并进行解压处理。
[root@fxkj local]# cd /usr/share/GeoIP/
[root@fxkj GeoIP]# ll
total 69612
lrwxrwxrwx. 1 root root 17 Mar 7 2026 GeoIP.dat -> GeoIP-initial.dat
-rw-r--r--. 1 root root 1242574 Oct 30 2026 GeoIP-initial.dat
lrwxrwxrwx. 1 root root 19 Mar 7 2026 GeoIPv6.dat -> GeoIPv6-initial.dat
-rw-r--r--. 1 root root 2322773 Oct 30 2026 GeoIPv6-initial.dat
-rw-r--r-- 1 root root 3981623 Aug 12 02:37 GeoLite2-Country.mmdb
第六步:编辑并应用Nginx配置文件
在进行任何修改之前,先对现有配置文件进行备份,以防误操作导致服务异常。
[root@fxkj ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf-bak
[root@fxkj ~]# vim /usr/local/nginx/conf/nginx.conf
在http配置块中添加若干行指令,用于定义IP数据库文件的具体位置。
geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
auto_reload 5m;
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default yes;
CN no;
}

在server块的location配置下添加判断条件,若识别到客户端IP属于国外IP,则执行相应的return动作。此处定义了三种策略,其中两种已被注释掉。
当检测到访问IP为国外IP时,直接返回404状态码。
if ($allowed_country = yes) {
# return https://www.baidu.com;
# return /home/japan;
return 404;
}

修改完成后,首先检测配置文件语法是否正确,随后重新加载Nginx配置使更改生效。
[root@fxkj ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[roo@fxkj ~]# /usr/local/nginx/sbin/nginx -s reload
第七步:模拟不同地域IP进行功能验证
使用位于海外的节点服务器发起网站访问请求,本例中使用的测试IP来源于韩国。

观察结果,访问网站时返回“404 Not Found”错误页面,表明屏蔽策略已生效。

进一步检查Nginx的访问日志,确认相关拦截记录。
“13.125.1.194 – – [14/Aug/2026:16:15:51 +0800] “GET /favicon.ico HTTP/1.1” 404 548 “https://www.fxkjnj.com/” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36”

至此,通过Nginx实现禁止国外IP访问网站的功能配置与验证已全部完成。

总结
关于利用Nginx禁止指定IP及国外IP访问网站的教程到此结束。如需了解更多nginx禁止国外IP访问网站的相关内容,请搜索本站历史文章或继续浏览下方推荐内容。感谢大家的支持!