利用nginx secure link module防盗链
nginx 的 secure_link_module 模块可以实现类似 lighttpd mod_secdownload 的防盗链效果,但原生功能缺少基于时间戳的 URL 过期控制,需要额外打一个 nginx-secure-link-ttl 补丁来实现。具体步骤是:对 nginx-0.7.62 打补丁,编译时加上 -with-http_secure_link_module,然后在 location 中配置 secure_link_secret 设置密钥、开启 secure_link_ttl,当 $secure_link 校验失败时返回 403。
配套的 PHP 端逻辑是用 md5(资源名 . 过期时间 . 密钥) 生成 hashmac,把 hashmac 和 hex 编码的过期时间拼进 URL 前缀,从而生成带时效的下载链接(示例中过期时间设为 3600 秒)。官方模块文档见 NginxHttpSecureLinkModule 页面。