Openresty在不同请求间进行数据共享

398次阅读
没有评论

共计 454 个字符,预计需要花费 2 分钟才能阅读完成。

Openresty在不同请求间进行数据共享

前面博主试了用ngx.ctx在一个请求的多个处理阶段内进行数据共享,而此处则讲下如何在不同请求间进行数据共享

要想在不同请求间进行数据共享,则需要在本请求内定义一个名为ctx 的table 作为参数传递给子请求

    location /sub {
        content_by_lua_block {
            ngx.ctx.foo = "bar";
        }
     }
     location /lua {
         content_by_lua_block {
             local ctx = {}
             res = ngx.location.capture("/sub", { ctx = ctx })
             ngx.say(ctx.foo);
             ngx.say(ngx.ctx.foo);
         }
     }

测试输出

[root@nginx-cluster conf.d]# curl 127.0.0.1:8084/lua2
bar
nil

另外一种写法是

    location /lua2 {
        content_by_lua_block {
            res = ngx.location.capture("/sub", { ctx = ngx.ctx })
            ngx.say(ngx.ctx.foo);
        }
    }

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2021-07-18发表,共计454字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)