@dume2007
2017-02-24T11:49:47.000000Z
字数 2179
阅读 3164
lua
nginx
下载地址: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
下载地址: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 # 不需要安装
下载地址: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 -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
在在/usr/local/nginx/conf/nginx.conf中的server节加入如下代码:
location ^~ /uploads/ {
access_by_lua '
local secret_key = "xhh_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
';
}
service nginx restart
访问URL:http://10.15.168.25/uploads/test.png?token=52f0b2b4ebedb0094eff53383098a4b8×tamp=1487901531
权限判断规则:
1、URL必须包含timestamp和token参数
2、timestamp为秒级时间戳,10s之内时间有效
3、token为md5(timestamp+secret_key)
时间过期输出403 Forbidden
验证成功返回文件。
Nginx API for Lua:
https://github.com/openresty/lua-nginx-module#nginx-api-for-lua