我给HR小姐姐讲了Nginx防盗链,我俩聊的很开心
概述
location ~* \.(jpg)$ {
root html;
valid_referers 192.168.42.135;
if ($invalid_referer){
rewrite ^/ http://192.168.42.135/images/loading.png;
}
}
配置截图
测试过程
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style type="text/css">
*{margin:0;padding:0;}
.avatar{width:200px;overflow: hidden;border:1px solid lightblue;}
img{width:100%;display: block;}
</style>
</head>
<body>
<h2>hello,this is a server by python http</h2>
<div class="avatar">
<img src="http://192.168.42.135/images/avatar.jpg" alt="设置防盗链">
</div>
<div class="avatar">
<img src="http://192.168.42.135/images/image.png" alt="没有防盗链">
</div>
</body>
</html>
index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to OpenResty!</title>
<style>
*{margin:0;padding:0}
.avatar{width:200px;overflow:hidden;border:1px solid lightblue;}
img{width:100%;display:block;}
</style>
</head>
<body>
<h2>welcome to nginx!</h2>
<div class="avatar"><img src="http://192.168.42.135/images/avatar.jpg"/></div>
<div class="avatar"><img src="http://192.168.42.135/images/image.png"/></div>
</body>
</html>
本地访问:http://10.119.9.167:8000/
虚拟机访问:http://192.168.42.135/
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
valid_referers none blocked start.igrow.cn sta.igrow.cn
valid_referers 192.168.42.135;
内容来源于网络,侵删
鉴于篇幅有限,更多详细课程大纲/知识体系/实体书籍/网工必备工具包等请扫码加群获取!
领取福利