防盗链的实现方法

Nginx/Web服务器
322
0
0
2022-12-23

nginx防盗链配置

location ~.*\.(gif|jpg|png|flv|swf|rar|zip)${
    valid referers none blocked test.com *.test.com;
    if ($invalid_referer)
    {
        #return 403;
        rewrite ^/ http://www.test.com/403.jpg;
    }
}

传统防盗链遇到的问题(伪造Referer)

可以使用加密签名解决

location ~.*\.(gif|jpg|png|flv|swf|rar|zip)${
    accesskey on;
    accesskey_hashmethod md5;
    accesskey_arg sign;
    accesskey_signature "jason$remote_addr";
    expires    30d;
}
<?php
$sign = md5('jason'.$_SERVER['REMOTE_ADDR]);
echo '<img src="./logo_new.png?sign='. $sign.'">';