<p>I'm setting up a Kubernetes cluster which contains three applications, each running within their own respective pod and service. The web frontends of these applications should be accesible at ports 80, 9000 and 15672. There are also a number of Backend-APIs running in their own pods and services, which should be accesible at their respective ports. The cluster is accessed through a NGINX reverse proxy with the following ingress-definition:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  namespace: my-namespace
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  tls:
  - hosts:
    - myapp.com
    secretName: my-certificat
  rules:
  - host: myapp.com
    http:
      paths:
      - backend:
          serviceName: my-service-1
          servicePort: 8092
        path: /api/someroute/(.*)
      - backend:
          serviceName: my-service-2
          servicePort: 30003
        path: /api/someotherroute/(.*)
      - backend:
          serviceName: my-other-frontend
          servicePort: 9000
        path: /other/(.*)
      - backend:
          serviceName: my-yetanother-frontend
          servicePort: 15672
        path: /yetanother/(.*)
      - backend:
          serviceName: my-main-frontend
          servicePort: 80
        path: /(.*)
</code></pre>

<p>This works for the api-services, but not for the frontends. Was I to enter a URI like <code>myapp.com/other/</code> in my browser it would make a call to <code>my-other-frontend:9000/other/</code>, instead of <code>my-other-frontend:9000/</code>. This can of course be solved with a rewrite annotation like <code>nginx.ingress.kubernetes.io/rewrite-target: /$1</code>, the problem is, that this would also apply to the api-services, and those actually need the complete route in their call, so a general rewrite rule would break them.</p>

<p><strong>So my question is: Is it possible to define rewrite rules that only apply to specific paths?</strong></p>

TigersEye120
<p>I would advise to split your ingress into 2 based on your different annotations.</p>

<p>1) First one for api-services, called <code>my-api-ingress</code></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-api-ingress
  namespace: my-namespace
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  tls:
  - hosts:
    - myapp.com
    secretName: my-certificat
  rules:
  - host: myapp.com
    http:
      paths:
      - backend:
          serviceName: my-service-1
          servicePort: 8092
        path: /api/someroute/(.*)
      - backend:
          serviceName: my-service-2
          servicePort: 30003
        path: /api/someotherroute/(.*)
</code></pre>

<p>2) And second one, lets say <code>my-front-ingress</code> with <code>nginx.ingress.kubernetes.io/rewrite-target: /$1</code> annotation for rest of your frontends. Based on your question I believe you dont need the detailed instruction for ingress creation with rewrite annotation.</p>

<p>EDIT1:
Yes, its possible to create multiple ingresses with the same host. But please pay attention which exactly nginx ingress. Refer to @LazerBass <a href="https://stackoverflow.com/a/59664999/9929015">answer</a> with his investigations. </p>

<p>In short, based on <a href="https://github.com/nginxinc/kubernetes-ingress/blob/master/docs/nginx-ingress-controllers.md" rel="nofollow noreferrer">nginx ingress comparation table</a>,</p>

<p>The <a href="https://github.com/nginxinc/kubernetes-ingress" rel="nofollow noreferrer">nginxinc</a> controller does not support merging Ingress rules with the same host. You can use it only with <a href="https://github.com/nginxinc/kubernetes-ingress/tree/master/examples/mergeable-ingress-types" rel="nofollow noreferrer">Mergeable Ingresses</a></p>

<p>The easiest way is to use regular <a href="https://github.com/kubernetes/ingress-nginx" rel="nofollow noreferrer">kubernetes/ingress-nginx</a>
<a href="https://i.stack.imgur.com/NQ4Kr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NQ4Kr.png" alt="enter image description here"></a></p>

<p>Hope that helps.</p>
