<p>Is it somehow possible to add different rewrite targets per domain in an ingress?</p>

<p>I have an ingress that looks like this:</p>

<pre><code>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
</code></pre>

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

<pre><code>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
</code></pre>

<p>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 :-)</p>

<p>Thanks</p>

errnesto
<p>The <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#ingressbackend-v1beta1-networking-k8s-io" rel="nofollow noreferrer">IngressBackend API spec</a> only includes <code>serviceName</code> and <code>servicePort</code> so you will need to rely on the annotations in separate ingress configs. </p>

<h3>ingress-nginx specifics</h3>

<p>The kubernetes <code>ingress-nginx</code> sets up a <a href="https://nginx.org/en/docs/http/ngx_http_core_module.html#server" rel="nofollow noreferrer">"server"</a> 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 <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/" rel="nofollow noreferrer">annotation</a> could include a domain you will need separate the ingress definitions. </p>

<p>You might get away with using a <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#configuration-snippet" rel="nofollow noreferrer"><code>configuration-snippet</code></a> 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.</p>

<h3>Multiple ingress definitions</h3>

<p>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. </p>
