【问题标题】:Nginx variable names are lost between OpenResty capturesNginx 变量名称在 OpenResty 捕获之间丢失
【发布时间】:2017-09-28 01:24:06
【问题描述】:

当在 Nginx 中从 server_name 创建一个变量并使用 ngx.location.capture 调用不同的端点时,该变量就消失了。

下面的例子通过调用 testlocalhost 和 acclocalhost 来演示:

server {
    listen 1003;
    server_name ~^(?<name>test|acc)localhost$; #<-Name is set here

    location / {
        #return 200 $name; #This would return the expected test or acc
        content_by_lua 'local options = {
                            method = ngx.HTTP_GET,
                        }
                        local res = ngx.location.capture("/internal", options)
                        ngx.say(res.body)';
    }

    location /internal {   
        return 200 $name; #<- Name is empty here
    }
}

有什么方法可以在不修改body或者使用url参数的情况下维护端点之间的变量?

【问题讨论】:

    标签: nginx lua openresty


    【解决方案1】:

    您需要将选项添加到 ngx.location.capture 以共享或复制所有可用变量。

    https://github.com/openresty/lua-nginx-module#ngxlocationcapture

    copy_all_vars 指定是否复制所有 Nginx 变量 当前请求对相关子请求的值。 修改子请求中的 nginx 变量不会影响 当前(父)请求。该选项最初是在 v0.3.1rc31 发布。

    share_all_vars 指定是否共享所有 Nginx 变量 带有当前(父)请求的子请求。的修改 子请求中的 Nginx 变量会影响当前(父) 要求。启用此选项可能会导致难以调试的问题,因为 坏的副作用,被认为是坏的和有害的。只启用这个 当您完全知道自己在做什么时选择。

        location / {
            #return 200 $name; #This would return the expected test or acc
            content_by_lua 'local options = {
                                method = ngx.HTTP_GET,
                                share_all_vars = true
                            }
                            local res = ngx.location.capture("/internal", options)
                            ngx.say(res.body)';
        }
    

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2010-12-22
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 2012-03-02
      • 2022-01-23
      • 2020-02-09
      • 1970-01-01
      相关资源
      最近更新 更多