<p>I have two service
serviceOld
serviceNew</p>

<p>I want to achieve the following effect</p>

<pre><code>http://host/any         =&gt; http://serviceOld/any
http://host/any/aaa     =&gt; http://serviceOld/any/aaa
http://host/feature     =&gt; http://serviceNew/feature
http://host/feature/bbb =&gt; http://serviceNew/feature/bbb
</code></pre>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-by-header: new
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: v2
  namespace: api
spec:
  rules:
  - host: xxx.com
    http:
      paths:
      - path: /
        backend:
          serviceName: serviceOld
          servicePort: 80
      - path: /feature/*
        backend:
          serviceName: serviceNew
          servicePort: 8080

</code></pre>

<p>I tried multiple methods and failed to achieve the goal. Can anyone help me?</p>

<p>All you need about rewrite <code>annotation</code> and <code>path</code> can be found in <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite-target" rel="nofollow noreferrer">Ingress Rewrite Docs</a>.</p>

<p>As per example on the site:</p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)
</code></pre>

<p>It will redirect:</p>

<pre><code>rewrite.bar.com/something rewrites to rewrite.bar.com/
rewrite.bar.com/something/ rewrites to rewrite.bar.com/
rewrite.bar.com/something/new rewrites to rewrite.bar.com/new
</code></pre>

<p>In your case it should looks like:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: v2
  namespace: api
spec:
  rules:
  - host: xxx.com
    http:
      paths:
      - path: /any(/|$)(.*)
        backend:
          serviceName: serviceOld
          servicePort: 80
      - path: /feature(/|$)(.*)
        backend:
          serviceName: serviceNew
          servicePort: 8080
</code></pre>
