nginx -请求周期变量ngx.ctx

一. 简述:

    在 OpenResty 的体系中,可以通过共享内存的方式完成不同工作进程的数据共享,可以通过 Lua 模块方式完成单个进程内不同请求的数据共享。ngx.ctx 表就是为了解决这类问题而设计的。

二. 关于ngx.ctx

   ngx.ctx 是一个表,所以我们可以对他添加、修改。它用来存储基于请求的 Lua 环境数据,其生存周期与当前请求相同 (类似 Nginx 变量)。它有一个最重要的特性:单个请求内的 rewrite (重写),access (访问),和 content (内容) 等各处理阶段是保持一致的。

每个请求,包括子请求,都有一份自己的 ngx.ctx 表,请求结束,ngx.ctx表会被销毁。

eg:

 location /sub {
     content_by_lua_block {
         ngx.say("sub pre: ", ngx.ctx.blah)
         ngx.ctx.blah = 32
         ngx.say("sub post: ", ngx.ctx.blah)
     }
 }

 location /main {
     content_by_lua_block {
         ngx.ctx.blah = 73
         ngx.say("main pre: ", ngx.ctx.blah)
         local res = ngx.location.capture("/sub")
         ngx.print(res.body)
         ngx.say("main post: ", ngx.ctx.blah)
     }
 }

-------- output
-- 访问 get /main
 main pre: 73
 sub pre: nil
 sub post: 32
 main post: 73

任意数据值,包括 Lua 闭包与嵌套表,都可以被插入ngx.ctx表中, 也可以将 n

/opt/nginx-sticky-module/ngx_http_sticky_misc.c: 在函数‘ngx_http_sticky_misc_sha1’中: /opt/nginx-sticky-module/ngx_http_sticky_misc.c:176:15: 错误:‘SHA_DIGEST_LENGTH’未声明(在此函数内第一次使用) u_char hash[SHA_DIGEST_LENGTH]; ^ /opt/nginx-sticky-module/ngx_http_sticky_misc.c:176:15: 附注:每个未声明的标识符在其出现的函数内只报告一次 /opt/nginx-sticky-module/ngx_http_sticky_misc.c:176:10: 错误:未使用的变量‘hash’ [-Werror=unused-variable] u_char hash[SHA_DIGEST_LENGTH]; ^ /opt/nginx-sticky-module/ngx_http_sticky_misc.c: 在函数‘ngx_http_sticky_misc_hmac_sha1’中: /opt/nginx-sticky-module/ngx_http_sticky_misc.c:242:15: 错误:‘SHA_DIGEST_LENGTH’未声明(在此函数内第一次使用) u_char hash[SHA_DIGEST_LENGTH]; ^ /opt/nginx-sticky-module/ngx_http_sticky_misc.c:243:12: 错误:‘SHA_CBLOCK’未声明(在此函数内第一次使用) u_char k[SHA_CBLOCK]; ^ /opt/nginx-sticky-module/ngx_http_sticky_misc.c:243:10: 错误:未使用的变量‘k’ [-Werror=unused-variable] u_char k[SHA_CBLOCK]; ^ /opt/nginx-sticky-module/ngx_http_sticky_misc.c:242:10: 错误:未使用的变量‘hash’ [-Werror=unused-variable] u_char hash[SHA_DIGEST_LENGTH]; ^ /opt/nginx-sticky-module/ngx_http_sticky_misc.c: 在函数‘ngx_http_sticky_misc_text_raw’中: /opt/nginx-sticky-module/ngx_http_sticky_misc.c:324:5: 错误:传递‘ngx_sock_ntop’的第 1 个参数时将整数赋给指针,未作类型转换 [-Werror] digest->len = ngx_sock_ntop(1); ^ In file included from src/core/ngx_core.h:82:0, from /opt/nginx-sticky-module/ngx_http_sticky_misc.c:8: src/core/ngx_inet.h:114:8: 附注:需要类型‘struct sockaddr *’,但实参的类型为‘int’ size_t ngx_sock_ntop(struct sockaddr *sa, socklen_t socklen, u_char *text, ^ /opt/nginx-sticky-module/ngx_http_sticky_misc.c:324:5: 错误:提供给函数‘ngx_sock_ntop’的实参太少 digest->len = ngx_sock_ntop(1); ^
最新发布
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值