Search code examples
google-kubernetes-engine

Remove routing path from Kubernetes ingress


I deployed service called "test" in kubernetes. service name : test port : 80

There is endpoint called "/abc"

ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: load-balancer

spec:
  rules:
  - http:
      paths:
      - path: /test/*
        backend:
          serviceName: test
          servicePort: 80

API call "http://ip-address/test/abc" given 404 error. But endpoint "/test/abc" working properly.

I need skip "/test" when routing. How I do this.


Solution

  • apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/use-regex: "true"
        nginx.ingress.kubernetes.io/rewrite-target: /$2
      name: rewrite
      namespace: default
    spec:
      rules:
      - host: rewrite.bar.com
        http:
          paths:
          - backend:
              serviceName: http-svc
              servicePort: 80
            path: /something(/|$)(.*)
    

    In this ingress definition, any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation.

    For example:

    • rewrite.bar.com/something rewrites to rewrite.bar.com/

    Source: https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md.