<p>I deployed a test service that's called &quot;web-server-service&quot;. That is connected to a container which when working just displayed a message on the screen (like a browser, etc): &quot;It works!&quot;. Purley as a connectivity test.</p>
<p><a href="https://i.stack.imgur.com/pYVjx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pYVjx.png" alt="enter image description here" /></a></p>
<p>Ingress controller = Nginx.
All of this is running in AWS.</p>
<p>I have set up an ingress controller and created an ingress resource</p>
<pre><code>apiVersion: v1
kind: Namespace
metadata:
  name: web
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-server
  namespace: web
spec:
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: httpd
        image: httpd:2.4.48-alpine3.14
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web-server-service
  namespace: web
spec:
  selector:
    app: web
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 80

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-server-ingress
  namespace: web
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
spec:
  ingressClassName: nginx
  rules:
  - host: somehost.com
    http:
      paths:
      - path: / #this works
        pathType: Prefix
        backend:
          service:
            name: web-server-service
            port:
              number: 5000
      - path: /test #this does not work. returns 404
        pathType: Prefix
        backend:
          service:
            name: web-server-service
            port:
              number: 5000
</code></pre>
<p>The problem I am having is that as long as I keep the path as just &quot;/&quot; then it works when I connect through the browser, curl, etc. But as soon as I actually add in a different path like &quot;/test&quot;, it sends back a 404.
It is almost like when it does the poxy_pass it adds that path to the end of the proxy_path url, making it not find the correct destination.</p>
<p>I expect this to act like a proxy to send the request to a service. In this case, it is the same service but eventually, it will be different services that are running.</p>
<p><strong>Using Rewrite:</strong></p>
<p>I understand that there is some rewrite functionality but I could not get it to work. It still returns the same 404. This is the ingress config I tried for that:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-server-ingress
  namespace: web
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
  - host: somehost.com
    http:
      paths:
      - path: / #this works
        pathType: Prefix
        backend:
          service:
            name: web-server-service
            port:
              number: 5000
      - path: /test(/|$)(.*) #this still does not work. returns 404
        pathType: Prefix
        backend:
          service:
            name: web-server-service
            port:
              number: 5000
</code></pre>
<p>Perhaps my rewrite is incorrect?</p>
<p>I essentially just want it to pass the request along to the service and not add in any paths when it does. I expect both the path of &quot;/&quot; and &quot;/test&quot; to be able to display the &quot;It works!&quot; message.</p>
<p>Has anyone got any input on what I am doing wrong? Thanks</p>

Gene Smith
<p><strong>Debug Steps:</strong></p>
<ol>
<li>Check your ingress controller pod logs to see what request is it actually making in the background due to which you are getting 404.</li>
</ol>
<hr />
<p>Details w.r.t. Ingress to get the rewrite-target functionality along with the use-cases</p>
<pre><code>$ echo '
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80
' | kubectl create -f -
</code></pre>
<p>In this ingress definition, any characters captured by <code>(.*)</code> will be assigned to the placeholder <code>$2</code>, which is then used as a parameter in the rewrite-target annotation.</p>
<p>For example, the ingress definition above will result in the following rewrites:</p>
<pre><code>rewrite.bar.com/something rewrites to rewrite.bar.com/
rewrite.bar.com/something/ rewrites to rewrite.bar.com/
rewrite.bar.com/something/new rewrites to rewrite.bar.com/new
</code></pre>
<p>Source: <a href="https://github.com/kubernetes/ingress-nginx/tree/main/docs/examples/rewrite" rel="nofollow noreferrer">https://github.com/kubernetes/ingress-nginx/tree/main/docs/examples/rewrite</a></p>
