<p>I'm trying the official example <a href="https://kubernetes.io/docs/tutorials/stateless-application/guestbook/" rel="nofollow noreferrer">Example: Deploying PHP Guestbook application with MongoDB</a>.</p>
<p>Everything is good, but when I deploy a <code>ingress</code> for it, I can see next in chrome network debug (Here, 31083 is the nodeport):</p>
<pre><code>Request URL: http://10.192.244.109:31083/gb 200 OK
Request URL: http://10.192.244.109:31083/controllers.js 404 Not Found
</code></pre>
<p><strong>ingress.yaml:</strong></p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: gb-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /gb
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              number: 80
</code></pre>
<p><strong>ingress-nginx:</strong> (Use the one <a href="https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.45.0/deploy/static/provider/baremetal/deploy.yaml" rel="nofollow noreferrer">here</a>)</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  annotations:
  labels:
    helm.sh/chart: ingress-nginx-3.27.0
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/version: 0.45.0
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: controller
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: http
    - name: https
      port: 443
      protocol: TCP
      targetPort: https
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/component: controller
</code></pre>
<p><strong>The source code of K8s guestbook index page which I get from chrome:</strong></p>
<pre><code>
&lt;html ng-app=&quot;guestbook&quot;&gt;
  &lt;head&gt;
    &lt;meta content=&quot;text/html;charset=utf-8&quot; http-equiv=&quot;Content-Type&quot;&gt;
    &lt;title&gt;Guestbook&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css&quot;&gt;
    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;controllers.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.0/ui-bootstrap-tpls.js&quot;&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body ng-controller=&quot;guestbookCtrl&quot;&gt;
    &lt;div style=&quot;width: 50%; margin-left: 20px&quot;&gt;
      &lt;h2&gt;Guestbook&lt;/h2&gt;
    &lt;form&gt;
    &lt;fieldset&gt;
    &lt;input ng-model=&quot;msg&quot; placeholder=&quot;Messages&quot; class=&quot;form-control&quot; type=&quot;text&quot; name=&quot;input&quot;&gt;&lt;br&gt;
    &lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot; ng-click=&quot;controller.onguestbook()&quot;&gt;Submit&lt;/button&gt;
    &lt;/fieldset&gt;
    &lt;/form&gt;
    &lt;div&gt;
      &lt;div ng-repeat=&quot;msg in messages track by $index&quot;&gt;
        {{msg}}
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>I know my <code>ingress</code> just specify <code>/gb</code> prefix, so <code>http://10.192.244.109:31083/controllers.js</code> can't route to correct service, but how can I make it work?</p>
<p>I think about maybe could add another ingress rule for <code>js</code> rewrite, but what if I have more than one application? Any suggestion?</p>

atline
<p>You should be able to do this with the correct <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#rewrite" rel="nofollow noreferrer"><code>rewrite-rule</code></a> and <code>path</code> pattern:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: gb-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - http:
      paths:
      - path: /gb(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              number: 80
</code></pre>
<p>This will rewrite your requests in the following way:</p>
<ul>
<li>/gb -&gt; /</li>
<li>/gb/controller.js -&gt; /controller.js</li>
<li>/gb/foo -&gt; /foo</li>
</ul>
