`

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


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)  


分享到:
评论

相关推荐

    lua-nginx-redis:Redis,Lua,Nginx,OpenResty笔记和资料

    Nginx与Lua编写脚本的基本内置块是指令执行顺序的图 Nginx教程 基础 案例 模块 好文 流媒体 其他 Lua教程 Redis教程 Openresty教程 Linux教程 壳牌 微信公众号

    linux-RedisLuaNginxOpenResty笔记

    Redis、Lua、Nginx、OpenResty笔记

    Nginx模块开发OpenResty简单使用笔记整理.zip

    Nginx模块开发OpenResty简单使用笔记整理 ### Nginx简介 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中。与Apache相比。 同时,大量的第三方扩展模块也令...

    openresty-debian:OpenResty Debian软件包生成器

    ##笔记 所有文件都放在标准的debian nginx位置 包括init,logrotate以及官方nginx软件包中的post / pre-install / remove脚本。 默认情况下为Ubuntu Trusty构建。 LuaJIT从2.1 Alpha升级到2.1 Beta 1 包括...

    lightpath-nginx:使用Openresty和Redis用Lua编写的CDN

    LightPath CDN Nginx模块版本:1.0.0-beta描述CDN,内容交付网络,使用Openresty(Nginx)用Lua编写。 网站配置(后端,缓存规则,边缘规则等)存储在Redis中。 如果有兴趣,我会在以后添加适当的文档。 该项目之...

    nginx课程笔记文件.zip

    其中有nginx怎样配置负载均衡,图片服务器,资源压缩,黑白名单限制,websocket反向代理,rewrite重写规则,服务器缓存设置,ssl证书配置,keepalive部署nginx集群,openResty部分介绍(漏桶算法流程图)等

    openresty-docker:使用 Docker 进行 OpenrestyLua 开发的 Dockerfile 和示例设置

    开始使用 docker 作为 Lua Web 应用程序服务器进行 Lua Web 开发 在这篇博文中,我将引导您通过使用容器技术软件来开始 Lua Web 开发。 为什么是 ? 从他们的网页: Docker 是一个开源项目,可以从任何应用程序...

    安卓毕业设计app源码-WikiNotes:我的笔记

    OpenResty FrontEnd AJAX VueJS NodeJS OpenSource Celery jieba NLTK webcollector 开源的框架学习。 Algorithm 数据结构、算法、设计模式等。 Intelligence Big_Data 大数据相关。 Data-Analysis 数据分析。 Web-...

    leetcode题库-blog:博客

    笔记 Kubernetes 使用 Kubernetes 开发 Kubernetes 源码分析 Docker Docker 原理相关 Bash Linux Linux gawk 由于 gawk 语言太过强大,想了想还是把它单独拎出来说。 关于 gawk 与 awk 的区别:gawk(GNU Awk) 是 ...

    java中solr的笔试题-note:个人学习信息

    个人学习笔记 python nginx AWS认证考试 技术路线 LB负载均衡 负载均衡常见使用场景,问题定位,特别是对于概率性出现的访问超时问题定位。包含如下组件 nginx lvs openresty kong apigateway 消息中间件 对于消息...

    用apisix 做一个api key web 管理和api key 权限和访问级别控制

    Apache APISIX是一个高性能、可扩展、云原生的API网关,它基于Nginx和OpenResty构建,具有灵活的插件机制和易于扩展的架构。 Apache APISIX提供了丰富的功能,包括负载均衡、路由、限流、熔断、重试、安全认证等,...

Global site tag (gtag.js) - Google Analytics