用Lua控制Nginx静态文件的url访问权限

需求背景:比如我们有一个存储文件的web服务器,一般通过url可直接访问到:http://127.0.0.1/uploads/test.rar,如果我们需要限制别人的访问,可以通过添加lua脚本来控制url访问权限,以下是实现步骤。

安装LuaJIT

下载地址:http://luajit.org/download.html

复制代码
tar zxf LuaJIT-2.1.0-beta2.tar.gz
cd LuaJIT-2.1.0-beta2
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit

下载ngx_devel_kit模块

下载地址:https://github.com/simpl/ngx_devel_kit/tags

目前最新版本:https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

复制代码
tar -xzvf v0.3.0.tar.gz # 不需要安装

下载lua-nginx-module模块

下载地址:https://github.com/openresty/lua-nginx-module/tags

目前最新版本:https://github.com/openresty/lua-nginx-module/archive/v0.10.7.tar.gz

复制代码
tar -xzvf v0.10.7.tar.gz # 不需要安装

重新编译安装Nginx

nginx -V查看已经编译的配置

复制代码
nginx -V

输出结果:

复制代码
--user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

进入Nginx源码目录,重新编译,加上以下参数(注意以下module解压路径)

复制代码
--with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/path/to/ngx_devel_kit-0.3.0 --add-module=/path/to/lua-nginx-module-0.10.7

完整编译配置如下:

复制代码
# 设置环境变量
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
# 配置
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/home/vagrant/ngx_devel_kit-0.3.0 --add-module=/home/vagrant/lua-nginx-module-0.10.7
# 编译安装
make -j2
make install

配置Nginx.conf

在/usr/local/nginx/conf/nginx.conf中的server节加入如下代码:

复制代码
location ^~ /uploads/ {
            access_by_lua '
                local secret_key = "prefix_eu56#42dfd6g*@"
                local diff_time  = 10
                local local_time = ngx.time()
                local timestamp = tonumber(ngx.var.arg_timestamp)
                local token     = ngx.var.arg_token
 
                if (timestamp == nil or token == nil) then
                    ngx.exit(403)
                elseif (timestamp + diff_time < local_time) then
                    ngx.exit(403)
                end
 
                local access_token = ngx.md5(timestamp..secret_key)
 
                if token == access_token then
                    return true
                else
                    ngx.exit(403)
                end
            ';
        }

重启Nginx

复制代码
service nginx restart

检查生效状态

访问URL:http://127.0.0.1/uploads/test.rar?token=52f0b2b4ebedb0094eff53383098a4b8\&timestamp=1487901531

权限判断规则:

复制代码
1、URL必须包含timestamp和token参数
2、timestamp为秒级时间戳,10s之内时间有效
3、token为md5(timestamp+secret_key)

时间过期输出403 Forbidden

验证成功返回文件。

相关推荐
Boilermaker19924 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
MM_MS4 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
꧁Q༒ོγ꧂5 小时前
LaTeX 语法入门指南
开发语言·latex
njsgcs5 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
alonewolf_995 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
古城小栈5 小时前
Rust 迭代器产出的引用层数——分水岭
开发语言·rust
ghie90906 小时前
基于MATLAB的TLBO算法优化实现与改进
开发语言·算法·matlab
恋爱绝缘体16 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
wuk9986 小时前
VSC优化算法MATLAB实现
开发语言·算法·matlab
AI小怪兽6 小时前
基于YOLOv13的汽车零件分割系统(Python源码+数据集+Pyside6界面)
开发语言·python·yolo·无人机