尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

ngx.location.capture()变量继承

ngx.location.capture()变量继承
📅 发布时间:2026/6/18 8:33:01

本文分享自天翼云开发者社区《ngx.location.capture()变量继承》.作者:lucky_lyw

通过几个例子,简要分析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.keyserver_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

相关新闻

  • 完整教程:爱发电nginx转发企业微信webhook
  • 2025 年安规测试仪源头厂家最新推荐排行榜:综合耐压电气等多类型设备品牌深度测评与靠谱厂家筛选三项/新能源/光伏安规测试仪公司推荐
  • 2025 年最新推荐钢花管源头厂家排行榜:聚焦高强度耐腐蚀环保型产品,精选五大靠谱品牌实测推荐地质钢花管/桩基钢花管/R780 地质钢花管公司推荐

最新新闻

  • STM8L15x开发板实测DS18B20温度采集工程(IAR环境,含完整驱动与调试脚本)
  • kafka源码-@KafkaListener消费端的poll调用逻辑
  • 3分钟学会:Windows上最轻量的安卓APK安装工具完全指南
  • OA与CMS系统漏洞挖掘:从权限边界突破到实战提权
  • TC820双斜积分ADC:从原理到3位半数字电压表设计实战
  • 豆包智能感从何而来:五层能力涌现机制解析

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号