<p>In k8s, I deploy an app running with clusterIP(port:5270). Then I want to set up an ingress-nginx to forward the request to this app. here is the configure:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: k8s-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /core
        pathType: Prefix
        backend:
          service:
            name: core-service 
            port:
              number: 5270
</code></pre>
<p>My service is a NestJS application, and this is my request to this service :</p>
<pre><code>192.168.10.131/core/api/myorder/get-order-by-id?id=1
</code></pre>
<p>&quot;myorder&quot; is one of the module I have, and I add a global prefix by using <code>app.setGlobalPrefix('api');</code> in the main.ts</p>
<p>When enter the request path, I will get :</p>
<pre><code>{&quot;statusCode&quot;:404,&quot;message&quot;:&quot;ENOENT: no such file or directory, stat '/app/client/index.html'&quot;}
</code></pre>
<p>It seems that the ingress can send the request to the pods, but it will look for &quot;/app/client/index.html&quot; which is not expected. It should access the myorder module. there is a client folder under the /app.</p>
<p>If I use the <code>Nodeport</code> to access the service instead of the ingress, everything is fine.</p>
<p><em><strong>Here is the change I have since I got your guy's solution:</strong></em></p>
<p>I deployed a simple nest app, a very simple one:
<a href="https://i.stack.imgur.com/5njbm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5njbm.png" alt="enter image description here" /></a></p>
<p>It is only has a getHalleo function.
Here is the main.js:
<a href="https://i.stack.imgur.com/8vfNt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8vfNt.png" alt="enter image description here" /></a></p>
<p>Here is the yaml for deploying at k8s:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8-ingress-test1
  labels:
    app: k8-ingress-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k8-ingress-test
  template:
    metadata:
      labels:
        app: k8-ingress-test
    spec:
      containers:
      - name: k8-ingress-test
        image: 192.168.10.145:8080/k8s-ingress-test
        imagePullPolicy: Always
        ports:
        - containerPort: 3146 
---
apiVersion: v1
kind: Service
metadata:
  name: k8-ingress-test-service
spec:
  selector:
    app: k8-ingress-test
  ports:
    - protocol: TCP
      port: 3146
      targetPort: 3146
  
</code></pre>
<p>Here is my new ingress-nginx:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: k8s-ingress
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /hello
        pathType: Prefix
        backend:
          service:
            name: k8-ingress-test-service
            port:
              number: 3146
</code></pre>
<p>I deleted &quot;nginx.ingress.kubernetes.io/rewrite-target: /&quot;.
Here is my access URL:</p>
<pre><code>192.168.10.131/hello
</code></pre>
<p>Based on my understanding, it should catch &quot;/hello&quot; and send &quot;/hello&quot; to the test app. since there is no 'hello' module in the app, I get the following :</p>
<pre><code>{&quot;statusCode&quot;:404,&quot;message&quot;:&quot;Cannot GET /hello&quot;,&quot;error&quot;:&quot;Not Found&quot;}
</code></pre>
<p>so, this is the reason why we have :</p>
<pre><code>nginx.ingress.kubernetes.io/rewrite-target: /
</code></pre>
<p>this will edit the request from &quot;/hello&quot; to &quot;/&quot;. In another word, it would send &quot;/&quot; to the test app instead of &quot;/hello&quot;. so I can hit the getHello function.</p>
<p>For the <code>192.168.10.131/core/api/myorder/get-order-by-id?id=1</code>, I can not do <code>nginx.ingress.kubernetes.io/rewrite-target: /</code> because it would rewrite the request to <code>/</code> which is wrong.</p>
<p>So, is there any way that I can catch the request including <code>core</code>, and delete the <code>core</code> part then send the <code>/api/myorder/get-order-by-id?id=1</code> to the port 5270(and it also can catch &quot;hello&quot; then delete it then send &quot;/&quot; to the port 3146)?</p>
<p>Thanks</p>

<p>I think you might have some mis-understanding between the nginx ingress's <code>rewrite-target</code> annotation and your NestJS application's <code>redirection</code>.</p>
<p>With the following annotation:</p>
<pre><code>nginx.ingress.kubernetes.io/rewrite-target: /
</code></pre>
<p>your request path <code>/core/api/myorder/get-order-by-id?id=1</code> will rewrite to <code>/</code>, not <code>/api/myorder/get-order-by-id?id=1</code>.
Check the docs for more info: <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#rewrite" rel="nofollow noreferrer">ingress-nginx annotations/#rewrite</a> and <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">examples</a>.</p>
<p>Not familiar with NestJS, I guess your application doesnot return correctly to root path <code>/</code>.</p>
<p>What you probably need is <code>Redirection</code> in a NestJS application. <a href="https://docs.nestjs.com/controllers#redirection" rel="nofollow noreferrer">NestJS Controllers Redirection</a></p>
