Nginx通過配置文件阻止海外ip訪問的方法步驟
說明
因為最近發(fā)現(xiàn)有不少刷 評論的腳本,在nginx請求日志里面看了眼,都是海外的ip,反正我的博客也是全中文。所以干脆把海外ip禁止artalk評論。
在/etc/nginx/nginx.conf中可以看到默認的日志路徑,在里面能找到每一個轉發(fā)的請求和其源IP。其中artak新增評論的請求是/api/add路徑
access_log /var/log/nginx/access.log main;
解決
APNIC介紹
后文出現(xiàn)的網站是來自APNIC (Asia Pacific Network Information Center),其是IP地址管理機構之一,負責亞洲、太平洋地區(qū)。
APNIC提供了每日更新的亞太地區(qū)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
腳本執(zhí)行后的效果如下
[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>
海外服務器請求結果也是上面這樣……然后發(fā)現(xiàn)是因為我的海外服務器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地址標明了地區(qū),我們只需要將其提取出來即可
http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
這個文件里面的內容結構如下
等級機構|獲得該IP段的國家/組織|資源類型|起始IP|IP段長度|分配日期|分配狀態(tài)
我們只需要提取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
執(zhí)行這個腳本后,會生成/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讓其定時執(zhí)行腳本并重啟nginx,我個人還是選擇人工處理了(什么時候想起來就去更新一下ip列表)
以上就是Nginx通過配置文件阻止海外ip訪問的方法步驟的詳細內容,更多關于Nginx阻止海外ip訪問的資料請關注腳本之家其它相關文章!
相關文章
在Debian11上安裝Openresty服務(Nginx+Lua)的詳細教程
OpenResty 是一個基于 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數(shù)的依賴項,這篇文章主要介紹了在Debian11上安裝Openresty服務(Nginx+Lua)?,需要的朋友可以參考下2022-10-10

