nginx.conf配置
lua_code_cache off;client_body_buffer_size 32K;client_max_body_size 32K;location = /public_api { internal; content_by_lua_file lualib/lua001/common.lua; }location = /app01 { content_by_lua_file lualib/lua001/app01.lua; }案例1.ngx.location.capture的参数之args和body及method**method 指定子请求的请求方法, 只接受类似 ngx.HTTP_POST 的常量。body 指定子请求的请求体 (仅接受字符串值)。args 指定子请求的 URI 请求参数 (可以是字符串或者 Lua 表)。**lualib/lua001/app01.lua--学习body&args--ngx.location.capture 返回res.status res.body res.header res.truncatedres = ngx.location.capture("/public_api",{method=ngx.HTTP_GET,body="name=zzj&age=33&name=badboy",args={arg_a=5,arg_b=4}})for key,val in pairs(res) do if type(val) == "table" then ngx.say(key,"=>",table.concat(val,",")) else ngx.say(key,"=>",val) endendlualib/lua001/common.lua-- 获取get传递的值local args = ngx.req.get_uri_args()for key,val in pairs(args) do if type(val) == "table" then ngx.say(key,":",table.concat(val,",")) end ngx.say(key,":",val)end--获取post传递的值ngx.req.read_body()local args, err = ngx.req.get_post_args()if not args then ngx.say("failed to get post args: ", err) returnendfor key, val in pairs(args) do if type(val) == "table" then ngx.say(key, ": ", table.concat(val, ", ")) else ngx.say(key, ": ", val) endend访问结果:[root@tengine_lua ~]# curl http://192.167.14.56:8080/app01status=>200body=>arg_b:4arg_a:5age: 33name: zzj, badboyheader=>truncated=>false案例2.ngx.location.capture的参数之vars**vars 用一个 Lua 表设置子请求中的 Nginx 变量值。**向子请求中传递nginx变量,需要在nginx.conf提前设置好变量location = /app01 { set $name ""; set $age ""; content_by_lua_file lualib/lua001/app01.lua; }或者使用ngx.var.name="zzj" ngx.var.age=34lualib/lua001/app01.lua--学习vars--ngx.var.name="badboy"--ngx.var.age=34local res = ngx.location.capture("/public_api",{vars={name="zzj",age=32}})--local res = ngx.location.capture("/public_api",{vars={name=ngx.var.name,age=ngx.var.age}})for key,val in pairs(res) do if type(val) == 'table' then ngx.say(key,"=>",table.concat(val,",")) else ngx.say(key,"=>",val) endendlualib/lua001/common.lua--测试varsngx.say("name:",ngx.var.name)ngx.say("age:",ngx.var.age)测试结果:[root@tengine_lua ~]# curl http://192.167.14.56:8080/app01status=>200body=>name:zzjage:32header=>truncated=>false案例3.ngx.location.capture的参数之ctx**ctx 指定一个 Lua 表作为子请求的 ngx.ctx 表,可以是当前请求的 ngx.ctx 表。这种方式可以让父请求和子请求共享完全相同的上下文环境。**lualib/lua001/app01.lua--学习ctxngx.say("First")local ctx={}--local res = ngx.location.capture("/public_api",{ctx=ctx})local res = ngx.location.capture("/public_api",{ctx=ngx.ctx})for key,val in pairs(res) do if type(val) == "table" then ngx.say(key,":",table.concat(val,",")) else ngx.say(key,":",val) endendngx.say("second")for key,val in pairs(ctx) do if type(val) == "table" then ngx.say(key,":",table.concat(val,",")) else ngx.say(key,":",val) endendngx.say("third")for key,val in pairs(ngx.ctx) do if type(val) == "table" then ngx.say(key,":",table.concat(val,",")) else ngx.say(key,":",val) endendlualib/lua001/common.lua--测试ctxngx.ctx.foo="Sub foo"ngx.say("ctx study")测试结果:[root@tengine_lua ~]# curl http://192.167.14.56:8080/app01Firststatus:200body:ctx studyheader:truncated:falsesecondthirdfoo:Sub foo案例4.ngx.location.capture的参数之share_all_vars**copy_all_vars 设置是否复制所有当前请求的 Nginx 变量值到子请求中,修改子请求的 nginx 变量值不影响当前 (父) 请求**lualib/lua001/app01.lua--学习copy_all_varsngx.var.name="zzj"local res = ngx.location.capture("/public_api",{copy_all_vars=true})for key,val in pairs(res) do if type(val) == "table" then ngx.say(key,":",table.concat(val,",")) else ngx.say(key,":",val) endendngx.say("main request of name:"..ngx.var.name)lualib/lua001/common.lua--测试copy_all_varsngx.var.name="badboy"ngx.say("sub request of name:"..ngx.var.name)测试结果:[root@tengine_lua ~]# curl http://192.167.14.56:8080/app01status:200body:sub request of name:badboyheader:truncated:falsemain request of name:zzj如果想了解更多,请关注我们的公众号公众号ID:opdevos扫码关注