配置防盗链的目的:当别的网站盗用了我们网站的图片,视频等文件资源,放到他们自己的网站上去,一方面这属于无耻的盗窃行为,另一方面在并发量很大的情况下势必会无端增加我们网站的流量,增加服务器的负载。

解决办法:与Apache一样,Nginx也可以限制referer


# cd /usr/local/nginx/conf/vhosts

# vim test.conf


server

{

    listen 80;

    server_name www.test.com;

    index index.html index.htm index.php;

    root /data/www;

    

    # 定义访问日志的路径和格式

    access_log /tmp/nginx_access.log test;

    

    # 不记录静态文件的的访问日志

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)$ {

        access_log off;

        expires 10d;

        valid_referers none blocked *.test.com *.baidu.com *.google.com;

        if ($invalid_referer) {

            return 403;

        }

    }

}


用curl去检测那些invalid_referer能否访问我们网站:

curl -e 可以用来指定referer

# curl -e 'http://www.qq.com/2222' -x127.0.0.1:80 'http://www.test.com/static/p_w_picpath/common/logo_88_31.gif' -I


HTTP/1.1 403 Forbidden

Server: nginx

Date: Mon, 22 Aug 2016 08:15:59 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive