Rewrite targets in Kong ingress

Hi,

I installed kong kubernetes ingress controller. I created an ingress resource for my application.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: baster-api-ingress
  namespace: baster
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    kubernetes.io/ingress.class: kong
spec:
  rules:
  - http:
      paths:
      - path: /(.*)
        backend:
          serviceName: baster-api
          servicePort: 5000
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: mongo-express-ingress
  namespace: baster
  annotations:
    kubernetes.io/ingress.class: kong
spec:
  rules:
  - http:
      paths:
      - path: /mongo-express
        backend:
          serviceName: mongo-express
          servicePort: 8081

Before using Kong i was using nginx ingress as k8s ingress controller and used it for sending the URI paths to the upstream using rewrite-target plugin (Rewrite - Ingress-Nginx Controller).

When i access http://hostname/path1, http://hostname/path2 then it sends to baster-api service with uri path /path1 and /path2 respectively…

But, If i send http://hostname/mongo-express it sends to mongo-express service with /mongo-express uri to this upstream

It was working fine and was routing the ingress to right service based on the uri and sends the uri to the target services.

Now i started using Kong and used the same nginx rewrite plugin to send the uri to right upstream. But now it is always sending to baster-api even if i try to access with /mongo-express.

How to fix this ? is there any native way in kong to send the URI paths to the upstream services based on match ? or can i make the nginx annotation work in someway ?

I fixed it and made it work. But had to make some changes to the regex for forwarding and removed the nginx rewrite.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: mongo-express-ingress
namespace: {{ .Values.namespace }}
annotations:
kubernetes.io/ingress.class: kong
spec:
rules:

  • http:
    paths:
    • path: /mongo-express(.*)
      pathType: Prefix
      backend:
      service:
      name: {{ .Values.mongo_express.service_name }}
      port:
      number: 8081
root@kong-microk8s-001:/opt/baster# cat helm/templates/baster-api-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: baster-api-ingress
  namespace: {{ .Values.namespace }}
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    kubernetes.io/ingress.class: kong
spec:
  rules:
  - http:
      paths:
      - path: /(.*)
        pathType: ImplementationSpecific
        backend:
          service:
            name: {{ .Values.baster_api.service_name }}
            port:
              number: 5000
1 Like

I think you should use the annotation for strip-path

konghq.com/strip-path: “true”