Nginx配置——防盗链
## 編輯配置文件
[root@wy ~]# vim /usr/local/nginx/conf/vhosts/test.conf
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
? ? ? ?access_log off;
? ? ? expires 15d;
? ? ? ?#防盜鏈
? ? ? valid_referers none blocked server_names *.test.com *.aaa.com *.bbb.com;
? ? ? ?if ($invalid_referer) {
? ? ? ? ? ?return 403;
? ? ? ? ? ?#rewrite .* http://www.example.com/nophoto.gif; ? //也可以跳轉到一個圖片
? ? ? ?}
? ? }
解釋說明:
因為之前配置了靜態文件緩存,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,就是可以訪問的
轉載于:https://blog.51cto.com/warm51fun/1888119
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Nginx配置——防盗链的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在DevExpress程序中使用Grid
- 下一篇: 【转载】MySQL启多个实例