## 编辑配置文件html
[root@wy ~]# vim /usr/local/nginx/conf/vhosts/test.confnginx
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {vim
access_log off;缓存
expires 15d;curl
#防盗链ide
valid_referers none blocked server_names *.test.com *.aaa.com *.bbb.com;测试
if ($invalid_referer) {网站
return 403;url
#rewrite .* http://www.example.com/nophoto.gif; //也能够跳转到一个图片spa
}
}
解释说明:
由于以前配置了静态文件缓存,expires 15d与access_log off匹配条件重复,因此与此功能合并;
valid(至关于指定白名单)与invalid是反义词
## 检查并从新加载
[root@wy ~]# /usr/local/nginx/sbin/nginx -t
[root@wy ~]# /usr/local/nginx/sbin/nginx -s reload
## 测试
[root@wy ~]# curl -e "http://www.baidu.com/sfskl" -x127.0.0.1:80 http://www.test.com/static/p_w_picpath/smiley/default/kiss.gif -I
HTTP/1.1 403 Forbidden
Server: nginx/1.6.2
Date: Thu, 10 Nov 2016 23:38:04 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
解释说明:
curl -e 能够指定你的referer,在这是伪造的,咱们是为了作此实验,看百度的能不能用个人图片;
403,说明它盗链了。
[root@wy ~]# curl -x127.0.0.1:80 http://www.test.com/static/p_w_picpath/smiley/default/kiss.gif -I HTTP/1.1 200 OK
解释说明:
把-e 选项去掉,正常的,咱们是能够访问的
[root@wy ~]# curl -e "http://www.test.com" -x127.0.0.1:80 http://www.test.com/static/p_w_picpath/smiley/default/kiss.gif -I
HTTP/1.1 200 OK
[root@wy ~]# curl -e "http://www.aaa.com" -x127.0.0.1:80 http://www.test.com/static/p_w_picpath/smiley/default/kiss.gif -I
HTTP/1.1 200 OK
解释说明:
使用了咱们本身的网站做为referer,就是能够访问的