<p>I have an ingress, defined as the following:</p>
<pre><code>Name:             online-ingress
Namespace:        default
Address:          
Default backend:  default-http-backend:80 (&lt;error: endpoints &quot;default-http-backend&quot; not found&gt;)
Rules:
  Host          Path  Backends
  ----          ----  --------
  jj.cloud.com  
                /online/(.*)   online:80 (172.16.1.66:5001)
                /userOnline    online:80 (172.16.1.66:5001)
Annotations:    nginx.ingress.kubernetes.io/rewrite-target: /$1
Events:
  Type    Reason          Age                From                      Message
  ----    ------          ----               ----                      -------
  Normal  AddedOrUpdated  29m (x4 over 74m)  nginx-ingress-controller  Configuration for default/online-ingress was added or updated
</code></pre>
<p>If I test it with no rewrite, it's ok.</p>
<pre><code>curl -X POST jj.cloud.com:31235/userOnline -H 'Content-Type: application/json' -d '{&quot;url&quot;:&quot;baidu.com&quot;,&quot;users&quot;:[&quot;ua&quot;]}'
OK
</code></pre>
<p>However, if I try to use rewrite, it will fail.</p>
<pre><code>curl -X POST jj.cloud.com:31235/online/userOnline -H 'Content-Type: application/json' -d '{&quot;url&quot;:&quot;baidu.com&quot;,&quot;users&quot;:[&quot;ua&quot;]}'
&lt;html&gt;
&lt;head&gt;&lt;title&gt;404 Not Found&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;&lt;h1&gt;404 Not Found&lt;/h1&gt;&lt;/center&gt;
&lt;hr&gt;&lt;center&gt;nginx/1.21.3&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>And it will produce the following error logs:</p>
<pre><code>2021/11/03 10:21:25 [error] 134#134: *63 open() &quot;/etc/nginx/html/online/userOnline&quot; failed (2: No such file or directory), client: 172.16.0.0, server: jj.cloud.com, request: &quot;POST /online/userOnline HTTP/1.1&quot;, host: &quot;jj.cloud.com:31235&quot;
172.16.0.0 - - [03/Nov/2021:10:21:25 +0000] &quot;POST /online/userOnline HTTP/1.1&quot; 404 153 &quot;-&quot; &quot;curl/7.29.0&quot; &quot;-&quot;
</code></pre>
<p>Why the path <code>/online/userOnline</code> doesn't match <code>/online/(.*)</code> and rewrite it to <code>/userOnline</code>? Or are there some other errors?
Here is the yaml:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: online-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - host: jj.cloud.com
      http:
        paths:
          - path: /online(/|$)/(.*)
            pathType: Prefix
            backend:
              service: 
                name: online
                port: 
                  number: 80
          - path: /userOnline
            pathType: Prefix
            backend:
              service:
                name: online
                port:
                  number: 80
  ingressClassName: nginx
</code></pre>
<p>When I checked the generated nginx config, I found (default-online-ingress.conf):</p>
<pre><code>location /online(/|$)/(.*) {
</code></pre>
<p>It seems lost of the modifier for regex match, like this:</p>
<pre><code>location ~* &quot;^/online(/|$)/(.*)&quot; {
</code></pre>
<p>If it's true, how to make rewrite take effect and generate correct nginx config?</p>

tanxin
<p>If I understood your issue correctly, you have a problem with captured groups.</p>
<p>According to <a href="https://github.com/kubernetes/ingress-nginx/tree/main/docs/examples/rewrite#rewrite-target" rel="nofollow noreferrer">Nginx Rewrite targets</a> it seems to me,</p>
<ul>
<li>your patch should be <code>path: /online(/|$)(.*)</code></li>
<li>your <code>rewrite-target:</code> should be <code>rewrite-target: /$2</code></li>
<li>in addition, if you use nginx ingress, I believe you should specify that in annotation section as <code>kubernetes.io/ingress.class: nginx</code></li>
</ul>
<hr />
<p>yaml:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: online-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - host: jj.cloud.com
      http:
        paths:
          - path: /online(/|$)(.*)
            pathType: Prefix
            backend:
              service: 
                name: online
                port: 
                  number: 80
          - path: /userOnline
            pathType: Prefix
            backend:
              service:
                name: online
                port:
                  number: 80
  ingressClassName: inner-nginx
</code></pre>
