【问题标题】:Kong API gateway v0.11.0 upstream_urlKong API 网关 v0.11.0 upstream_url
【发布时间】:2017-08-23 12:23:46
【问题描述】:

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

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

在早期版本 (

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"

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

非常感谢您。

【问题讨论】:

    标签: lua openresty kong


    【解决方案1】:

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

    最后,我通过“Host”标头进行了重定向,我在插件中所做的是更改标头以适应其他 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”标头:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-31
      • 2020-08-25
      • 1970-01-01
      • 2019-02-08
      • 2019-02-17
      • 2019-10-26
      • 2017-01-14
      • 1970-01-01
      相关资源
      最近更新 更多