<p>I am running <code>Ingress</code> for some services on Kubernetes, and although services are re-directing to those cluster IPs (somewhat correctly), I am not sure how to correctly resolve paths after just those basic paths, for example, if I have Tomcat in path <code>/</code> and Nginx on path <code>/nginx</code>, any Tomcat path after <code>/</code> does not resolve and neither does any Nginx path resolve for even <code>/</code>. I already have <code>nginx.ingress.kubernetes.io/rewrite-target: /</code> set in my <code>ingress.yaml</code>:</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  namespace: kube-system
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-deployment-service 
          servicePort: 8080
      - path: /nginx
        backend:
          serviceName: nginx-deployment-service 
          servicePort: 80
</code></pre>
<p>If Tomcat requires a <code>/main.css</code> file, for example, the browser would try to fetch <code>/main.css</code>, but this returns the <code>default-backend</code>.</p>
<p>Also, if I try to visit <code>/nginx</code> (attempting to hit <code>/</code>) on the Nginx server, Nginx says <code>/nginx</code> is not found (obviously).</p>
<pre><code>2019/03/02 08:12:04 [error] 8#8: *343 open() &quot;/usr/share/nginx/html/nginx&quot; failed (2: No such file or directory), client: 10.128.0.7, server: localhost, request: &quot;GET /nginx HTTP/1.1&quot;, host: &quot;REDACTED_SERVER_IP&quot;
10.128.0.7 - - [02/Mar/2019:08:12:04 +0000] &quot;GET /nginx HTTP/1.1&quot; 404 153 &quot;-&quot; &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0&quot; &quot;REDACTED_CLIENT_IP, REDACTED_SERVER_IP&quot;
10.40.1.1 - - [02/Mar/2019:08:12:05 +0000] &quot;GET /nginx HTTP/1.1&quot; 404 153 &quot;-&quot; &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0&quot; &quot;REDACTED_CLIENT_IP, REDACTED_SERVER_IP&quot;
2019/03/02 08:12:05 [error] 8#8: *344 open() &quot;/usr/share/nginx/html/nginx&quot; failed (2: No such file or directory), client: 10.40.1.1, server: localhost, request: &quot;GET /nginx HTTP/1.1&quot;, host: &quot;REDACTED_SERVER_IP&quot;
</code></pre>
<p><strong>How can I make paths after a specified <code>path</code> in <code>ingress.yaml</code> resolve considering the beginning part of the path as viewed from the service?</strong></p>
<p>i.e.</p>
<ol>
<li>If Tomcat was ingress pathed to <code>/tomcat</code>, how could I make <code>/tomcat/main.css</code> appear as <code>/main.css</code> to the Tomcat server (and not resolve to default backed)?</li>
<li>If Nginx was ingress pathed to <code>/nginx</code>, how could I make <code>/nginx</code> appear as <code>/</code> to the Nginx server (and not appear as <code>/nginx</code>)?</li>
</ol>
<p>I thought this was what <code>/rewrite-target</code> was suppose to do.</p>
<p><strong>Would I need to use wildcards or something?</strong></p>
<p>My ingress is the following:</p>
<pre><code>Name:             tomcat-ingress
Namespace:        kube-system
Address:          REDACTED_SERVER_IP
Default backend:  default-http-backend:80 (10.40.0.6:8080)
Rules:
  Host  Path  Backends
  ----  ----  --------
  *         
        /        tomcat-deployment-service:8080 (10.40.2.15:8080)
        /nginx   nginx-dep-ser:80 (10.40.0.26:80,10.40.1.46:80)
Annotations:
  ingress.kubernetes.io/url-map:                     k8s-um-kube-system-tomcat-ingress--b0fc8aa23db1001d
  kubectl.kubernetes.io/last-applied-configuration:  {&quot;apiVersion&quot;:&quot;extensions/v1beta1&quot;,&quot;kind&quot;:&quot;Ingress&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{&quot;nginx.ingress.kubernetes.io/rewrite-target&quot;:&quot;/&quot;},&quot;name&quot;:&quot;tomcat-ingress&quot;,&quot;namespace&quot;:&quot;kube-system&quot;},&quot;spec&quot;:{&quot;rules&quot;:[{&quot;http&quot;:{&quot;paths&quot;:[{&quot;backend&quot;:{&quot;serviceName&quot;:&quot;tomcat-deployment-service&quot;,&quot;servicePort&quot;:8080},&quot;path&quot;:&quot;/&quot;},{&quot;backend&quot;:{&quot;serviceName&quot;:&quot;nginx-dep-ser&quot;,&quot;servicePort&quot;:80},&quot;path&quot;:&quot;/nginx&quot;}]}}]}}

  nginx.ingress.kubernetes.io/rewrite-target:  /
  ingress.kubernetes.io/backends:              {&quot;k8s-be-30985--b0fc8aa23db1001d&quot;:&quot;HEALTHY&quot;,&quot;k8s-be-31229--b0fc8aa23db1001d&quot;:&quot;HEALTHY&quot;,&quot;k8s-be-32736--b0fc8aa23db1001d&quot;:&quot;HEALTHY&quot;}
  ingress.kubernetes.io/forwarding-rule:       k8s-fw-kube-system-tomcat-ingress--b0fc8aa23db1001d
  ingress.kubernetes.io/target-proxy:          k8s-tp-kube-system-tomcat-ingress--b0fc8aa23db1001d
Events:
  Type    Reason  Age                From                      Message
  ----    ------  ----               ----                      -------
  Normal  CREATE  33m                nginx-ingress-controller  Ingress kube-system/tomcat-ingress
  Normal  ADD     33m                loadbalancer-controller   kube-system/tomcat-ingress
  Normal  UPDATE  3m (x65 over 33m)  nginx-ingress-controller  Ingress kube-system/tomcat-ingress
  Normal  CREATE  3m (x31 over 33m)  loadbalancer-controller   ip: REDACTED_SERVER_IP
</code></pre>

<p>I assume that you have quite recent version of ingress controller, and according to the <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="noreferrer">documentation</a>:</p>

<blockquote>
  <p>Starting in Version 0.22.0, ingress definitions using the annotation <code>nginx.ingress.kubernetes.io/rewrite-target</code> are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a <strong>capture group</strong>.</p>
</blockquote>

<p>E.g:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: tomcat-ingress
  namespace: default 
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: tomcat-deployment-service
          servicePort: 8080
        path: /tomcat/?(.*)
      - backend:
          serviceName: nginx-deployment-service
          servicePort: 80
        path: /nginx/?(.*)
</code></pre>

<p>Your current configuration works well for <code>/</code> and <code>/nginx</code> destinations only with the recent version of ingress controller.</p>

<p>Another important thing, the Ingress object should be created in the same namespace with the service(s) it refers to. Otherwise ingress-nginx usually returns <code>503 Service Temporarily Unavailable</code> error. </p>

<p>And it's not allowed to refer to a service in the different namespace in the <code>serviceName</code>:</p>

<blockquote>
  <p>a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name',  or 'abc-123', regex used for validation is 'a-z?')</p>
</blockquote>
