Chinaunix首页 | 论坛 | 博客
  • 博客访问: 551976
  • 博文数量: 911
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 7395
  • 用 户 组: 普通用户
  • 注册时间: 2022-03-07 15:28
个人简介

天翼云是中国电信倾力打造的云服务品牌,致力于成为领先的云计算服务提供商。提供云主机、CDN、云电脑、大数据及AI等全线产品和场景化解决方案。

文章分类

全部博文(911)

文章存档

2026年(14)

2025年(202)

2024年(244)

2023年(247)

2022年(202)

我的朋友

分类: 网络与安全

2025-12-29 16:56:54

本文分享自天翼云开发者社区《 ngx.location.capture()变量继承》.作者:lucky_lyw
通过几个例子,简要分析variable与ctx在主请求与子请求中的关系。
copy_all_vars & share_all vars

点击(此处)折叠或打开

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

  6.     #ssl_certificate ssl/vsochina.com.crt;
  7.     #ssl_certificate_key ssl/vsochina.com.key
  8.     # ssl_certificate conf.d/common/ssl/vsochina.com.crt;
  9.     #ssl_certificate_key conf.d/common/ssl/vsochina.com.key

  10.     server_name www.l.com;

  11.     location /static {
  12.     root /root/resources/;
  13.     }
  14.     
  15.     location /sub {
  16.         content_by_lua_block {
  17.             ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
  18.             ngx.var.dysta = "luoyuwen"
  19.             ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
  20.         }
  21.     }
  22.     
  23.     location /main {
  24.         content_by_lua_block {
  25.             ngx.var.dysta="luoluo"
  26.             ngx.log(ngx.ERR, "main: ", ngx.var.dysta)
  27.             res = ngx.location.capture("/sub", {copy_all_vars=true})
  28.             ngx.log(ngx.ERR, "main: ", ngx.var.dysta)
  29.             res = ngx.location.capture("/sub", {share_all_vars=true})
  30.             ngx.log(ngx.ERR, "sub: ", ngx.var.dysta)
  31.         }
  32.     }
  33. }

  34. 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: ""
  35. 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: ""
  36. 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: ""
  37. 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: ""
  38. 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: ""
  39. 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: ""
  40. 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: ""

结论:

copy仅赋值,share共享

ctx

点击(此处)折叠或打开

  1. location /sub {
  2.         content_by_lua_block {
  3.             ngx.log(ngx.ERR, "sub: ", ngx.ctx.foo)
  4.             ngx.ctx.foo = "bar"
  5.             ngx.log(ngx.ERR, "sub: ", ngx.ctx.foo)
  6.         }
  7.     }
  8.     
  9.     location /main {
  10.         content_by_lua_block {
  11.             ngx.ctx.foo = "luoluo"
  12.             local ctx = {}
  13.             ctx.foo = "mm"
  14.             ngx.log(ngx.ERR, "main: ", ngx.ctx.foo)
  15.             res = ngx.location.capture("/sub", {ctx=ctx})
  16.             ngx.log(ngx.ERR, "main: ", ngx.ctx.foo)
  17.             ngx.log(ngx.ERR, "main: ", ctx.foo)
  18.         }
  19.     }

  20. 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: ""
  21. 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: ""
  22. 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: ""
  23. 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: ""
  24. 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: ""
结论:
主请求与子请求中的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


阅读(54) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~