Nginx--Secure_link_module
1. 在实验环境中检查下Secure_link_module 模块是否安装,在环境中运行nginix -V 查看如图所示则安装。
ps:实验环境中的nginx 采用的是yum 的安装方式,yum安装的时候默认就把secure_link_module模块安装上了。
2. 在实验环境中的/etc/nginx/conf.d 目录中的配置文件内容如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
root /opt/app/code;
location / {
#$arg为参数,$arg_md5,$arg_expires 相当于nginx变量里取请求的 md5的数值和expires的数值
根据服务器端 secure_link_md5 这个算法 算出的数值和$secure_link这个变量通过对比来确定是否合法
对比合法的话通过判断是否为空或者为零则可判断。
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;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
3. 进入到/opt/app/code目录中在 /opt/app/code/download目录中创建了一个演示文件file.img
4. 实验演示我们可以看到是在服务器端匹配,然后再nginx端进行验证,需要在服务器端生产一个加密好的串。在实验环境中编写了一个md5.sh脚本
#!/bin/sh
#
#Auth:Jeson@imoocc.com
servername="jeson.t.imooc.io" #自定义变量,请求域名
download_file="/download/file.img" #自定义变量,请求路径
time_num=$(date -d "2020-01-08 00:00:00" +%s) #生成一个expire时间戳
secret_num="imooc" # 这个secret_num和nginx中配置的num 是 一样的
res=$(echo -n "${time_num}${download_file} ${secret_num}"|openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)
#使用openssl 命令生成一个加密连接串
echo "http://${servername}${download_file}?md5=${res}&expires=${time_num}"