How to redirect to a different backend path

Hello

We are upgrading our product with kong right now. I want to redirect the request path to a different backend path. we have achieved this function in a non-Kubernetes environment, but it’s not working in the k8s.

For example, we want request path /sea/api/rest to /sea/api. just strip out the last one.

how Kong achieve this? thanks.

This is the ingress, but it’s not working.

apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
  name: voicenet-module-api
upstream:
  hash_on: ip
proxy:
  path: /sea/api/

apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
  annotations:
    konghq.com/strip-path: "false"
  name: voicenet-module-api
  namespace: app
spec:      
  ingressClassName: kong
  rules:      
  - host: voicenet.com
    http:
      paths:
      - path: /sea/api/rest/ 
        pathType: Prefix
        backend:
          serviceName: voice-network-gateway
          servicePort: 9527

@smallc2009 Can you try Request-Transformer plugin for your use-case?
I believe you can simply achieve this. It has replace.uri parameter just use that with a regex pattern. This github issue would help you.
Hope it helps.

You’ll want konghq.com/strip-path: "true" instead of "false" on the Ingress, since otherwise the rule path (/sea/api/rest/) will be set upstream (with your current configuration, a request for /sea/api/rest/ from a client would be sent to /sea/api/sea/api/rest/ upstream).

You’ll also need to make sure that your Service has an konghq.com/override: voicenet-module-api annotation to bind that KongIngress to it.

Hi guys,

Thanks for your advice. This problem is solved by using Kong plugin - Request Transform, not using KongIngress at all. please see below a brief explanation, hopefully, it can help.

the original purpose was when the client requested http://192.168.0.1/sea/api/rest/xxx/xxx would redirect to http://192.168.0.1/sea/api/xxx/xxx

it simplified as following

Route: -----> Service
/sea/api/rest/xxx -----> /sea/api/xxx

when using request-transform, the regex of ingress file matched route path, saved as variable and then replaced by request transform.

the ingress and plugin configuration files showed as below:

  1. Ingress file
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    konghq.com/plugins: "remove-rest"
    konghq.com/strip-path: "true"
  name: voicenet-rest-api
  namespace: app
spec:
  ingressClassName: kong
  rules:
  - host:  
    http:
      paths:
      - path: /sea/api/rest/(?<url>\S+)$
        pathType: Prefix
        backend:
          serviceName: voice-network-gateway
          servicePort: 9527
  1. Kong Plugin configure file
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: remove-rest
config: 
  replace:
    uri: '/sea/api/$(uri_captures.url)'
plugin: request-transformer