<p>I am migrating legacy websites to Kubernetes, which were working on URLs like <strong><a href="http://www.app1.com" rel="nofollow noreferrer">www.app1.com</a></strong>, <strong>www.app2.com,</strong>.</p>
<p>As all are getting deployed in one K8s cluster so I want to use URLs like <code>www.myapp.com/app1</code> and so on. But when I access <strong><a href="http://www.myapp.com/app1" rel="nofollow noreferrer">www.myapp.com/app1</a></strong> it routes to <strong><a href="http://www.myapp.com/login" rel="nofollow noreferrer">www.myapp.com/login</a></strong> rather than <strong><a href="http://www.myapp.com/app1/login" rel="nofollow noreferrer">www.myapp.com/app1/login</a></strong></p>
<p><strong>Try1:</strong></p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: rewrite
spec:
  rules:
  - host: www.myapp.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /app1/?(.*)
</code></pre>
<p>I tried path: /app1(.*) but it always routes this way:</p>
<p><strong>Try2:</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/configuration-snippet: rewrite ^(/app1)$ $1/ permanent;
  name: rewrite
spec:
  rules:
  - host: www.myapp.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /app1(/|$)(.*)
</code></pre>
<p>This lands me to the login page but all URLs on the page still have URLs like <a href="http://www.myapp.com/page1" rel="nofollow noreferrer">www.myapp.com/page1</a> or <strong><a href="http://www.myapp.com/page2" rel="nofollow noreferrer">www.myapp.com/page2</a></strong> rather than <strong><a href="http://www.myapp.com/app1/page1" rel="nofollow noreferrer">www.myapp.com/app1/page1</a></strong> or <strong><a href="http://www.myapp.com/app1/page2" rel="nofollow noreferrer">www.myapp.com/app1/page2</a></strong>.</p>
<p>It looks like I have to do something in my app, but can't we do something within ingress without changes in the app code?</p>
<p><strong>Update1:</strong>
<em>ingress logs:</em>
In browser when i access <a href="http://www.myapp.com/app1" rel="nofollow noreferrer">www.myapp.com/app1</a> it prints following logs</p>
<pre><code>[13/Aug/2020:21:19:25 +0000] &quot;GET /app1 HTTP/2.0&quot; 303 0 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36&quot; 503 0.005 [my-ns-http-svc-80] [] x.x.x.x:80 5 0.005 303 d5da4ff09ee26c83fe67519c98f5eb50
</code></pre>
<p>and in browser it gives 404 error and in URL-bar URL is <strong><a href="http://www.myapp.com/login" rel="nofollow noreferrer">www.myapp.com/login</a></strong></p>

<p>Looks like the following redirect is happening in your app:</p>
<p><code>www.myapp.com/app1</code> ➡️ <code>www.myapp.com/app1/login</code></p>
<p>So if you'd like the same behavior in the Ingress resource you will have to remove the <code>nginx.ingress.kubernetes.io/rewrite-target:</code> annotation.</p>
<p>Then on your paths, you can have something like this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: rewrite
spec:
  rules:
  - host: www.myapp.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /app1 👈 Handles the initial request
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /app1/.* 👈 Handles the redirect your app is doing.
</code></pre>
<p>✌️</p>
