配合nginx禁止一些烦人的UA和ip

1696天前 · 分享 · 1082次阅读

前一篇说到很多乱七八糟的访问记录,其中SemrushBot最多,UA也是怪怪的,于是禁止之(
不过好像是含有这个字段的都会被禁止,某个特定UA没仔细看,需要的时候再说hhh
在nginx.conf同一个目录新建一个UA_deny.conf文件
文件内容如下

if ($http_user_agent ~ "SemrushBot"){
     return 403;
}

然后再特定的server{}中添加一句

include UA_deny.conf;

用curl测试返回结果如下

curl -A "Mozilla/5.0 (compatible; SemrushBot/6~bl; +http://www.semrush.com/bot.html)" https://blog.weimo.info
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>

由于网站加了cdn,禁止ip需要禁的是用户真实ip而不是cdn的ip,所以得做一些处理。
在nginx.conf中加入下面的部分

map $HTTP_CF_CONNECTING_IP $clientRealIp 
{
    "" $remote_addr;
    ~^(?P<firstAddr>[a-z0-9.:]+),?.*$ $firstAddr;
}

然后还是里面,当然也可以另外新建一个文件,这里我就偷个懒了。。(嗯,没错,5.188.84.*这个段一直发垃圾评论)

if ($clientRealIp ~ "5.188.84."){
     return 403;
}
👍 0

none

最后修改于1695天前

评论

贴吧 狗头 原神 小黄脸
收起

贴吧

狗头

原神

小黄脸

目录

avatar

未末

迷失

126

文章数

275

评论数

7

分类