## 編輯配置文件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,就是能夠訪問的