新建配置配置文件 (例如进入到nginx安装目录下的conf目录,创建: agent_deny.conf)
禁止指定UA及UA为空的访问
然后,在网站相关配置中的 server段插入如下代码: include agent_deny.conf;
重启nginx:
测试 使用curl -A 模拟抓取即可,比如:
[root@11 conf]# curl -I -A ‘YYSpider’ www.xxx.cn
HTTP/1.1 403 Forbidden
Server: nginx/1.12.0
Date: Wed, 24 Apr 2019 11:35:21 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
模拟UA为空的抓取:
[root@11 conf]# curl -I -A’ ‘ www.xxx.cn
HTTP/1.1 403 Forbidden
Server: nginx/1.12.0
Date: Wed, 24 Apr 2019 11:36:06 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
模拟百度蜘蛛的抓取:
[root@11 conf]# curl -I -A ‘Baiduspider’ www.xxx.cn
HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Wed, 24 Apr 2019 11:36:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 12 Apr 2019 13:49:36 GMT
Connection: keep-alive
ETag: “5cb09770-264”
Accept-Ranges: bytes
UA类型
nginx 防盗链配置
refer方式实现防盗链
工作模块:ngx_http_referer_module。
作用变量:$invalid_referer,全局变量。
配置域:server, location
配置:
- valid_referers: 指定资源访问是通过以下几种方式为合法,即白名单。 vaild_referers 有效的引用连接,如下,否则就进入$invaild_refere,返回403 forbiden。
- none:允许缺失的头部访问。
- blocked:允许referer没有对应值的请求。
- server_names:若referer站点域名与server_name中本机配的域名一样允许访问。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
原文链接:https://www.yisu.com/zixun/316018.html