<p>I'm using the OSS <code>ingress-nginx</code> Ingress controller and trying to create a rewrite-target rule such that I can append a path string before my string match.</p>
<p>If I wanted to create a rewrite rule with regex that matches <code>/matched/path</code> and rewrites that to <code>/prefix/matched/path</code>, how might I be able to do that?</p>
<p>I've tried something like the following but it's no good, and I'm just confused about the syntax of this ingress definition:</p>
<pre><code>metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - path: /(/prefix/)(/|$)(/matched/path)(.*)
      backend:
        serviceName: webapp1
</code></pre>

Xenyal
<blockquote>
<p>If I wanted to create a rewrite rule with regex that matches
<code>/matched/path</code> and rewrites that to <code>/prefix/matched/path</code>, how might
I be able to do that?</p>
</blockquote>
<p>In order to achieve this you have add <code>/prefix</code> into your <code>rewrite-target</code>.
Here's a working example with ingress syntax from k8s v1.18:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: example-ingress-v118
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /prefix/$1
spec:
  rules:
    http:
       paths:
       - path: /(matched/path/?.*)
        backend:
          serviceName: test
          servicePort: 80   
</code></pre>
<p>Since the syntax for the new ingress changed in 1.19 (see <a href="https://kubernetes.io/docs/setup/release/notes/" rel="nofollow noreferrer">release notes</a> and some small info at the end) I`m placing also an example with it:</p>
<pre class="lang-yaml prettyprint-override"><code>
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress-v119
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /prefix/$1
spec:
  rules:
  - http:
      paths:
      - path: /(matched/path/?.*)
        pathType: Prefix
        backend:
          service:
            name: test
            port:
              number: 80

</code></pre>
<p>Here is a test with http echo server:</p>
<pre><code>➜  ~ curl 172.17.0.4/matched/path 
{
  &quot;path&quot;: &quot;/prefix/matched/path&quot;,
  &quot;headers&quot;: {
    &quot;host&quot;: &quot;172.17.0.4&quot;,
    &quot;x-request-id&quot;: &quot;011585443ebc6adcf913db1c506abbe6&quot;,
    &quot;x-real-ip&quot;: &quot;172.17.0.1&quot;,
    &quot;x-forwarded-for&quot;: &quot;172.17.0.1&quot;,
    &quot;x-forwarded-host&quot;: &quot;172.17.0.4&quot;,
    &quot;x-forwarded-port&quot;: &quot;80&quot;,
    &quot;x-forwarded-proto&quot;: &quot;http&quot;,
    &quot;x-scheme&quot;: &quot;http&quot;,
    &quot;user-agent&quot;: &quot;curl/7.52.1&quot;,
    &quot;accept&quot;: &quot;*/*&quot;
  },
</code></pre>
<p>This rule will also ignore the <code>/</code> at the end of the request:</p>
<pre><code>➜  ~ curl 172.17.0.4/matched/path/
{
  &quot;path&quot;: &quot;/prefix/matched/path/&quot;,
  &quot;headers&quot;: {
    &quot;host&quot;: &quot;172.17.0.4&quot;,
    &quot;x-request-id&quot;: &quot;0575e9022d814ba07457395f78dbe0fb&quot;,
    &quot;x-real-ip&quot;: &quot;172.17.0.1&quot;,
    &quot;x-forwarded-for&quot;: &quot;172.17.0.1&quot;,
    &quot;x-forwarded-host&quot;: &quot;172.17.0.4&quot;,
    &quot;x-forwarded-port&quot;: &quot;80&quot;,
    &quot;x-forwarded-proto&quot;: &quot;http&quot;,
    &quot;x-scheme&quot;: &quot;http&quot;,
    &quot;user-agent&quot;: &quot;curl/7.52.1&quot;,
    &quot;accept&quot;: &quot;*/*&quot;
  },
</code></pre>
<hr />
<p>Worth to mention some notable differences/changes in the new ingress syntax:</p>
<ul>
<li><code>spec.backend</code> -&gt; <code>spec.defaultBackend</code></li>
<li><code>serviceName</code> -&gt; <code>service.name</code></li>
<li><code>servicePort</code> -&gt; <code>service.port.name</code> (for string values)</li>
<li><code>servicePort</code> -&gt; <code>service.port.number</code> (for numeric values) <code>pathType</code> no longer has a default value in v1; &quot;Exact&quot;, &quot;Prefix&quot;, or
&quot;ImplementationSpecific&quot; must be specified Other Ingress API updates</li>
<li>backends can now be resource or service backends</li>
<li><code>path</code> is no longer required to be a valid regular expression (#89778,
@cmluciano) [SIG API Machinery, Apps, CLI, Network and Testing]</li>
</ul>
