美文网首页
OpenResty中的子查询

OpenResty中的子查询

作者: Uzero | 来源:发表于2017-02-07 14:14 被阅读0次

Nginx 子请求是一种非常强有力的方式,它可以发起非阻塞的内部请求访问目标 location。目标 location 可以是配置文件中其他文件目录,或任何其他 nginx C 模块,需要注意的是,子请求只是模拟 HTTP 接口的形式,没有额外的 HTTP/TCP 流量,也没有IPC (进程间通信) 调用。所有工作在内部高效地在 C 语言级别完成。

原型:

local res = ngx.location.capture(uri)

返回一个包含四个元素的 Lua 表 (res.status,res.header,res.body, 和res.truncated)。

res.status(状态) 保存子请求的响应状态码。

res.header(头) 用一个标准 Lua 表储子请求响应的所有头信息。如果是“多值”响应头,这些值将使用 Lua (数组) 表顺序存储。

res.body(体) 保存子请求的响应体数据,它可能被截断。用户需要检测res.truncated(截断) 布尔值标记来判断

模拟get请求

local res = ngx.location.capture(

        '/foo?a=1',

        { args = { b = 3, c = 'a' } }

)

等同于

local res = ngx.location.capture('/foo?a=1&b=3&c=a')

foo.lua代码:

ngx.say("par_1=", ngx.var.b)

ngx.say("par_2=", ngx.var.c)

--do  some things ...

模拟post请求

do.lua代码:

local res = ngx.location.capture(

         '/bar',

         { 

                 method = ngx.HTTP_POST,

                 body = 'hello, world'

         }

)

bar.lua代码:

--do  some things ...

模拟post请求发送json字符串

easy.lua代码:

local res_userinfo = ngx.location.capture(

          '/dd',

          {

                 method = ngx.HTTP_POST,

                 body = cjson.encode( {

                            time   = 'time' ,

                            appid = 'appid' ,

                            sid     = 'sid' ,

                            from  = 'from' ,                                                    

                           page  = 'page'

                      })

          }

)

dd.lua代码:

ngx.req.read_body()

local json_str = ngx.req.get_body_data()

local arr      = comm:json_decode(json_str)

ngx.say(arr['sid'])

--do  some things ...

ngx.location.capture_multi用法差不多

local  res1, res2, res3=ngx.location.capture_multi{   

        {"/foo", { args="a=3&b=4"} },   

        {"/bar"},    

        {"/baz", { method=ngx.HTTP_POST, body="hello"} }

}

if res1.status == ngx.HTTP_OK

then

       ...

end

if res2.body == "BLAH"

then

       ...

end

...

相关文章

  • OpenResty中的子查询

    Nginx 子请求是一种非常强有力的方式,它可以发起非阻塞的内部请求访问目标 location。目标 locati...

  • sql语句

    sql中in和exist的区别: 1、in先子查询,后主查询 2、exist先主查询,后子查询。子查询中,如果结果...

  • 《mysql必知必会》读书实战笔记14-子查询

    第14章 使用子查询 14.1子查询 简单查询:查询单个数据表的select查询语句。 子查询:嵌套在其他查询中的...

  • mysql 子查询

    子查询指的是嵌套在查询中的查询

  • 2018-06-04

    第11章 子查询 11.1 子查询 SQL 允许创建子查询(subquery),即嵌套在其他查询中的查询。 11....

  • 子查询

    嵌套在其他查询中的查询叫子查询 使用到 WHERE 子句中时,子查询必须用括号括起来。 子查询在 SELECT 子...

  • T-SQL基础(三)之子查询与表表达式

    子查询 在嵌套查询中,最外面查询结果集返回给调用方,称为外部查询。嵌套在外部查询内的查询称为子查询,子查询的结果集...

  • 17/12/11高级子查询

    17/12/11高级子查询 嵌套子查询:在通常的子查询中,子查询是以嵌套的方式写在父查询的WHERE、HAVING...

  • 5.2 子查询 连结

    子查询 嵌套在其它查询中的查询 子查询总是从内向外处理。 作为计算字段使用子查询。 Select name , s...

  • 子查询和联结表

    子查询 子查询即嵌套在其它查询中的查询。 利用子查询进行过滤 下面是两个单独的查询语句: 上面两个子查询进行组合:...

网友评论

      本文标题:OpenResty中的子查询

      本文链接:https://www.haomeiwen.com/subject/sssrbttx.html