新建配置配置文件 (例如进入到nginx安装目录下的conf目录,创建: agent_deny.conf)
禁止Scrapy等工具的抓取 if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 403; }
禁止指定UA及UA为空的访问
1 |
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
#forbidden Scrapy
if ($http_user_agent ~* (Scrapy|Curl|HttpClient))
{
return 403;
}
#forbidden UA
if ($http_user_agent ~ "Bytespider|FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$" )
{
return 403;
}
#forbidden not GET|HEAD|POST method access
if ($request_method !~ ^(GET|HEAD|POST)$)
{
return 403;
} |
然后,在网站相关配置中的 server段插入如下代码: include agent_deny.conf;
重启nginx:
1 |
|
/data/nginx/sbin/nginx -s reload |
测试 使用curl -A 模拟抓取即可,比如:
1 |
|
curl -I -A 'YYSpider' <<<a href= " http://www.xxx.con%3e%3e/ " rel= "external nofollow" >www.xxx.con>>< /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为空的抓取:
1 |
|
curl -I -A ' ' <<<a href= " http://www.xxx.cn> >" rel= "external nofollow" >www.xxx.cn>>< /a > |
结果
[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
模拟百度蜘蛛的抓取:
1 |
|
curl -I -A 'Baiduspider' <<<www.xxx.cn>>> |
[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类型
1 |
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
FeedDemon 内容采集
BOT/0.1 (BOT for JCE) sql注入
CrawlDaddy sql注入
Java 内容采集
Jullo 内容采集
Feedly 内容采集
UniversalFeedParser 内容采集
ApacheBench cc攻击器
Swiftbot 无用爬虫
YandexBot 无用爬虫
AhrefsBot 无用爬虫
YisouSpider 无用爬虫(已被UC神马搜索收购,此蜘蛛可以放开!)
jikeSpider 无用爬虫
MJ12bot 无用爬虫
ZmEu phpmyadmin 漏洞扫描
WinHttp 采集cc攻击
EasouSpider 无用爬虫
HttpClient tcp攻击
Microsoft URL Control 扫描
YYSpider 无用爬虫
jaunty wordpress爆破扫描器
oBot 无用爬虫
Python-urllib 内容采集
Indy Library 扫描
FlightDeckReports Bot 无用爬虫
Linguee Bot 无用爬虫 |
背景:防止第三方引用链接访问我们的图片,消耗服务器资源和网络流量,我们可以在服务器上做防盗链限制。
实现防盗链的方式有两种:refer方式和签名方式。
refer方式实现防盗链
工作模块:ngx_http_referer_module。
作用变量:$invalid_referer,全局变量。
配置域:server, location
配置:
1 |
2
3
4
5
6
7
8
9
10
11 |
server {
listen 80;
server_name www.imcati.com refer- test .imcati.com;
root /usr/share/nginx/html ;
location ~*\\.(gif|jpg|jpeg|png|bmp|swf)$ {
valid_referers none blocked www.imcati.com;
if ($invalid_referer) {
return 403;
}
}
} |
- valid_referers: 指定资源访问是通过以下几种方式为合法,即白名单。 vaild_referers 有效的引用连接,如下,否则就进入$invaild_refere,返回403 forbiden。
- none:允许缺失的头部访问。
- blocked:允许referer没有对应值的请求。
- server_names:若referer站点域名与server_name中本机配的域名一样允许访问。
到此这篇关于nginx 防盗链防爬虫配置详解的文章就介绍到这了,更多相关nginx 防盗链防爬虫配置内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://segmentfault.com/a/1190000037437691
相关文章
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 41
-
2025-05-29 93
-
2025-05-25 68
-
2025-05-25 40
-
2025-05-29 28