Ingress rewrite-target for inner url

12/31/2019

I have a ingress controller with 2 application, one is just a hello world and another with a real micro-service application. now when I curl my application(ingress seems working fine) curl -v /test -->it give hello-world curl -v /clus -->it redirects to my application (as application is a springboot with resources/static/index.html,it gets rendered when curl. it curl -v /clus -->index.html -->expected but if I want to get curl -v /v1/heart-beat -->still it gives me index.html any idea why it is so ? I think culprit is my ingress.yaml rewrite-target which always redirect to root? Any idea how to resolve it?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: dev
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /

spec:
  rules:
  - http:
      paths:
        - path: /clus
          backend:
            serviceName: clus-dev-svc
            servicePort: 80
        - path: /test
          backend:
            serviceName: hello-service
            servicePort: 80
-- Jithin Kumar S
kubectl
kubernetes
kubernetes-ingress
nginx-ingress

1 Answer

1/1/2020

Finally after a long search and R&D found the answer.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: dev
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1


spec:
  rules:
  - http:
      paths:
        - path: /clus/(.+)
          backend:
            serviceName: clus-service
            servicePort: 80
        - path: /test
          backend:
            serviceName: hello-service
            servicePort: 80

Initially thought to add regex but didn't worked, finally the above worked

-- Jithin Kumar S
Source: StackOverflow