<p>I have two services hosted on different ports and I have created a ingress resource which looks like this</p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
  namespace: mynamespace
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
      - path: /svc1/
        backend:
          serviceName: app1-svc
          servicePort: 3000
       - path: /svc2/
         backend:
           serviceName: app2-svc
           servicePort: 8080
</code></pre>

<p>on top of this I have created a <strong><em>NodePort</em></strong> type ingress controller.</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-ingress
  name: controller-nginx-ingress-controller
spec:
  clusterIP: 10.88.18.191
  externalTrafficPolicy: Cluster
  ports:
  - name: http
    nodePort: 30080
    port: 80
    protocol: TCP
    targetPort: http
  - name: https
    nodePort: 31442
    port: 443
    protocol: TCP
    targetPort: https
  selector:
    app: nginx-ingress
    component: controller
    release: controller
</code></pre>

<p>And finally, setup a cloud load balancer to access application running on my K8S cluster.</p>

<p><strong>Problem:</strong>
I couldn't able to access any of my application using URL routing</p>

<ul>
<li><a href="http://load-balancer-ip/svc1/" rel="nofollow noreferrer">http://load-balancer-ip/svc1/</a></li>
<li><a href="http://load-balancer-ip/svc2/" rel="nofollow noreferrer">http://load-balancer-ip/svc2/</a></li>
</ul>

<p>Can anyone please let me know what incorrect I'm doing? And how to resolve this issue?</p>

Geeky Ninja
<p>From what you mentioned in comments I am pretty sure the problem can be solved with path rewrites.</p>

<p>Now, when you are sending a request to <code>/svc1/</code> with <code>path: /svc1/</code> then the request is forwarded to <code>app1-svc</code> with path set to <code>/svc1/</code> and you are receiving 404 errors as there is no such path in app1. From what you mentioned, you can most probably solve the issue using rewrite. You can achieve it using <code>nginx.ingress.kubernetes.io/rewrite-target</code> annotation, so your ingress would look like following:</p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
  namespace: mynamespace
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
      - path: /svc1(/|$)(.*)
        backend:
          serviceName: app1-svc
          servicePort: 3000
       - path: /svc2(/|$)(.*)
         backend:
           serviceName: app2-svc
           servicePort: 8080
</code></pre>

<p>In this case when sending request with path set to <code>/svc1/something</code> the request will be forwarded to app1 with path rewritten to <code>/something</code>.</p>

<p>Also take a look in <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">ingress docs for more explanation</a>.</p>

<p>Let me know if it solved you issue.</p>
