"); //-->
本文分享自天翼云开发者社区《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.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"
专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们
相关推荐
帮个忙:一个网络接口是否能拥有两个IP地址?
8、16位MCU接入TCPIP网络方案
我国西部首个,重庆算力互联互通平台在两江新区上线
INA217的输入稳定网络电路
采用TDA4290的音量调节网络
HTML 解析之 BeautifulSoup
[转帖]IBM可编程网络处理器
网络爬虫开发常用框架Scrapy
Addison.Wesley.Advanced.Linux.Networking.part3.rar
Addison.Wesley.Advanced.Linux.Networking.part4.rar
教程:设置内置逻辑的 IO-Link 网络
Coherent推出全系列InP技术组合
中国首例!我国对美国相关光纤产品发起反规避调查
超高速光纤创下全球速度纪录:在欧式距离上达到1.02拍比特每秒
怎么样才能让一个网络接口同时有两个ip address?
AI网络架构或将进入“无交换机”时代
安捷伦(Agilent)科技公司的网络优化测试仪
Addison.Wesley.Advanced.Linux.Networking.part1.rar
SDN可编程交换芯片架构核心:RMT,一个可编程的网络DSA
INA103构成的使放大器稳定工作的输入网络
IGBT无损吸收网络
Addison.Wesley.Advanced.Linux.Networking.part2.rar
INA166的输入稳定网络电路
GSMA:全球 43% 的人口仍未使用移动互联网
: 影响未来的20项数字技术