<p>I have two services running in kubernetes with url say <code>https://service1.abc.cloud.com</code> and <code>https://service2.abc.cloud.com</code>. I have routes defined in Ingress of both the services, for example my swagger path is <code>https://service1.abc.cloud.com/docs</code> and in ingress I have defined /docs to go to my service at SSO port 8082. Once SSO is successfull SSO redirects to my service1/docs route.</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ template &quot;service1.fullname&quot; . }}
  labels:
{{ include &quot;service1.labels&quot; . | indent 4 }}
{{ include &quot;chart.labels&quot; . | indent 4 }}
  annotations:
{{ include &quot;service1.annotations&quot; . | indent 4 }}
spec:
  rules:
    - host: {{ template &quot;service1.dns&quot; . }}
      http:
        paths:
          - path: /docs
            backend:
              serviceName: {{ template &quot;service1.fullname&quot; . }}
              servicePort: 8082
</code></pre>
<p>Similar ingress file is for service2.</p>
<p>Till now the story was all good, but there is requirement popped up that we need to have a common url for all the services and the path param for the common url will have the service name. So we have 2 ingress file for service1 and service2 now.</p>
<p>The new url for service1 and service2 will be:-</p>
<pre><code>https://mynamespace.abc.cloud.com/service1
https://mynamespace.abc.cloud.com/service2
</code></pre>
<p>I created a common ingress file for mynamespace host as below. So now, I have 3 ingress file one for mynamespace , one for service1 and one for service2.
I know it should be one ingress file doing all the routes mapping but untill we deprecate the service1 and service2 urls we need to support the older service1 and service2 urls. Once we deprecate, we will be merging all code in one deployment and ingress.</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ template &quot;mynamespace.fullname&quot; . }}
  labels:
{{ include &quot;mynamespace.labels&quot; . | indent 4 }}
{{ include &quot;chart.labels&quot; . | indent 4 }}
  annotations:
{{ include &quot;mynamespace.ingress.annotations&quot; . | indent 4 }}
spec:
  rules:
    - host: {{ template &quot;iot.dns&quot; . }}
      http:
        paths:
          - path: /mynamespace/docs
            backend:
              serviceName: {{ template &quot;service1.fullname&quot; . }}
              servicePort: 8082
</code></pre>
<p>Problem is I'm not able to route /mynamespace/docs of mynamespace host to /docs of service1 host. I also have SSO sidecar which is at port 8082 and my service1 and service2 is at port 8080.</p>
<p>End goal to be achieved:- When user hits url <code>https://mynamespace.abc.cloud.com/service1/docs</code> it should internally open the service1 swagger file which is hosted at <code>https://service1.abc.barco.cloud.com/docs</code>. The url in browser should not change so redirect should happen at kubernetes level and url should not change in browser.
Since i need to support old urls too, they should also work i.e. <code>https://service1.abc.cloud.com</code> and <code>https://service2.abc.cloud.com</code></p>
<p>Old design below:-
<a href="https://i.stack.imgur.com/eHEQn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eHEQn.png" alt="enter image description here" /></a></p>
<p>New design below:-
<a href="https://i.stack.imgur.com/9hX9E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9hX9E.png" alt="enter image description here" /></a></p>

<p>I am putting my best guess here:</p>
<blockquote>
<p>You are trying to route <code>mynamespace.abc.cloud.com/service1/docs</code> to <code>service1:8082/docs</code>. You are having difficulty to convert the external path <code>/service1/docs</code> to the internal path <code>/docs</code> of service1</p>
</blockquote>
<p>If that is the case, what your ingress needs is the <code>rewrite-target</code> and <code>use-regex</code> annotations:</p>
<pre><code>nginx.ingress.kubernetes.io/rewrite-target: /docs/$2
nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
</code></pre>
<p>And the path definition needs to be suffixed with <code>(/|$)(.*)</code>:</p>
<pre><code>- path: /service1/docs(/|$)(.*)
</code></pre>
