gpt4 book ai didi

lua - Kong API网关 v0.11.0 upload_url

转载 作者:行者123 更新时间:2023-12-02 08:21:24 25 4
gpt4 key购买 nike

我们正在尝试设置一个依赖于请求 header 的插件,将其代理到特定主机。例如。

curl -H 'Env: foo' http://127.0.0.1:8000/poc -> https://foo.example.com/poc
curl -H 'Env: bar' http://127.0.0.1:8000/poc -> https://bar.example.com/poc

在早期版本(< v0.11.0)中,以下代码有效(这是我们的插件的 access.lua 文件):

local singletons = require "kong.singletons"
local responses = require "kong.tools.responses"

local _M = {}

function _M.execute(conf)
local environment = ngx.req.get_headers()['Env']

if environment then
local result, err = singletons.dao.environments:find_all {environment = environment}


if err then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
else
ngx.ctx.upstream_url = result[1].proxy_redirect
end

end
end

return _M

由于 ngx.ctx.upstream_url 覆盖了 proxy_pass 行为,因此该方法有效。

由于我们想在 k8s 环境中使用它,因此我们必须使用 0.11.0 版本,因为他们修复了一些有关 dns 的问题。问题似乎是他们已将 ngx.ctx.upstream_url 更改为 ngx.var.upstream_uri,但行为并不相同,它不会将主机更改为代理我们的要求。这是我们得到的错误:

2017/08/23 11:28:51 [error] 22#0: *13 invalid port in upstream "kong_upstreamhttps://foo.example.com", client: 192.168.64.1, server: kong, request: "GET /poc HTTP/1.1", host: "localhost:8000"

有人有同样的问题吗?还有其他方法可以解决我们的问题吗?

提前非常感谢您。

最佳答案

如果有人对此感兴趣,这就是我解决此问题的方法。

最后,我通过“Host” header 进行了重定向,我在插件中所做的是更改 header 以适应其他 api。我的意思是:

我创建了 2 个 API:

curl -H 'Host: foo' http://127.0.0.1:8000/ -> https://foo.example.com
curl -H 'Host: bar' http://127.0.0.1:8000/ -> https://bar.example.com

我的插件的行为应该是这样的:

curl -H 'Host: bar' -H 'Env: foo' http://127.0.0.1:8000/poc -> https://foo.example.com/poc
curl -H 'Host: foo' -H 'Env: bar' http://127.0.0.1:8000/poc -> https://bar.example.com/poc

最重要的想法是您应该在 handler.lua 文件中使用重写上下文而不是访问上下文:

function ContextRedirectHandler:rewrite(conf)

ContextRedirectHandler.super.rewrite(self)
access.execute(conf)

end

然后,您可以在 access.lua 文件中更改“Host” header ,如下所示:

local singletons = require "kong.singletons"
local responses = require "kong.tools.responses"

local _M = {}

function _M.execute(conf)
local environment = ngx.req.get_headers()['Env']

if environment then
local result, err = singletons.dao.environments:find_all {environment = environment}

if err then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
else
ngx.req.set_header("Host", result[1].host_header_redirect)
end

end
end

return _M

关于lua - Kong API网关 v0.11.0 upload_url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45839634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com