防盗链可以解决网站资源被其他网站盗用,节约带宽,避免原创版权被非版权费盗用。Nginx的官方模块ngx_http_secure_link_module可以很好的解决资源被盗用的问题。
实验步骤:SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/
1、编译模块:SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/
--with-http_secure_link_module; SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/
2、模块secure_link_secret word:SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/
012345678910 location /s/ {secure_link_secret passwd;if ($secure_link = "") {return 405;}rewrite ^ /media/$secure_link;}location /media/ {add_header Cache-Control max-age=31536000;}
URL生成方法:
1:密码:passwd
2:文件:video.mp4
3:MD5加密:echo -n 'video.mp4passwd' | openssl dgst -md5
4:访问地址:http://localhost/s/146b9a69c11eceb3c67865c2fd9abfe5/video.mp4
#146b9a69c11eceb3c67865c2fd9abfe5 为 文件+密码计算出的结果 SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/
3、secure_link_md5加密方法SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/
01234567891011 location ~ .*\.mp4${add_header Cache-Control max-age=31536000;secure_link $arg_md5,$arg_expires;secure_link_md5 "$secure_link_expires$uri passwd";if ($secure_link = "") {return 404;}if ($secure_link = "0") {return 403;}}
URL生成方法:
shell脚本:
0123456 key="passwd"url="localhost"path="/media/video.mp4"endtime=$(date -d "2017-12-22 00:00:00" +%s) #假设为24小时res=$(echo -n "${endtime}${path} ${key}" | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)echo $resecho "http://${url}${path}?md5={res}&expires=${endtime}"
生成的地址:http://localhost/media/video.mp4?md5=TC291LXl9Dlvau7vDRRFkQ&expires=1513872000 SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/
参考文档:
1:http://nginx.org/en/docs/http/ngx_http_secure_link_module.html#secure_link_md5SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/
SourceByrd's Weblog-https://note.t4x.org/environment/nginx-http-secure-link-module/