Nginx ngx_http_secure_link_module模块能够检查请求链接的权限以及是否过期,多用于下载服务器防盗链。
./configure --with-http_secure_link_module #编译nginx时加入
1
该模块提供两种工作模式:
通过配置 secure_link
,secure_link_md5
,可实现对链接进行权限以及过期检查判断的功能。
uri
(如demo.com/s/link uri则为/s/link) 以及 加密 密钥secret
,如果该链接具有时效,则expression需包含$secure_link_expires
,expression还可以加入客户端信息,如访问IP,浏览器版本信息等.nginx配置
位置 /s/ {安全链接 $arg_md5
,$ arg_expires ;
secure_link_md5我的秘密 $arg_expires $uri ; if ( $secure_link = "" ) { 返回403 ;
} if ( $secure_link = "0" ) { 返回410 ;
}
}
在UNIX系统中MD5的数值计算下面得出
echo -n '2147483647/s/link127.0.0.1 秘密' | \
openssl md5 -二进制| openssl base64 | tr + / -_ | tr -d =
$secret = "秘密" ; $uri = "/hls/cctv1.m3u8" ; $过期=时间()+ 300 ;
$md5 = base64_encode(md5( $expire . $uri . $secret , true )); $md5 = strtr( $md5 , '+/' , '-_' ); $md5 = str_replace( '=' , '' , $md5 ); $url = "http://demo.com/hls/cctv1.m3u8?md5={ $md5 }&expires={ $expire }" ;
通过配置 secure_link_secret
可实现对链接进行权限判断的功能。
请求的链接形式如下
/prefix/hash/link
前缀是非斜杠的任意的字符串,哈希值是link与密钥secret的MD5值,link请求的路径
位置/ s/{
secure_link_secret 我的秘密;
如果 ($secure_link = "") {
返回403;
}
重写 ^ /helloworld/ $secure_link ;
}
位置 /helloworld/ { 返回503 ;
}
如访问
/s/fdb191a547321828363169ca530ee271/hello
被直接到
/helloworld/hello
哈希值的计算方法 md5(link.MySecret)