openresty 笔记 博客分类: openresty openresty
程序员文章站
2024-02-23 11:17:07
...
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方法
POST方法
自适应参数和method
tcpcopy or capture_multi
只能访问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)
tcpcopy or capture_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)
推荐阅读
-
openresty 笔记 博客分类: openresty openresty
-
spring cloud 学习笔记 博客分类: spring cloud
-
spring cloud 学习笔记 - eureka 博客分类: spring cloud
-
openresty 笔记 博客分类: openresty openresty
-
OpenResty与Nginx学习配置记录
-
RSpec笔记 - let 和 let! 博客分类: 测试Ruby on Rails RSpecRuby on Rails
-
《产品经理实战手册》笔记一 博客分类: Agile/PM 情感活动工作
-
TCP-IP详解笔记1.5 RARP:逆地址解析协议 博客分类: Database 网络协议配置管理
-
CLRS笔记11,Hash Table 博客分类: Infrastructure MySQL数据结构.netHTMLBlog
-
CLRS笔记15,动态规划 博客分类: Infrastructure 算法Cache