<p>I have multiple namespaces which utilize different services<br>
Some of these services expose the same paths, i.e. <code>/ready</code>  </p>

<p>I'de like to create an ingress rule per namespace/service so the <strong>external</strong> path of <code>namespaceA/ready</code> will actually go to the service under <code>namespaceA</code> at <code>/ready</code> path</p>

<p>I read about <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">rewrite</a> but if I understand it correctly, it will redirect <code>namespaceA/ready</code> to <code>/ready</code> globally, meaning it will just act as if the path was <code>/ready</code>, regardless to the namespace. </p>

SagiLow
<p>In addition to <a href="https://stackoverflow.com/users/8884013/lentil1016">Lentil1016</a> <a href="https://stackoverflow.com/a/57691517/9521610">answer</a>, here is the example of the same <a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md#rewrite-target" rel="nofollow noreferrer">rewrite</a> solution for <a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md" rel="nofollow noreferrer">nginx-ingress-controller</a>.</p>

<pre><code>---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    ingress.kubernetes.io/ssl-redirect: 'false'
    nginx.ingress.kubernetes.io/force-ssl-redirect: 'false'
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: myapp
  namespace: namespaceA
spec:
  rules:
  - host: mydomain.io
    http:
      paths:
      - backend:
          serviceName: myapp
          servicePort: 80
        path: /namespaceA(/|$)(.*)
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: myapp
  namespace: namespaceB
spec:
  rules:
  - host: mydomain.io
    http:
      paths:
      - backend:
          serviceName: myapp
          servicePort: 80
        path: /namespaceB(/|$)(.*)
</code></pre>

<p>For example, the ingress definition above will result in the following rewrites:</p>

<p>For destination service myapp in the namespace namespaceA:</p>

<pre><code>mydomain.io/namespaceA rewrites to mydomain.io/ 
mydomain.io/namespaceA/ rewrites to mydomain.io/
mydomain.io/namespaceA/ready/ rewrites to mydomain.io/ready/
</code></pre>

<p>For destination service myapp in the namespace namespaceB:</p>

<pre><code>mydomain.io/namespaceB rewrites to mydomain.io/
mydomain.io/namespaceB/ rewrites to mydomain.io/
mydomain.io/namespaceB/ready/ rewrites to mydomain.io/ready/
</code></pre>

<p><strong>Note</strong>: You may need to use nginx-config <a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/nginx-configuration/configmap.md#ssl-redirect" rel="nofollow noreferrer">ConfigMap</a> to completely switch off <a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/nginx-configuration/annotations.md#server-side-https-enforcement-through-redirect" rel="nofollow noreferrer">ssl-redirect</a> for nginx-ingress-controller.<br>
<strong>Note</strong>: There are two kinds of nginx-ingress-controllers, <a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md" rel="nofollow noreferrer">kubernetes</a> and <a href="https://github.com/nginxinc/kubernetes-ingress/blob/master/docs/installation.md" rel="nofollow noreferrer">nginxinc</a>. ConfigMap options and <a href="https://github.com/nginxinc/kubernetes-ingress/blob/master/docs/configmap-and-annotations.md" rel="nofollow noreferrer">annotations</a> could be different for each of them.</p>

<pre><code>kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-config
  namespace: nginx-ingress  # &lt;--- or ingress-nginx, depends on your installation
data:
  redirect-to-https: "false"
  ssl-redirect: "false"
</code></pre>
