title: How to use nginx capture group to attach prefix to rewrite rule
body: <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>