用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
验证成功返回文件。

相关推荐

  1. Lua控制Nginx静态文件url访问权限

    2024-03-31 12:22:03       35 阅读
  2. nginx+lua 实现URL重定向(根据传入参数条件)

    2024-03-31 12:22:03       25 阅读
  3. Nginx访问控制

    2024-03-31 12:22:03       54 阅读
  4. nginx访问控制

    2024-03-31 12:22:03       38 阅读
  5. nginx访问控制

    2024-03-31 12:22:03       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 12:22:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 12:22:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 12:22:03       87 阅读
  4. Python语言-面向对象

    2024-03-31 12:22:03       96 阅读

热门阅读

  1. Vue-前端应用开发平时作业第二章

    2024-03-31 12:22:03       28 阅读
  2. Ubuntu下udp通信

    2024-03-31 12:22:03       40 阅读
  3. es与mysql同步问题

    2024-03-31 12:22:03       39 阅读
  4. Leetcode 3. 无重复字符的最长子串

    2024-03-31 12:22:03       41 阅读
  5. 带头双向循环链表的实现及注释教学

    2024-03-31 12:22:03       39 阅读
  6. 面试碰到的一些问题

    2024-03-31 12:22:03       34 阅读
  7. PHP - ZipArchive上传、下载实例

    2024-03-31 12:22:03       39 阅读
  8. HTML 中的 jQuery 事件处理与 Ajax 异步请求

    2024-03-31 12:22:03       37 阅读
  9. 【图像处理】-1.图像二值化

    2024-03-31 12:22:03       39 阅读
  10. Mysql 常用语句及用法记录

    2024-03-31 12:22:03       34 阅读