Kong API Gateway v0.11.0 upstream_url

We are trying to configure a plugin, which depends on the request headers, the proxy server on a specific host. Example.

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

In earlier versions (<v0.11.0), the following code worked (this is our plugin access.lua file):

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

This worked because ngx.ctx.upstream_url overwrite the proxy_pass behavior.

As we want to use it in k8s environment, we had to use version 0.11.0, because they fixed some problems related to dns. It seems that the problem was that they changed ngx.ctx.upstream_url to ngx.var.upstream_uri , but this is not the same thing, it does not change the host to the proxy server our request. This is the error we get:

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"

- ? ?

.

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

"Host" 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.req.set_header("Host", result[1].host_header_redirect)
    end

  end
end

return _M
+2

Source: https://habr.com/ru/post/1684297/


All Articles