k8s ingress nginx set rewrite-target per domain

1/27/2020

Is it somehow possible to add different rewrite targets per domain in an ingress?

I have an ingress that looks like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: multiple-domains-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /entry1/$1
spec:
  rules:
    - host: domain1.com
      http:
        paths:
          - path: /(.*)
            backend:
              serviceName: my-service
              servicePort: 8080

It would be nice if I could add multiple paths with different redirects like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: multiple-domains-ingress

spec:
  rules:
    - host: domain1.com
      http:
        paths:
          - path: /(.*)
            backend:
              serviceName: my-service
              servicePort: 8080
              redirect: /entry1/$1

    - host: domain2.com
      http:
        paths:
          - path: /(.*)
            backend:
              serviceName: my-service
              servicePort: 8080
              redirect: /entry2/$1

Or do I have to create a new ingress for every domain? Also If there are any best practices I'm violating here I'd be happy to hear about it :-)

Thanks

-- errnesto
kubernetes
kubernetes-ingress
nginx-ingress

2 Answers

1/27/2020

This is one of the frustrating things about the Ingress abstraction. It was built be a minimalist system so that it could be easily implemented by multiple controllers (which worked), but that means a lot of features are stuck being implemented by annotations which usually don’t match up against the abstraction very well since those are a generic key/value pair system.

-- coderanger
Source: StackOverflow

1/28/2020

The IngressBackend API spec only includes serviceName and servicePort so you will need to rely on the annotations in separate ingress configs.

ingress-nginx specifics

The kubernetes ingress-nginx sets up a "server" in it's config per ingress definition. This is similar to an apache VirtualHost. Annotations are generally applied at this server level. So unless the specific annotation could include a domain you will need separate the ingress definitions.

You might get away with using a configuration-snippet annotation supplying custom nginx config on the one ingress but I would avoid that as it introduces the chance of breaking configuration. This also increases the requirement of using only nginx for the ingress controller.

Multiple ingress definitions

The main thing to watch out for is repeated configs. If you end up managing ingress definitions for 100's of domains you might want to build the config from a single list of domains rather than maintaining the same information in 100's of files.

-- Matt
Source: StackOverflow