<p>I'm using Ubuntu 20.04.2 LTS. I installed microk8s 1.20.6 rev 2143 and experimenting with ingress. I must be missing something - but it doesn't work as I expect it to. I tracked the strange behavior down to the following configuration:</p>
<p>ingress.yaml:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ubuntu
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: my-ubuntu
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
      - path: /nginx
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
</code></pre>
<p>nginx-service.yaml:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  ports:
    - port: 80
      name: http
    - port: 443
      name: https
  type: ClusterIP
  selector:
    app: nginx
</code></pre>
<p>Now,</p>
<pre class="lang-sh prettyprint-override"><code>curl my-ubuntu/                     # this returns Welcome page, as expected
curl my-ubuntu/nginx                # this returns Welcome page, as expected
curl my-ubuntu/bad-page.html        # this returns 404 Not Found, as expected
curl my-ubuntu/nginx/bad-page.html  # this returns Welcome page. WHY?
</code></pre>
<p>Any request under my-ubuntu/nginx/* returns Welcome page, even when the url is correct and should have returned different content. Did I configure something wrong?</p>
<p>I was able to reproduce the same strange behavior using Docker for Windows + WSL2 + Ubuntu + ingress installed using:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.41.2/deploy/static/provider/cloud/deploy.yaml
</code></pre>
<p><strong>EDIT</strong></p>
<p>nginx-deployment.yaml I used:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  revisionHistoryLimit: 0
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx
</code></pre>
<p>When I try <code>/nginx/</code> instead of <code>/nginx</code> like @HarshManvar suggested, I get this behavior:</p>
<pre class="lang-sh prettyprint-override"><code>curl my-ubuntu/                     # this returns Welcome page, as expected
curl my-ubuntu/bad-page.html        # this returns 404 Not Found, as expected
curl my-ubuntu/nginx                # this returns 404 Not Found
curl my-ubuntu/nginx/               # this returns Welcome page
curl my-ubuntu/nginx/bad-page.html  # this returns Welcome page
</code></pre>
<p><a href="https://kubernetes.io/docs/concepts/services-networking/ingress/#simple-fanout" rel="nofollow noreferrer">Kubernetes Ingress documentation about Simple fanout</a> also does use <code>/nginx</code> pattern but not working as described above.</p>

kennethc
<p><a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/examples/rewrite/</a> explains how to use <code>rewrite-target</code> annotation. I was able to make it work with the following ingress.yaml:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ubuntu
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /(.*)
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
      - path: /nginx($|/.*)
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
</code></pre>
<p>Each <code>path</code> defines regular expression with <code>( )</code>, which yields <code>$1</code>, <code>$2</code>, etc., aka. regex capture group variables. Now you put <code>rewrite-target</code> using those variables, and that will be the actual URL that is passed to the service's container that handles the request.</p>
<p>Maybe there is another way, but this is the only way I was able to make it work.</p>
