官方描述如下:This module embeds Lua, via the standard Lua 5.1 interpreter or LuaJIT 2.0/2.1, into Nginx and by leveraging Nginx's subrequests, allows the integration of the powerful Lua threads (Lua coroutines) into the Nginx event model.
该模块能扩充nginx功能,配合nginx 的其他module 可以进行各种逻辑操作。
注意使用此模块前提需要在当前环境安装 lua luajit 并设置好相关环境变量信息。
基本例子:
location /recur {
# MIME type determined by default_type:
default_type 'text/plain';
content_by_lua '
local num = tonumber(ngx.var.arg_num) or 0 #将url参数 num 赋予num 如果num不存在则返回0
if num > 50 then ngx.say("num too big")
return
end #如果nux大于50 则response num too big 并结束
ngx.say("num is: ", num)
if num > 0
then res = ngx.location.capture("/recur?num=" .. tostring(num - 1)) #如果num>0 则访问 url。。。。。/recur。。。
ngx.print("status=", res.status, " ") #nginx 打印 访问url的返回状态
ngx.print("body=", res.body)
else
ngx.say("end")
end ';
}
环境的搭建:
1:安装lua luajit 默认就是简单的 make make install。 默认lib 以及驱动文件路径是/usr/local/lib 和 /usr/local/include/luajit-2.0