Search code examples
kubernetes-ingresstraefik-ingress

Traefik Ingress rewrite-target does nothing


I have a backend service which responds to / but I want it to run on the ingress route myhost.com/overview. No matter which configuration I try, traefik does not strip the path /overview away - I can see the backend getting /overview.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: registry-ingress
  namespace: ingress
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: myhost.com
      http:
        paths:
        - path: /overview
          pathType: Prefix
          backend:
            service:
              name: overview
              port:
                number: 8079

I have tried several variations of this:

traefik.ingress.kubernetes.io/rewrite-target: /$1
...
        - path: /overview(.*)

But these lead to 404 on the ingress /overview requests do not reach the backend.

In summary I want https://myhost.com/overview/ to hit the backend at /.


Solution

  • Traefik does not support the traefik.ingress.kubernetes.io/rewrite-target annotation. See the list of available annotations here: https://doc.traefik.io/traefik/routing/providers/kubernetes-ingress/#annotations

    But your goal can be achieved using the StripPrefix middleware.

    ---
    # Middleware
    # Strip prefix /overview
    apiVersion: traefik.containo.us/v1alpha1
    kind: Middleware
    metadata:
      name: mw-admin
      namespace: ingress
    spec:
      stripPrefix:
        forceSlash: false
        prefixes:
          - /overview
    ---
    # Ingress
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: registry-ingress
      namespace: ingress
      annotations:
        kubernetes.io/ingress.class: traefik
        traefik.ingress.kubernetes.io/router.middlewares: ingress-mw-admin@kubernetescrd
    spec:
      tls:
      - hosts:
        - myhost.com
        secretName: tls-secret
      rules:
        - host: myhost.com
          http:
            paths:
            - path: /overview
              pathType: Prefix
              backend:
                service:
                  name: overview
                  port:
                    number: 8079