用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

验证成功返回文件。

相关推荐
何中应3 天前
Nginx转发请求错误
前端·后端·nginx
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
芝士雪豹只抽瑞克五3 天前
Nginx 高性能Web服务器笔记
服务器·nginx
QQ4022054963 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月3 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js