环境搭建
1.nginx lua环境搭建
安装前准备好如下软件包
· nginx 地址:http://www.nginx.org
· luajit 地址:http://luajit.org/download.html
· HttpLuaModule 地址:http://wiki.nginx.org/HttpLuaModule
1. 下载安装LuaJIT
# cd /usr/local/src
# wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
# tar -xzvf LuaJIT-2.0.2.tar.gz
# cd LuaJIT-2.0.2
# make
出现如下内容表示编译成功
OK Successfully built LuaJIT
make[1]: Leaving directory `/usr/local/src/LuaJIT-2.0.2/src'
==== Successfully built LuaJIT 2.0.2 ====
# make install
出现如下内容,表示安装成功
==== Successfully installed LuaJIT 2.0.2 to /usr/local ====
2. 下载准备nginx lua模块
# cd /usr/local/src
# wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz
# tar -xzvf v0.8.6
1
2
3
3. 安装nginx
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.4.2.tar.gz
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
//先导入环境变量,告诉nginx去哪里找luajit
# export LUAJIT_LIB=/usr/local/lib
# export LUAJIT_INC=/usr/local/include/luajit-2.0
# ./configure --prefix=/usr/local/nginx-1.4.2 --add-module=../lua-nginx-module-0.8.6
# make -j2
# make install
常见错误:loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 解决方法
# /usr/local/nginx-1.4.2/sbin/nginx -v
./objs/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
解决方法:
# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
nginx lua配置
location ~* ^/2328(/.*) {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, ttlsa lua")';
}
5. 启动测试
/usr/local/nginx-1.4.2/sbin/nginx
curl http://test.ttlsa.com/2328/
hello, ttlsa lua //使用curl测试
2.redis lua环境搭建
安装lua-resty-redis
cd /usr/local/src
wget https://github.com/openresty/lua-resty-redis/archive/master.zip
unzip master.zip
cd lua-resty-redis-master
mkdir -p /usr/local/nginx/lua
#将lib拷贝到nginx安装目录下的lua文件夹内
cp -rf lib /usr/local/nginx/lua
cd /usr/local/nginx/lua/lib
ln -s redis.lua resty/redis.lua
代码结构
1.lua脚本
var=ngx.var
arg=ngx.req.get_uri_args()
cjson=require("cjson")
--在table的agr中获取token, token字符串组合成的key值
if arg ~= nil and arg.token ~= nil and arg.token ~= '' then
token = "download:files:" .. arg.token
end
--连接redis
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000)
local ok, err =red:connect("192.168.1.2", 6379)
if not ok then
ngx.say("redis连接失败: ", ok, "<br/>")
ngx.say("redis连接失败: ", err, "<br/>")
return
end
-- 请注意这里 auth 的调用过程 这是redis设置密码的
local count
count, err = red:get_reused_times()
if 0 == count then
ok, err = red:auth("123456")
if not ok then
ngx.say(cjson.encode({code = 500,message = err}))
return
end
elseif err then
ngx.say("failed to get reused times: ", err, "<br/>")
return
end
--redis中若 key 存在返回 1 ,否则返回 0 。
local res, errs = red:exists(token)
if res == 0 then
local loginfailobj = {code = 500,message = "token is wrong"}
-- local loginfailjson = cjson.encode(loginfailobj)
--ngx.say(loginfailjson)
ngx.say(" token is not exist: <br/>")
end
if res == 1 then
-- local loginfailjson = cjson.encode(loginfailobj)
--ngx.say(loginfailjson)
ngx.say("success lua: <br/>")
end
2.lua位置
/usr/local/nginx-1.4.2/conf
3.nginx配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
lua_package_path "/usr/local/nginx-1.4.2/lua/lib/resty/redis.lua;;";
lua_package_cpath "/usr/local/lib/?.so;;";
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location ~ /download/files/.*?/data {
default_type 'text/html';
access_by_lua_file /usr/local/nginx-1.4.2/conf/access.lua;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
测试