ngx lua模块之ngx.location.capture子请

2019-04-16  本文已影响0人  金星show

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.truncated

res = 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)
    end
end

lualib/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)
 return
end
for key, val in pairs(args) do
 if type(val) == "table" then
     ngx.say(key, ": ", table.concat(val, ", "))
 else
     ngx.say(key, ": ", val)
 end
end

访问结果:

[root@tengine_lua ~]# curl http://192.167.14.56:8080/app01
status=>200
body=>arg_b:4
arg_a:5
age: 33
name: zzj, badboy

header=>
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=34

lualib/lua001/app01.lua
--学习vars

--ngx.var.name="badboy"
--ngx.var.age=34
local 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)    
    end
end

lualib/lua001/common.lua
--测试vars

ngx.say("name:",ngx.var.name)
ngx.say("age:",ngx.var.age)

测试结果:

[root@tengine_lua ~]# curl http://192.167.14.56:8080/app01
status=>200
body=>name:zzj
age:32

header=>
truncated=>false

案例3.ngx.location.capture的参数之ctx

ctx 指定一个 Lua 表作为子请求的 ngx.ctx 表,可以是当前请求的 ngx.ctx 表。这种方式可以让父请求和子请求共享完全相同的上下文环境。
lualib/lua001/app01.lua
--学习ctx

ngx.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)
    end
end

ngx.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)
    end
end

ngx.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)
    end
end

lualib/lua001/common.lua
--测试ctx

ngx.ctx.foo="Sub foo"
ngx.say("ctx study")

测试结果:

[root@tengine_lua ~]# curl http://192.167.14.56:8080/app01
First
status:200
body:ctx study

header:
truncated:false
second
third
foo:Sub foo

案例4.ngx.location.capture的参数之share_all_vars

copy_all_vars 设置是否复制所有当前请求的 Nginx 变量值到子请求中,修改子请求的 nginx 变量值不影响当前 (父) 请求
lualib/lua001/app01.lua
--学习copy_all_vars

ngx.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)
    end
end

ngx.say("main request of name:"..ngx.var.name)

lualib/lua001/common.lua
--测试copy_all_vars

ngx.var.name="badboy"
ngx.say("sub request of name:"..ngx.var.name)

测试结果:

[root@tengine_lua ~]# curl http://192.167.14.56:8080/app01
status:200
body:sub request of name:badboy

header:
truncated:false
main request of name:zzj
上一篇下一篇

猜你喜欢

热点阅读