Kong ingress - Rewrite path on ingress


I was wondering if there was a way, using the kong ingress controller in kubernetes, of rewriting the path of a request at the ingress level.

I saw that you can use the “route.proxy” in the KongIngress object to change the path of a request, for example:

example.com/foo -> example.com/bar

But that only works when the KongIngress is attached to a service.

In my case, i have multiple Ingresses that have the same Service as a backend, and i’m trying to rewrite the path on each ingress to another path:

  • Ingress 1: /foo -> service/bar
  • Ingress 2: /somepath -> service/anotherpath

I tried using the url-rewrite plugin but couldn’t make it work.

Is there some feature or plugin that i’m missing, or there is no support to do this?


(Extra info:
I am using Kong version 1.3.0 in DBless mode, and Kong ingress controller version 0.5.0)

You can use https://docs.konghq.com/hub/kong-inc/request-transformer/ to accomplish this. There is an undocumented property config.replace.uri where you can set the rewrite rule.

I had the exact same question earlier today:

Unfortunately it does not seem to take uri captures into account so the request-transformer plugin can only do static replacement of uris

EDIT: After further investigation this is possible. The following manifests does the trick of having multiple routes to one service with the path rewritten

apiVersion: extensions/v1beta1
kind: Ingress
  name: docs
    plugins.konghq.com: apidocs-rewrite
    kubernetes.io/ingress.class: "kong"
  - http:
      - path: /api-docs/demo/(.*)
          serviceName: demo-service
          servicePort: http
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
  name: apidocs-rewrite
    uri: "/api-docs/$(uri_captures[1])"
plugin: request-transformer

Thank you for the response!

I will try using the request-transformer plugin then

Hey @goober, thanks for the post. Do you mind enlightening me on where you found the documentation surrounding that uri_captures[1] bit?

I have been struggling through how to get a rewrite to work after getting regular ingress rules to work.

Hi @thatbenguy. Unfortunately, I did not found any documentation for this at all. I had to dig into the source code of the request-transformer-plugin. The README states that the plugin accepts a config.replace.uri parameter, however that information is missing on kong hub

Then I found several examples in the plugin’s test files where the uri_captures were referenced so I decided to try and it succeeded.

If you have trouble getting it to work, please reach out and I can see if I can help you

Ahhhh, I see, that’s why I didn’t find it :slight_smile:

The ‘uri_captures’ is mentioned on the request-transformer-advanced-plugin which is for the enterprise kong version (https://docs.konghq.com/hub/kong-inc/request-transformer-advanced/0.33-x.html#template-as-value). It didn’t sound like you are using enterprise though?

For the moment I just added a bunch of ingress entries for each endpoint I wanted instead of using wildcards and thus requiring the uri_captures. It’s not the cleanest solution, but for now it works.

Thanks for the reply!

UPDATE: With a bit more fiddling i was able to get it working using the uri_captures as you mentioned in your posts. THANKS!

© 2019 Kong Inc.    Terms  •  Privacy  •  FAQ