How do you make a Kubernetes Ingress Path with a wildcard or regex in the middle?

Sorry this has probably been asked a million times and in the documentation but I can't seem to find the answer. I have a url endpoint with a unique UUID in the middle so how do I route that path in my Kubernetes ingress router. For example a path like this api/foo/123-aad-1123-abc/bar needs to be routed but for all uuids and the uuid needs to be provided to the microservice

Here is my Kubernetes ingress yaml. What would I need to update the path to get this to work?:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/router.entrypoints: "web"
    traefik.ingress.kubernetes.io/router.tls: "false"
    traefik.ingress.kubernetes.io/router.middlewares: default-traefik-header@kubernetescrd
  name: traefik
spec:
  rules:
    - host: "localhost"
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: webapp
                port:
                  name: webapp
          - path: "Foo/*/bar"
            pathType: Prefix
            backend:
              service:
                name: api
                port:
                  name: api

Thanks for any help in advance

1 Like

A similar question has been asked before, sadly without an answer.

I've done a writeup on how this works for us. Allow configuring rule syntax with Kubernetes Ingress annotation by rtribotte · Pull Request #10985 · traefik/traefik · GitHub

I suspect if you changed

  • path: "Foo/*/bar"
    to something like one of these (I don't know how many times you will have to escape the slashes)
  • path: '/{regex:(foo/.*/bar)}'
  • path: '/{regex:(foo/.*/bar)}'
  • path: '/{regex:(foo\/.*\/bar)}'

it might work. Note if you are on traefik 3.x you will have to pass in some command line switches.