ngx lua模塊之ngx.location.capture子請求學習

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


如果想了解更多,請關注我們的公衆號
公衆號ID:opdevos
掃碼關注

gongzhouhao.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章