我最近在搭建一个nginx rtmp流服器,使用.m3u8文件+静态预切片.ts文件;安全考虑需要增加防盗链功能;
针对单个.m3u8文件请求增加防盗链没有问题;但是生成.ts动态防盗链有一些问题;
生成.ts动态防盗链方法:
将.m3u8防盗链key=xxxx赋值给.ts(使用ngx_http_substitutions_filter_module-master模块替换内容 .ts 替换成 .ts?key=xxxx); 但是key=xxxx防盗链存在局限性,不能保证key=xxxx和播放地址关联加密;
解决方式是通过对播放地址加入加密串:
.m3u8地址: http://192.168.1.133:801/hls/1000109475_test.m3u8?key=cc8f99b3e00949f04b2a997e03a553cc
.ts地址: http://192.168.1.133:801/hls/1000109475_test/1.ts?key=cc8f99b3e00949f04b2a997e03a553cc
相同路径为:/hls/1000109475_test(可以把相同路径放到加密串中)
nginx配置方法:
location /hls {
set $myuri $uri;
if ($uri ~ (.*).m3u8$){
set $myuri $1;
}
if ($uri ~ (.*)\/[0-9]*.ts$){
set $myuri $1;
}
accesskey on;
accesskey_hashmethod md5;
accesskey_arg "key";
accesskey_signature "mypass$remote_addr$myuri";
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
sub_filter .ts '.ts?key=$arg_key';
sub_filter_once off;
sub_filter_types application/vnd.apple.mpegurl;
alias /home/liujun/hls;
expires 1;
}
目前问题:
Worker_connections 超过24之后,nginx.conf中“if语句正则表达式”和“nginx-accesskey模块”不在同一个域(估计是多线程原因);
导致“nginx-accesskey模块”异常,返回终端”403”错误;