利用nginx secure link module防盗链

阿熊的窝 2009-10-29 12:00:02 累计浏览 5,605 次

之前写过一篇blog是《利用lighttpd的mod_secdownload实现防盗链》,最近看到nginx也有类似功能,叫secure_link_module模块也试验了一把。

nginx需要打一个补丁才能实现跟lighttpd一样,通过时间戳来控制url过期。

1.给nginx打补丁,下载《nginx-secure-link-ttl》:

 
cd nginx-0.7.62
patch -p1 < ../nginx-secure-link-ttl.patch

2.编译nginx的时候加上“-with-http_secure_link_module”

3.配置nginx:

 
location /down/ {
            secure_link_secret "sbear.cn";  //密钥
            secure_link_ttl on;
            root /data/test/down;
            if ($secure_link = "") {
                return 403;
            }
            rewrite ^ /$secure_link break;
        }

4.php demo:

 
<?php
define(URL_TIMEOUT, 3600); //这里设置过期时间单位是秒
$prefix = "http://www.sbear.cn/down";
$protected_resource = "test.exe";
$secret = "sbear.cn";  //密钥
$time = pack('N', time() + URL_TIMEOUT);
$timeout = bin2hex($time);
$hashmac = md5( $protected_resource . $time . $secret );
$url = $prefix . "/" . $hashmac . $timeout . "/" . $protected_resource;
 
echo "<a href=" . $url . ">down</a>";
echo time();
?>

官方模块介绍:http://wiki.nginx.org/NginxHttpSecureLinkModule

同分类推荐文章

  1. 全盘加密应对的威胁模型、分层设计与实践 (2026-11-14 03:10:02)
  2. 别了,SMTP mTLS (2026-11-14 00:10:02)
  3. Stop Writing Authentication Checks Into Every Service. Combine Them in your Kubernetes Gateway. (2026-10-27 13:40:42)

查看更多 安全 文章 →

建议继续学习

  1. 配置Nginx+uwsgi更方便地部署python应用 (累计阅读 109,256)
  2. 搜狐闪电邮箱的 Nginx/Postfix 使用模式 (累计阅读 34,258)
  3. 记录一个软中断问题 (累计阅读 17,390)
  4. 解析nginx负载均衡 (累计阅读 17,000)
  5. Nginx模块开发入门 (累计阅读 11,530)
  6. server日志的路径分析 (累计阅读 11,499)
  7. Cacti 添加 Nginx 监控 (累计阅读 11,296)
  8. 检查nginx配置,重载配置以及重启的方法 (累计阅读 11,290)
  9. fsockopen 异步处理 (累计阅读 10,805)
  10. 使用Squid缓存视频 (累计阅读 10,587)