openresty 笔记

byourb 2018-08-13

ngx.exec转发

只能访问nginx内部资源,而且是get方式,==rewrite

调用方法:

ngx.exec("/data/new_horizontal_overview1.jpg");

ngx.exec("/tomcat/test",'name=lizw&age=19790825');

ngx.exec("/tomcat/test",{name=lizw,age="19790825"});

注意,都是get方式

ngx.location.capture转发

GET方法

local request_method = ngx.var.request_method
local args = nil
 
 
--获取参数的值
if "GET" == request_method then
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.req.read_body()
    args = ngx.req.get_post_args()
end
 
--转发请求(仅限nginx内部有效)
a=args["a"]
b=args["b"]
local res = nil

--method get
res=ngx.location.capture("/tomcat/test", {args={a=a, b=b}})

ngx.say("status:", res.status, " response:", res.body)

POST方法

local request_method = ngx.var.request_method
local args = nil
 
 
--获取参数的值
if "GET" == request_method then
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.req.read_body()
    args = ngx.req.get_post_args()
end
 
--转发请求(仅限nginx内部有效)
a=args["a"]
b=args["b"]
local res = nil

--method post
--不用写参数, 参数在body里
res=ngx.location.capture("/tomcat/test", { method = ngx.HTTP_POST)

ngx.say("status:", res.status, " response:", res.body)

自适应参数和method

local request_method = ngx.var.request_method
local args = nil
 
 
--获取参数的值
if "GET" == request_method then
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.req.read_body()
    args = ngx.req.get_post_args()
end
 
--转发请求(仅限nginx内部有效)
a=args["a"]
b=args["b"]
local res = nil

--自动化
local method=nil
if "GET" == request_method then
    method = ngx.HTTP_GET
    res=ngx.location.capture("/tomcat/test", {method = method, args = ngx.req.get_uri_args()})
elseif "POST" == request_method then
    method = ngx.HTTP_POST
    res=ngx.location.capture("/tomcat/test", {method = method})
    --参数随着body转过去了, 所以这里不需要 args = ...
end

ngx.say("status:", res.status, " response:", res.body)

tcpcopyorcapture_multi

local request_method = ngx.var.request_method  
local args = nil  
   
ngx.log(ngx.ERR, "start");

--获取参数的值  
if "GET" == request_method then  
    args = ngx.req.get_uri_args()  
elseif "POST" == request_method then  
    ngx.req.read_body()  
    args = ngx.req.get_post_args()  
end  
   
--转发请求(仅限nginx内部有效)  
local res = nil  

ngx.log(ngx.ERR, "doing");

local uri = ngx.var.uri 
ngx.log(ngx.ERR, string.format("获取当前请求的url %s",uri))

--自动化  
local method=nil  
if "GET" == request_method then  
    method = ngx.HTTP_GET  
    res1,res2=ngx.location.capture_multi({
        {"/tomcat/test", {method = method, args = ngx.req.get_uri_args()}},
        {"/tomcat7/test", {method = method, args = ngx.req.get_uri_args()}}
    })  
elseif "POST" == request_method then  
    method = ngx.HTTP_POST  
    --post的参数在body里, 所以这里不用指定, 下游解body时自动获取
    res1,res2=ngx.location.capture_multi({
        {"/tomcat/test", {method = method}},
        {"/tomcat7/test", {method = method}}
    }) 
end

ngx.log(ngx.ERR, string.format("doing2, method: %s",method))

ngx.log(ngx.ERR, "end")

return ngx.say("status:", res1.status, " response:", res1.body)

相关推荐

yyzhu / 0评论 2015-11-01