Nginx通过UA名称屏蔽访问

位置:首页>文章>详情   分类: 教程分享 > nginx教程   阅读(3487)   2023-03-28 11:29:14

1.新增nginx屏蔽配置文件

屏蔽恶意蜘蛛

文件暂时为
/etc/nginx/conf.d/deny_ua.config
说明:文件名deny_ua.config,后缀为.config非.conf,原因是.conf在conf.d目录下默认配置会扫描所有.conf文件。如果我们只希望某个站配置,则这里就不能用.conf

2.deny_ua.config文件内容:

 
#forbidden UA
if ($http_user_agent ~* "Bytespider|^$" )
{
    return 403;
}


以上为禁用UA为空或者UA包含Bytespider的访问;
如果需要屏蔽其他者修改
"Bytespider|^$"  
这一部分,例如屏蔽百度
"BaiduSpider|Bytespider|^$" 

3.在nginx对应的server片段引入配置

xxx.con.conf

server{
.....其他省略....
include conf.d/deny_ua.config
.....其他省略....
}
 

4.重启nginx

nginx -s reload

5.测试

curl -A "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.1547.1694 Mobile Safari/537.36; Bytespider" http://www.leftso.com

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.10.2</center>
</body>
</html>
 
地址:https://www.leftso.com/article/686.html

相关阅读

1.新增nginx屏蔽配置文件文件暂时为/etc/nginx/conf.d/deny_ua.config说明:文件名deny_ua.config,后缀为.config非.conf,原因是.con...
最近网站被恶意访问了,了解了一些基础防护手段,nginx屏蔽ua访问 编写文件agent_deany.conf #使用方法 #创建目录 /etc/nginx/conf.d/deny [注意]...
Linux(centos) nginx安装和nginx入门配置方法1.切换至root用户$su - 2.执行在线安装命令#yum install nginx -y 如果上面提示以下内容[root...
打开任务管理器发现一大堆nginx进程
最近博客升级总是nginx原生的502页面,感觉有些不友好,特此研究了下502页面的配置nginx 502配置502页面核心配置 #502 page error_p...
解决centos系统使用yum安装nginx启动时报错[::]:80 failed (97: Address family not supported by protocol)一般默认的使用yu...
nginx开启gzip压缩配置配置参考:server{listen443ssl;server_namet.example.com;#charsetkoi8-r;#access_log/var/l...
nginx 跨域头统一配置 location / { add_header Access-Control-Allow-Origin *; add_header Access-...
nginx代理Tomcat带路径的项目location / {                 proxy_pass http://localhost:8080/v1/;            ...
centos 6 nginx+php7环境配置 做一记录,谨防采矿 $title(php.conf) # # The default server # server { listen ...