说明
因为最近发现有不少刷 评论的脚本,在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访问的资料请关注代码网其它相关文章!
发表评论