本文分享自天翼雲開發者社區《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