一:一般的防盜鏈如下:
<code>location ~* \.(gif|jpg|png|swf|flv)$ { valid_referers none blocked www<span>.jzxue</span><span>.com</span> jzxue<span>.com</span><span>; </span> if ($invalid_referer) { rewrite ^/ http://www<span>.jzxue</span><span>.com</span>/retrun<span>.html</span><span>; </span><span>#return 403; </span> } } </code>
第一行:gif|jpg|png|swf|flv
表示對gif、jpg、png、swf、flv字尾的檔案實施防盜鏈
第二行: 表示對www.ingnix.com這2個來路進行判斷
if{}裡面內容的意思是,如果來路不是指定來思是,如果來路不是指定來路就跳到http://www.jzxue.com/retrun.html頁面,當然直接回到403也是可以的。
二:針對圖片目錄防止盜鏈
<code>location /images<span>/</span> { alias /<span>data</span>/images<span>/</span>; valid_referers <span>none</span> blocked server_names <span>*</span><span>.</span>xok<span>.</span>la xok<span>.</span>la ; <span>if</span> (<span>$invalid_referer</span>) {<span>return</span><span>403</span>;} } </code>
三:使用第三方模組ngx_http_accesskey_module實作Nginx防盜鏈
實作方法如下:
1. 下載NginxHttpAccessKeyModule模組檔案:http://wiki.nginx.org/File:Nginx-accesskey-2.0.3.tar.gz;
2. 解壓縮此檔案後,找到nginx-accesskey-2.0.3下的config檔。編輯此檔案:替換其中的”$HTTP_ACCESSKEY_MODULE”為”ngx_http_accesskey_module”;
3. 用參數重新編譯nginx:
<code>./configure --<span>add</span>-<span>module</span>=path/<span>to</span>/nginx-accesskey <<pestd <span>add</span></code>
上面需要加上原有到編譯參數,然後執行: make && make install
修改nginx的conf文件,加入以下幾行:
<code><span>location</span> /download { <span>accesskey</span><span>on</span>; <span>accesskey_hashmethod</span> md5; <span>accesskey_arg</span><span>"key"</span>; <span>accesskey_signature</span><span>"mypass<span>$remote_addr</span>"</span>; }</code>
存取測試腳本download.php:
<code><span><span><?</span><span>$ipkey</span>= md5(<span>"mypass"</span>.<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]); <span>$output_add_key</span>=<span>"<a href=http://www.jzxue.com/download/G3200507120520LM.rar?key="</span>.<span>$ipkey</span>.<span>">download_add_key</a><br />"; <span>$output_org_url</span>=<span>"<a href=http://www.jzxue.com/download/G3200507120520LM.rar>download_org_path</a><br />"</span>; <span>echo</span><span>$output_add_key</span>; <span>echo</span><span>$output_org_url</span>; <span>?></span></code>
以上就介紹了Nginx防盜鏈的3種方法,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。
🎜 🎜