Nginx通过配置文件阻止海外ip访问的方法步骤

 更新时间:2025年07月02日 10:08:54   作者:慕雪华年  
这篇文章主要介绍了Nginx通过配置文件阻止海外ip访问的方法步骤,为防止海外IP刷评论,用户通过Nginx配置屏蔽海外IP,利用APNIC数据提取国内IP并生成deny规则,测试后确认有效,可手动或定时更新配置以保持防护,需要的朋友可以参考下

说明

因为最近发现有不少刷 评论的脚本,在nginx请求日志里面看了眼,都是海外的ip,反正我的博客也是全中文。所以干脆把海外ip禁止artalk评论。

/etc/nginx/nginx.conf中可以看到默认的日志路径,在里面能找到每一个转发的请求和其源IP。其中artak新增评论的请求是/api/add路径

access_log  /var/log/nginx/access.log  main;

解决

APNIC介绍

后文出现的网站是来自APNIC (Asia Pacific Network Information Center),其是IP地址管理机构之一,负责亚洲、太平洋地区。

APNIC提供了每日更新的亚太地区IPv4,IPv6,AS号分配的信息表:
http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
该文件的格式与具体内容参见:
http://ftp.apnic.net/pub/apnic/stats/apnic/README.TXT

脚本获取ip

可以下载所有海外ip列表并生成一个nginx配置,写入/etc/nginx/blackip.conf

#!/bin/bash
rm -f legacy-apnic-latest black_`date +%F`.conf && wget http://ftp.apnic.net/apnic/stats/apnic/legacy-apnic-latest

awk -F '|' '{if(NR>2)printf("%s %s/%d%s\n","deny",$4,24,";")}' legacy-apnic-latest > black_`date +%F`.conf && \
rm -f /etc/nginx/blackip.conf && \
ln -s $PWD/black_`date +%F`.conf /etc/nginx/blackip.conf

脚本执行后的效果如下

[root@bt-7274:/etc/nginx]# ll
total 88
lrwxrwxrwx 1 root root   34 Dec  9 16:03 blackip.conf -> /root/docker/black_2023-12-09.conf

文件内容如下

[root@bt-7274:/etc/nginx/conf.d]# cat ../blackip.conf
deny 128.134.0.0/24;
deny 128.184.0.0/24;
deny 128.250.0.0/24;
deny 129.60.0.0/24;
deny 129.78.0.0/24;
...后面的省略了

nginx屏蔽海外ip

你可以将这个blackip.conf/etc/nginx/nginx.conf中的http模块里面include,这样会阻止当前服务器所有反代的海外的请求。

include /etc/nginx/blackip.conf;

还可以在单个配置文件的location里面引用

    location / {
        proxy_redirect off; # artalk的nginx配置中必须有这个
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header Upgrade-Insecure-Requests 1;
        proxy_set_header X-Forwarded-Proto https;

        include /etc/nginx/blackip.conf; # 引用配置

        proxy_pass http://127.0.0.1:14722;
  }

修改后重启nginx,没有报错就是ok了

systemctl restart nginx

用海外的服务器试试能不能请求artalk,用Artalk这个管理员登录页面来进行测试。

国内服务器请求结果如下,和浏览器打开的结果基本是一样(管理员登录界面)

[root@bt-7274:/etc/nginx/conf.d]# curl https://artk.musnow.top/sidebar/#/login
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Artalk Sidebar</title>
    <script type="module" crossorigin src="./assets/index-5a0b3a93.js"></script>
    <link rel="stylesheet" href="./assets/index-84fdcf98.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
  </head>
  <body>
    <div id="app"></div>

  </body>
</html>

海外服务器请求结果也是上面这样……然后发现是因为我的海外服务器ip压根不在那个black的deny列表里面

尝试把ip的网段给加进去,重启nginx再试试。完美处理!添加前能正常请求到,添加后就变成403了

[root@RainYun-8aNbbsmA:~]# curl https://artk.musnow.top/sidebar/#/login
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Artalk Sidebar</title>
    <script type="module" crossorigin src="./assets/index-5a0b3a93.js"></script>
    <link rel="stylesheet" href="./assets/index-84fdcf98.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
  </head>
  <body>
    <div id="app"></div>

  </body>
</html>
[root@RainYun-8aNbbsmA:~]#
[root@RainYun-8aNbbsmA:~]# curl https://artk.musnow.top/sidebar/#/login
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@RainYun-8aNbbsmA:~]#

nginx屏蔽非国内ip

我前文提到了我的海外服务器的ip不在这个deny的ip列表里面,没有被屏蔽。

考虑到网上搜不到legacy-apnic-latest文件存放的是什么ip的信息,我决定换一个思路:allow国内的ip,拒绝所有非国内的ip

下面这个url里面的ip地址标明了地区,我们只需要将其提取出来即可

http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest

这个文件里面的内容结构如下

等级机构|获得该IP段的国家/组织|资源类型|起始IP|IP段长度|分配日期|分配状态

我们只需要提取CN的所有IP,然后允许他们,再deny all阻止其他ip就可以了

#!/bin/bash
rm -f delegated-apnic-latest blackcn_`date +%F`.conf && wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest

awk -F\| '/CN\|ipv4/ { printf("%s %s/%d%s\n","allow",$4, 32-log($5)/log(2), ";") }' delegated-apnic-latest > blackcn_`date +%F`.conf && \
rm -f /etc/nginx/blackcn.conf && \
ln -s $PWD/blackcn_`date +%F`.conf /etc/nginx/blackcn.conf

执行这个脚本后,会生成/etc/nginx/blackcn.conf文件

[root@bt-7274:/etc/nginx]# ll
total 88
lrwxrwxrwx 1 root root   42 Dec  9 16:54 blackcn.conf -> /root/docker/nginx/blackcn_2023-12-09.conf
lrwxrwxrwx 1 root root   40 Dec  9 16:56 blackip.conf -> /root/docker/nginx/black_2023-12-09.conf

内容如下

allow 223.248.0.0/14;
allow 223.252.128.0/17;
allow 223.254.0.0/16;
allow 223.255.0.0/17;
allow 223.255.236.0/22;
allow 223.255.252.0/23;
....

还是修改nginx单个站点配置文件的location中的内容

    location / {
        proxy_redirect off;
    	proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header Upgrade-Insecure-Requests 1;
        proxy_set_header X-Forwarded-Proto https;
		# 允许所有国内ip
    	include /etc/nginx/blackcn.conf;
    	deny all; # 阻止其他ip

    	proxy_pass http://127.0.0.1:14722;
        }

先来试试不修改配置文件(不做任何deny和allow操作的情况下)海外ip请求结果

[root@RainYun-8aNbbsmA:~]# curl https://artk.musnow.top/sidebar/#/login
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Artalk Sidebar</title>
    <script type="module" crossorigin src="./assets/index-5a0b3a93.js"></script>
    <link rel="stylesheet" href="./assets/index-84fdcf98.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
  </head>
  <body>
    <div id="app"></div>

  </body>
</html>

符合预期,正常请求出了登录页面的html文件。

添加如上修改后,重启nginx,再次进行测试。这一次已经403阻止了,完美!

[root@RainYun-8aNbbsmA:~]# curl https://artk.musnow.top/sidebar/#/login
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

The end

你可以写个crontab让其定时执行脚本并重启nginx,我个人还是选择人工处理了(什么时候想起来就去更新一下ip列表)

以上就是Nginx通过配置文件阻止海外ip访问的方法步骤的详细内容,更多关于Nginx阻止海外ip访问的资料请关注脚本之家其它相关文章!

相关文章

  • nginx通过https部署vue项目的完整步骤

    nginx通过https部署vue项目的完整步骤

    在实际开发中,我们会以https形式进行页面访问,下面这篇文章主要给大家介绍了关于nginx通过https部署vue项目的完整步骤,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • 关于Nginx动静分离详解以及配置

    关于Nginx动静分离详解以及配置

    这篇文章主要介绍了关于Nginx动静分离详解以及配置,动静分离是通过中间件将动态请求和静态请求进行分离,分离资源,减少不必要的请求消耗,减少请求延时,需要的朋友可以参考下
    2023-04-04
  • Nginx(PHP/fastcgi)的PATH_INFO问题

    Nginx(PHP/fastcgi)的PATH_INFO问题

    PATH_INFO是一个CGI 1.1的标准,经常用来做为传参载体. 比如, 我们可以使用PATH_INFO来代替Rewrite来实现伪静态页面, 另外不少PHP框架也使用PATH_INFO来作为路由载体.
    2011-08-08
  • 浅谈Nginx七层反向代理和负载均衡

    浅谈Nginx七层反向代理和负载均衡

    这篇文章主要介绍了浅谈Nginx七层反向代理和负载均衡,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Nginx基本运行原理解析

    Nginx基本运行原理解析

    这篇文章给大家介绍Nginx基本运行原理解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2026-05-05
  • Nginx部署前端静态文件指南分享(基于虚拟机环境)

    Nginx部署前端静态文件指南分享(基于虚拟机环境)

    本笔记详细介绍了如何使用Nginx部署前端静态文件,包括环境准备、文件传输、配置文件编写、自定义配置加载、验证配置和日志查看等步骤,通过实践操作,用户可以掌握用Nginx快速部署任意前端静态文件的核心技能
    2026-02-02
  • 记一次nginx配置不当引发的499与failover 机制失效问题

    记一次nginx配置不当引发的499与failover 机制失效问题

    近期在非高峰期也存在499超过告警阈值的偶发情况,多的时候一天几次,少的时候则几天一次,持续一般也就数分钟,经过和小伙伴的共同探究,最后发现之前对于499是客户端主动断开因而和服务端关系不大的想当然认知是错误的,这里记录一下
    2023-05-05
  • linux下安装Nginx1.16.0的教程详解

    linux下安装Nginx1.16.0的教程详解

    因为最近在倒腾linux,想安装新版本的nginx,找了一圈教程没有找到对应的教程,在稍微倒腾了一会之后终于成功的安装了最新版。这篇文章主要介绍了linux下安装Nginx1.16.0 ,需要的朋友可以参考下
    2019-06-06
  • Nginx同一个域名配置多个项目的实现方法

    Nginx同一个域名配置多个项目的实现方法

    这篇文章主要介绍了Nginx同一个域名配置多个项目的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • nginx 负载均衡轮询方式配置详解

    nginx 负载均衡轮询方式配置详解

    负载均衡(load-balance)就是将负载分摊到多个操作单元上执行,从而提高服务的可用性和响应速度,带给用户更好的体验,本文给大家介绍nginx 负载均衡轮询方式配置,感兴趣的朋友一起看看吧
    2022-03-03

最新评论