Lua 香港API网关v0.11.0上游url

Lua 香港API网关v0.11.0上游url,lua,openresty,kong,Lua,Openresty,Kong,我们正试图建立一个依赖于请求头的插件,将其代理到特定主机。前 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 在早期版本(

我们正试图建立一个依赖于请求头的插件,将其代理到特定主机。前

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
在早期版本( 这是因为ngx.ctx.upstream\u url重写了代理传递行为

因为我们想在k8s环境中使用它,所以我们不得不使用0.11.0版本,因为他们已经修复了一些关于dns的问题。问题似乎是他们通过ngx.var.upstream\u uri更改了ngx.ctx.upstream\u url,但行为不同,它不会更改主机以代理我们的请求。这是我们得到的错误:

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"
有人有同样的问题吗?我们的问题还有别的解决办法吗


提前非常感谢。

如果有人对这个问题感兴趣,我就是这样解决的

最后,我通过“主机”头进行了重定向,我在插件中所做的是更改头以适应其他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
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
最重要的一点是,您应该在handler.lua文件中使用重写上下文,而不是访问上下文:

function ContextRedirectHandler:rewrite(conf)

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

end
然后,您可以更改access.lua文件中的“主机”头,如下所示:

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
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