title: Kubernetes Ingress path rewrites
body: <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>