Nginx 配置之视频防盗链

Nginx 防盗链 About 1,274 words

查看安装的模块

nginx -V

安装 secure_link 模块

如果没有secure_link模块,则需安装。

./configure --with-http_secure_link_module \ --prefix=/usr/local/nginx --with-http_stub_status_module
make
make install

重启 Nginx 服务

不要用nginx -s reload

nginx -s stop
nginx -s start

配置 nginx.conf

location / {
    root /resource/video;
    #这里配置了2个参数一个是st,一个是e
    secure_link $arg_st,$arg_e;
    #st的哈希格式为 自定义秘钥+url+e,e为时间戳单位s,url为请求地址
    secure_link_md5 customSecretKey$uri$arg_e;
    #这里我们的st是我们按照secure_link_md5的方式计算的哈希,secure_link会比对它计算的哈希值是否与我们的st参数一致
    if ($secure_link = "") {
        #资源不存在或哈希比对失败
        return 402;
    }
    if ($secure_link = "0") {
        #时间戳过期
        return 404;
    }
    if ($request_filename ~* ^.*?\.(mp4)$){
        #直接下载防止打开文件  格式: (mp4|txt|jpg)
        add_header Content-Disposition 'attachment;';
    }
}

Java 生成防盗链地址

使用Apache commons-codec提供的jar

注意:进行md5运算时,视频名字前加斜杆 如:/demo.mp4

public static void main(String[] args) {
    String time = String.valueOf(System.currentTimeMillis() / 1000 + Long.valueOf(600));// +600代表600秒后地址失效
    String md5 = Base64.encodeBase64URLSafeString(DigestUtils.md5("customSecretKey" + "/demo.mp4" + time));
    return "http://127.0.0.1/demo.mp4?st=" + md5 + "&e=" + time;
}

最终视频防盗链地址为:http://127.0.0.1/demo.mp4?st=xxxxxxx&e=xxxxxx

Views: 4,494 · Posted: 2019-03-20

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh