Ingress-Nginx rewrite service name based on path

1/24/2022

I've seen many examples online of doing the following, straight from the rewrite docs:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80

However, we have an API that dynamically creates services, and would like to have a single ingress to manage them. All I'm asking is, does this work? Or something like it?

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: direct-to-service-by-name
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
        # Match any character but / and put it in capture group $1
      - path: /([^\/]+)
        pathType: Prefix
        backend:
          service:
            # Name the service based on capture group 1
            name: $1
            port: 
              number: 80
-- Ryan
kubernetes
kubernetes-ingress
nginx
nginx-ingress

0 Answers