Traefik Ingress rewrite-target does not work

Hello,
As described here and here the annotation ***.ingress.kubernetes.io/rewrite-target in Kubernetes Ingress seems to work fine with NGINX but not with traefik.

Not sure if this is a Traefik or Kubernetes Issue but Traefik Ingress Controller is just ignoring traefik.ingress.kubernetes.io/rewrite-target:

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

Similarly with path: /foo(.*) and traefik.ingress.kubernetes.io/rewrite-target: /$1 no path rewriting is taking place and a 404 is returned.

Hello @cawoodm,

Traefik does not support the traefik.ingress.kubernetes.io/rewrite-target annotation. See the list of available annotations here: Kubernetes Ingress - Traefik

But your goal can be achieved using the StipPrefix middleware.

Thanks Tom, I looked into that and it seems I have to employ Middleware and Ingress Routers.

Is there no way to configure or "talk to" traefik from an Ingress Resource?

I guess I could create a Middleware but I don't see how to link it to my routing rules which are in an Ingress Resource kind: Ingress.

You have access to this annotation on Ingress: traefik.ingress.kubernetes.io/router.middlewares: my-stripprefix@kubernetescrd.

Hope it helps :slight_smile:

Unfortunately, as soon as I apply the annotation everything stops working and I just get 404s.

│ 10.42.0.1 - - [21/Jan/2022:13:40:39 +0000] "GET /overview/ HTTP/2.0" 404 19 "-" "-" 23891 "-" "-" 0ms

Also I had to remove the @kubernetescrd part as traefik did not like it:

time="2022-01-21T13:40:00Z" level=error msg="middleware \"mw-admin@kubernetescrd\" does not exist" entryPointName=web routerName=
---
# 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: mw-admin
spec:
  tls:
  - hosts:
    - admin.mydomain.com
    secretName: tls-secret
  rules:
    - host: admin.mydomain.com
      http:
        paths:
        - path: /overview
          pathType: Prefix
          backend:
            service:
              name: overview
              port:
                number: 8079

I tried with forceSlash: false, forceSlash: true and without this line.

OK, thanks to this post I was able to solve it.
The namespace must pre prefixed to the annotation and the @ suffix appended!

traefik.ingress.kubernetes.io/router.middlewares: ingress-mw-admin@kubernetescrd

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.