IT技术博客大学习 共学习 共进步

利用nginx secure link module防盗链

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

之前写过一篇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. 配置Nginx+uwsgi更方便地部署python应用 (阅读 106,824)
  2. 搜狐闪电邮箱的 Nginx/Postfix 使用模式 (阅读 33,762)
  3. 解析nginx负载均衡 (阅读 16,422)
  4. Nginx模块开发入门 (阅读 11,041)
  5. 检查nginx配置,重载配置以及重启的方法 (阅读 10,683)
  6. Cacti 添加 Nginx 监控 (阅读 10,362)
  7. Nginx+FastCgi+Php 的工作机制 (阅读 10,084)
  8. 奇怪的 Nginx 的 upstream timed out 引起响应 502 (阅读 9,823)
  9. nginx的配置文件 (阅读 9,782)
  10. 解决 nginx 反向代理网页首尾出现神秘字符的问题 (阅读 8,961)