nginx 之高级模块

secure_link_module 模块

作用:

  • 制定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问
  • 限制链接生效周期

配置语法

  • Syntax:secure_link expression 
  • default : - 
  • Context: http、server、location
  • Syntax:secure_link_md5 expression; 
  • default : - 
  • Context:http、server、location

secure_link模块实现请求资源验证

 首先确认安装的时候已经编译了此模块

 

 test_safe_down.conf

server {
    listen       80;
    server_name  www.zhangbiao.com;

    root /opt/app/code;

    location / {
        secure_link $arg_md5,$arg_expires;
        secure_link_md5 "$secure_link_expires$uri imooc";

        if ($secure_link = "") {
            return 403;
        }

        if ($secure_link = "0") {
            return 410;
        }
    }

    }
}

  

在/opt/app/code/download下准备一个文件用于下载

 

找一个md5加密的文件放在/opt/work下,这里如果没有的openssl命令话需要用yum安装

 md5url.sh

#!/bin/sh
#
#Auth:www.zhangbiao.com
servername="www.zhangbiao.com"
download_file="/download/test.img"
time_num=$(date -d "2019-7-18 00:00:00" +%s)
secret_num="imooc"
res=$(echo -n "${time_num}${download_file} ${secret_num}"|openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)
echo "http://${servername}${download_file}?md5=${res}&expires=${time_num}"

 

访问

http://www.zhangbiao.com/download/?md5=v5W0ZVlg&expires=1563379200

 

 访问一个错误的

猜你喜欢

转载自www.cnblogs.com/crazymagic/p/11037488.html
今日推荐