一、一般的最简单的对图片防盗链的方法:

引用
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
                    {
                    valid_referers none blocked www.wangqiao123.com wangqiao123.com;
                    if ($invalid_referer){
                            rewrite ^/ http://www.wangqiao123.com/return.html;
                            #return 403;
                        }
                            expires            15d;
                    }


   第一行:gif|jpg|jpeg|png|bmp|swf 表示对gif、jpg、png、bmp、swf、flv后缀的文件实行防盗链
  第二行: 表示对www.wangqiao123.com wangqiao123.com这2个来路进行判断
  if{}里面内容的意思是,如果来路不是指定来路就跳转到http://www.wangqiao123.com/retrun.html页面,当然直接返回403也是可以的。

  二、针对图片目录防止盗链

引用
location /p_w_picpaths/
                     {
                         alias /usr/local/nginx/htdocs/p_w_picpaths/;
                         valid_referers none blocked server_names *.wangqiao123.com wangqiao.com;
                         if ($invalid_referer)
                                {
                                return 403;
                                }
                     }


   三、用accesskey模块实现Nginx服务器深度防盗链

    首先下载Nginx-accesskey模块:
引用
wget http://wiki.nginx.org/p_w_picpaths/5/51/Nginx-accesskey-2.0.3.tar.gz
tar xzvf Nginx-accesskey-2.0.3.tar.gz

    修改其目录下的config文件,将$HTTP_ACCESSKEY_MODULE替换成ngx_http_accesskey_module
   重新编译nginx 并在其原有编译参数的基础上添加 --add-module=path/nginx-accesskey-2.0.3

    修改nginx的conf文件,添加以下几行:
引用
  
location /download {
     accesskey                         on;
     accesskey_hashmethod    md5;
     accesskey_arg                 "key" ;
     accesskey_signature     "mypass$remote_addr" ;
     }


  其中:
accesskey为模块开关;
accesskey_hashmethod为加密方式MD5或者SHA-1;
accesskey_arg为url中的关键字参数;
accesskey_signature为加密值,此处为mypass和访问IP构成的字符串。

访问测试脚本download.php:
引用
<?php
$ipkey= md5("mypass".$_SERVER['REMOTE_ADDR']);
$output_add_key="<a href=http://www.wangqiao123.com/download/1.rar?key=".$ipkey.">download_add_key</a><br />"
$output_org_url="<a href=http://www.wangqiao123.com/download/1.rar>download_org_path</a><br />"
echo $output_add_key;
echo $output_org_url;
?>


    访问第一个download_add_key链接可以正常下载,第二个链接download_org_path会返回403 Forbidden错误。