nginx防盗链

2016-03-25 14:19  994人阅读  评论 (0)
Tags: nginx

nginx 配置文件

location /files/ {
    secure_link $arg_md5,$arg_expires;
    secure_link_md5 "$secure_link_expires$uri$remote_addr 12345678";

    if ($secure_link = "") {
        return 403;
    }

    if ($secure_link = "0") {
        return 410;
    }
}

php签名计算

<?php
$expires = time()+300;
$uri = "/files/dotcoo.com.pdf";
$remote_addr = $_SERVER["REMOTE_ADDR"];
$secret = "12345678";

$md5 = rtrim(strtr(base64_encode(md5("$expires$uri$remote_addr $secret", true)), '+/', '-_'), "=");

echo "http://www.dotcoo.com/$uri?md5=$md5&expires=$expires";