[nginx]盗链和防盗链场景模拟实现

盗链环境模拟

http://www.daolian.com/index.html 这个页面盗用http://www.maotai.com/qq.jpg这个站点页面的图.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>毛台</title>
</head>
<body>
<h1>maotai</h1>
<p><img src="http://www.maotai.com/qq.jpg"></p>
</body>
</body>
</html>

搭建2个虚拟主机:

http://www.maotai.com/qq.jpg
http://www.daolian.com/index.html, 这个盗用上面那个站点的图.
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.maotai.com;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

    server {
        listen       80;
        server_name  www.daolian.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}

访问测试
806469-20180311220405308-832187826.png

806469-20180311220331339-125517098.png

防盗链效果

  • 拓扑图

806469-20180312113648972-2091847222.png

  • 要实现的效果
(n1)www.maotai.com/qq.jpg
(n1)www.daolian.com      #实现效果: 盗用qq.jpg链接时, 将盗链的连接改为www.192.168.14.12/404.jpg

(n2)www.192.168.14.12/404.jpg
  • n1访问qq.jpg正常访问时候没问题

806469-20180312114351448-41770561.png

  • 盗链服务器访问

806469-20180312114326769-780987437.png

未盗链时应该是这样的
806469-20180312114441130-1377553510.png

nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.maotai.com maotai.com;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~* \.(gif|jpg|png|swf|flv)$ {
            valid_referers none blocked *.maotai.com;
            if ($invalid_referer) {
                rewrite ^/ http://192.168.14.12/404.jpg; #注这里要重新找台服务器, 如果同一台,我没做出效果
                #return 404;
            }
        }

    }

    server {
        listen       80;
        server_name  www.daolian.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}

Module ngx_http_referer_module
Nginx软件优化
完美的nginx图片防盗链设置详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值