如何在 nginx 的 upstream 请求之前,在 Lua 代码中动态设置 proxy_http_version

我按照 此 StackOverflow 评论 中提供的方法进行了尝试

location / {
   content_by_lua_block {
      -- 这里有一些逻辑
      if flag then
         return ngx.exec("@http1_0")
      end
      return ngx.exec("@http1_1")
   }
}

location @http1_0 {
   proxy_pass ...;
   proxy_http_version 1.0;
   ...
}

location @http1_1 {
   proxy_pass ...;
   proxy_http_version 1.1;
   ...
}

但我遇到了以下错误:

kong_1 | 2020/11/10 17:19:33 [error] 25#0: 37490 failed to run balancer_by_lua: /usr/local/share/lua/5.1/kong/init.lua:576: attempt to index local 'balancer_data' (a nil value)
kong_1 | stack traceback:
kong_1 |  /usr/local/share/lua/5.1/kong/init.lua:576: in function 'balancer'

我想以编程方式更改 Lua 代码中的代理 HTTP 版本。 有没有什么方法?

点赞