searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

ngx.location.capture()变量继承

2023-06-29 08:05:34
61
0

通过几个例子,简要分析variable与ctx在主请求与子请求中的关系。

 

copy_all_vars & share_all vars

server {
    listen [::]:80;         #https配置-http访问端口v6格式
    listen 80;              #https配置-http访问端口v4格式
    #listen [::]:443 ssl;    #https配置-https访问端口v6格式
    #listen 443 ssl;         #https配置-https访问端口v4格式

    #ssl_certificate              ssl/vsochina.com.crt;
    #ssl_certificate_key           ssl/vsochina.com.key
    #    ssl_certificate conf.d/common/ssl/vsochina.com.crt;
    #ssl_certificate_key conf.d/common/ssl/vsochina.com.key

    server_name www.l.com;

    location /static {
    root /root/resources/;
    }
    
    location /sub {
        content_by_lua_block {
            ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
            ngx.var.dysta = "luoyuwen"
            ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
        }
    }
    
    location /main {
        content_by_lua_block {
            ngx.var.dysta="luoluo"
            ngx.log(ngx.ERR, "main: ", ngx.var.dysta)
            res = ngx.location.capture("/sub", {copy_all_vars=true})
            ngx.log(ngx.ERR, "main: ", ngx.var.dysta)
            res = ngx.location.capture("/sub", {share_all_vars=true})
            ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
        }
    }
}

2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:34):3: main: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:23):2: sub: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:23):4: sub: luoyuwen, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:34):5: main: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:23):2: sub: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:23):4: sub: luoyuwen, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:34):7: sub: luoyuwen, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"

结论:

copy仅赋值,share共享

 

ctx

location /sub {
        content_by_lua_block {
            ngx.log(ngx.ERR, "sub: ", ngx.ctx.foo)
            ngx.ctx.foo = "bar"
            ngx.log(ngx.ERR, "sub: ", ngx.ctx.foo)
        }
    }
    
    location /main {
        content_by_lua_block {
            ngx.ctx.foo = "luoluo"
            local ctx = {}
            ctx.foo = "mm"
            ngx.log(ngx.ERR, "main: ", ngx.ctx.foo)
            res = ngx.location.capture("/sub", {ctx=ctx})
            ngx.log(ngx.ERR, "main: ", ngx.ctx.foo)
            ngx.log(ngx.ERR, "main: ", ctx.foo)
        }
    }

2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:35):5: main: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"
2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:23):2: sub: mm, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:23):4: sub: bar, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:35):7: main: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"
2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:35):8: main: bar, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"

 

结论:
主请求与子请求中的ctx可以赋值并相互影响。
ngx.location.capture("/uri", {ctx_a = ctx_b})
ctx_a为子请求中的ngx.ctx,ctx_b为主请求中的ctx_b, 若该值为ngx.ctx,则为主请求中的ngx.ctx。
首先,将ctx_b赋值给子请求中的ngx.ctx。
子请求中可查询ngx.ctx,其值为主请求的ctx_b
若在子请求中更改ngx.ctx,则会映射到主请求中的ctx_b

 

0条评论
0 / 1000
lucky_lyw
4文章数
0粉丝数
lucky_lyw
4 文章 | 0 粉丝
lucky_lyw
4文章数
0粉丝数
lucky_lyw
4 文章 | 0 粉丝
原创

ngx.location.capture()变量继承

2023-06-29 08:05:34
61
0

通过几个例子,简要分析variable与ctx在主请求与子请求中的关系。

 

copy_all_vars & share_all vars

server {
    listen [::]:80;         #https配置-http访问端口v6格式
    listen 80;              #https配置-http访问端口v4格式
    #listen [::]:443 ssl;    #https配置-https访问端口v6格式
    #listen 443 ssl;         #https配置-https访问端口v4格式

    #ssl_certificate              ssl/vsochina.com.crt;
    #ssl_certificate_key           ssl/vsochina.com.key
    #    ssl_certificate conf.d/common/ssl/vsochina.com.crt;
    #ssl_certificate_key conf.d/common/ssl/vsochina.com.key

    server_name www.l.com;

    location /static {
    root /root/resources/;
    }
    
    location /sub {
        content_by_lua_block {
            ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
            ngx.var.dysta = "luoyuwen"
            ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
        }
    }
    
    location /main {
        content_by_lua_block {
            ngx.var.dysta="luoluo"
            ngx.log(ngx.ERR, "main: ", ngx.var.dysta)
            res = ngx.location.capture("/sub", {copy_all_vars=true})
            ngx.log(ngx.ERR, "main: ", ngx.var.dysta)
            res = ngx.location.capture("/sub", {share_all_vars=true})
            ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
        }
    }
}

2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:34):3: main: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:23):2: sub: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:23):4: sub: luoyuwen, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:34):5: main: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:23):2: sub: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:23):4: sub: luoyuwen, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 14:07:42 [error] 770#0: *6 [lua] content_by_lua(www.l.com.conf:34):7: sub: luoyuwen, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"

结论:

copy仅赋值,share共享

 

ctx

location /sub {
        content_by_lua_block {
            ngx.log(ngx.ERR, "sub: ", ngx.ctx.foo)
            ngx.ctx.foo = "bar"
            ngx.log(ngx.ERR, "sub: ", ngx.ctx.foo)
        }
    }
    
    location /main {
        content_by_lua_block {
            ngx.ctx.foo = "luoluo"
            local ctx = {}
            ctx.foo = "mm"
            ngx.log(ngx.ERR, "main: ", ngx.ctx.foo)
            res = ngx.location.capture("/sub", {ctx=ctx})
            ngx.log(ngx.ERR, "main: ", ngx.ctx.foo)
            ngx.log(ngx.ERR, "main: ", ctx.foo)
        }
    }

2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:35):5: main: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"
2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:23):2: sub: mm, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:23):4: sub: bar, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", subrequest: "/sub", host: "www.l.com"
2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:35):7: main: luoluo, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"
2022/07/12 13:56:11 [error] 770#0: *4 [lua] content_by_lua(www.l.com.conf:35):8: main: bar, client: 127.0.0.1, server: www.l.com, request: "GET /main HTTP/1.1", host: "www.l.com"

 

结论:
主请求与子请求中的ctx可以赋值并相互影响。
ngx.location.capture("/uri", {ctx_a = ctx_b})
ctx_a为子请求中的ngx.ctx,ctx_b为主请求中的ctx_b, 若该值为ngx.ctx,则为主请求中的ngx.ctx。
首先,将ctx_b赋值给子请求中的ngx.ctx。
子请求中可查询ngx.ctx,其值为主请求的ctx_b
若在子请求中更改ngx.ctx,则会映射到主请求中的ctx_b

 

文章来自个人专栏
ccslyw
4 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0