<p>I am currently trying to rewrite four paths in kubernetes ingress.</p>

<p>The three are done but the static file path is a bit tricky.</p>

<p>My frontend path is <code>/$1</code> -> <code>/(.*)</code> web-front-cluster-ip-service</p>

<p>I also need to rewrite all <code>/img/$1</code> -> <code>/img/(.*)</code> apollo-api-cluster-ip-service</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: servers-ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
          - backend:
              serviceName: apollo-api-cluster-ip-service
              servicePort: 4000
            path: /apollo_api/?(.*)
          - backend:
              serviceName: rest-api-cluster-ip-service
              servicePort: 5000
            path: /rest/?(.*)
          - backend:
              serviceName: web-front-cluster-ip-service
              servicePort: 6000
            path: /?(.*)
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: statics-ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /img/$1
spec:
  rules:
    - http:
        paths:
        - backend:
            serviceName: apollo-api-cluster-ip-service
            servicePort: 4000
          path: /img/?$(.*)
</code></pre>

<p>My current setting looks like above but for unknown reason, I am not able to reach the route configured by the second route.</p>

<p>So what I am trying to do instead is to combine two routes together and provide prefix to capture group that matches <code>/img</code></p>

<p>if I do</p>

<pre><code>      - backend:
          serviceName: apollo-api-cluster-ip-service
          servicePort: 6000
        path: /?(.*)
</code></pre>

<p>it will route everything to <code>apollo-api</code> when I need any route that does not start with <code>/img</code> to be routed to <code>web-front</code></p>

<p>so I need something like below</p>

<pre><code>      - backend:
          serviceName: apollo-api-cluster-ip-service
          servicePort: 4000
        path: /img?(.*) &lt;- somehow add /img to $(.*) because currently it rewrites `/img/static.png` -&gt; `/static.png` but I need `/img/static.png` -&gt; `/img/static.png`.
</code></pre>

<p>Any help would be appreciated.</p>

forJ
<p>As mentioned in <a href="https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/rewrite#rewrite-target" rel="nofollow noreferrer">Rewrite Target documentation</a>:</p>
<blockquote>
<p>!!! attention Starting in Version 0.22.0, ingress definitions using
the annotation nginx.ingress.kubernetes.io/rewrite-target are not
backwards compatible with previous versions. In Version 0.22.0 and
beyond, any substrings within the request URI that need to be passed
to the rewritten path must explicitly be defined in a capture group.</p>
<p>!!! note Captured groups are saved in numbered placeholders, chronologically, in the form $1, $2 ... $n. These placeholders can be
used as parameters in the rewrite-target annotation.</p>
</blockquote>
<p>So I suppose your second ingress should look like:</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: statics-ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /img/$2
spec:
  rules:
    - http:
        paths:
        - backend:
            serviceName: apollo-api-cluster-ip-service
            servicePort: 4000
          path: /img(/|$)(.*)
</code></pre>
<p>In this case <code>$2</code> will reflect <code>(.*)</code></p>
