有一个非常好的 nginx 模块可以做到这一点。
URL 有两个参数 - 我们称它们为 s(安全性)和 t(时间戳)。安全性是根据时间戳、路径和盐生成的安全哈希(在您的情况下只需添加 ip)。
$ip = $_SERVER['REMOTE_ADDR'];
$salt = 'change me cause im not secure';
$path = '/download/webapp.rar';
$timestamp = time() + 3600; // one hour valid
$hash = md5($salt . $ip . $timestamp . $path); // order isn't important at all... just do the same when verifying
$url = "http://mysite.com{$path}?s={$hash}&t={$timestamp}"; // use this as DL url
核实:
$ip = $_SERVER['REMOTE_ADDR'];
$salt = 'change me cause im not secure';
$path = $_SERVER['REQUEST_URI'];
$hashGiven = $_GET['s'];
$timestamp = $_GET['t'];
$hash = md5($salt . $ip . $timestamp . $path);
if($hashGiven == $hash && $timestamp <= time()) {
// serve file
} else {
die('link expired or invalid');
}
现在您只需将下载重写为“中间人”脚本即可。
nginx 重写示例:
location /download {
rewrite ^.*$ /download.php last;
break;
}
我不太熟悉 apache 重写,所以你可以自己检查一下。
如果您使用以下模块之一,则不需要自己验证所有这些,并且它在性能方面要好得多,但请注意,它提供更多配置,有时还提供另一种生成 url 和哈希的方法(请参阅此处的模块文档)。
或者你只使用 nginx 安全链接模块:http://wiki.nginx.org/HttpSecureLinkModule http://wiki.nginx.org/HttpSecureLinkModule
还有一个 Lighty 的吊坠:http://redmine.lighttpd.net/wiki/1/Docs:ModSecDownload http://redmine.lighttpd.net/wiki/1/Docs:ModSecDownload
或者nginx安全下载模块:http://wiki.nginx.org/HttpSecureDownload http://wiki.nginx.org/HttpSecureDownload
也许 apache 也有一些东西...也许你可以在那里重写一些东西...