openresty 笔记

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方式


[size=large]ngx.location.capture转发[/size]

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)


[size=large]tcpcopy or capture_multi[/size]

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值