0

I'm trying to add "client_ip" in to a response header, but I can see the IP address is being printed on the kong apigateway logs but cannot forward it to a response header,

Sample log output:

,"method":"GET"},"client_ip":"49.36.22.209","tries":[{"balancer

I was trying following methods to try it out, but still response header is not printed the ip address.

- name: response-transformer
  route: routeName
  config:
    add:
      headers:
      - X-Real-IP:${{client_ip}}

Can anyone help me to try enable this header on kong apigateway configs ?

Thanks.

1 Answer 1

1

You could use the plugin "serverless-functions"

In your case you would use the "post-function" running on service response at "header-phase".

With this plugin and post-function you can write custom logic with lua and modify the response.
With pre-function you could modify the request.
Kong have a PDK you can use globally.

Either if lb or not you would use kong.client.get_ip() or kong.client.get_forwarded_ip()

Example Code

local client = kong.client
local response = kong.response

local function set_client_ip_header()
  local client_ip = client.get_ip() -- or client.get_forwarded_ip()
  response.set_header("X-Real-Ip", client_ip)
end

return set_client_ip_header -- return for memoization

Not the answer you're looking for? Browse other questions tagged or ask your own question.