<p>I have problem setting path ingress controller for backend service. For example i want setup :</p>

<ol>
<li>frontend app with angular (Path :/)  </li>
<li>backend service with NodeJs (Path :/webservice).</li>
</ol>

<p><strong>NodeJS : Index.js</strong></p>

<pre><code>const express = require('express')
const app = express()
const port = 4000

app.get('/', (req, res) =&gt; res.send('Welcome to myApp!'))

app.use('/data/office', require('./roffice'));
app.listen(port, () =&gt; console.log(`Example app listening on port ${port}!`))
</code></pre>

<p><strong>Another Route:roffice.js</strong></p>

<pre><code>var express = require('express')
var router = express.Router()

router.get('/getOffice', async function (req, res) {
   res.send('Get Data Office')
}); 

module.exports = router
</code></pre>

<p><strong>Deployment.yaml</strong></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: ws-stack
spec:
  selector:
   matchLabels:
     run: ws-stack
  replicas: 2
  template:
    metadata:
      labels:
      run: ws-stack
  spec:
     containers:
     - name: ws-stack
       image: wsstack/node/img
       imagePullPolicy: IfNotPresent
       ports:
         - containerPort: 4000
</code></pre>

<p><strong>Service.yaml</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: service-wsstack
   labels:
    run: service-wsstack
  spec:
   type: NodePort
   ports:
   - port: 80
     protocol: TCP
     nodePort: 30009
     targetPort: 4000
   selector:
     run: ws-stack
</code></pre>

<p><strong>ingress.yaml</strong></p>

<pre><code> apiVersion: networking.k8s.io/v1beta1
 kind: Ingress
 metadata:
   name: stack-ingress
   annotations:
     nginx.ingress.kubernetes.io/rewrite-target: /
     nginx.ingress.kubernetes.io/use-regex: "true"
  spec:
   rules:
    - host: hello-world.info
    - http:
        paths:
        - path: /
          backend:
            serviceName: service-ngstack --&gt; frondend
            servicePort: 80
        - path: /webservice
          backend:
            serviceName: service-wsstack --&gt; backend
            servicePort: 80
</code></pre>

<p>i setup deployment, service and ingress successfully. but when i called with curl</p>

<pre><code>curl http://&lt;minikubeip&gt;/webservice  --&gt; Welcome to myApp! =&gt; Correct
curl http://&lt;minikubeip&gt;/webservice/data/office/getOffice --&gt; Welcome to myApp! =&gt; Not correct
</code></pre>

<p>if i called another route, the result is the same 'Welcome to myApp'. But if i used Nodeport </p>

<pre><code>curl http://&lt;minikubeip&gt;:30009/data/office/getOffice =&gt; 'Get Data Office', working properly.
</code></pre>

<p>What is the problem? any solution? Thank you</p>

sembilanlangit
<h1>TL;DR</h1>

<ul>
<li><code>nginx.ingress.kubernetes.io/rewrite-target: /$2</code></li>
<li><code>path: /webservice($|/)(.*)</code></li>
</ul>

<h1>Explanation</h1>

<p>The problem is from that line in your ingress:</p>

<p><code>nginx.ingress.kubernetes.io/rewrite-target: /</code></p>

<p>You're telling nginx to rewrite your url to <code>/</code> whatever it matched.</p>

<ul>
<li><code>/webservice</code> => <code>/</code></li>
<li><code>/webservice/data/office/getOffice</code> => <code>/</code></li>
</ul>

<p>To do what you're trying to do <strong>use regex</strong>, here is a simple example:</p>

<pre><code> apiVersion: networking.k8s.io/v1beta1
 kind: Ingress
 metadata:
   name: stack-ingress
   annotations:
     nginx.ingress.kubernetes.io/rewrite-target: /$2
     nginx.ingress.kubernetes.io/use-regex: "true"
  spec:
   rules:
    - host: hello-world.info
    - http:
        paths:
        - path: /
          backend:
            serviceName: service-ngstack --&gt; frondend
            servicePort: 80
        - path: /webservice($|/)(.*)
          backend:
            serviceName: service-wsstack --&gt; backend
            servicePort: 80
</code></pre>

<p>This way you're asking nginx to rewrite your url with the second matching group.
Finally it gives you:</p>

<ul>
<li><code>/webservice</code> => <code>/</code></li>
<li><code>/webservice/data/office/getOffice</code> => <code>/data/office/getOffice</code></li>
</ul>
