<p>I have been trying my Nginx Path-Based routing to work, however, after spending almost 4 hours, I am failed to understand, why is it not working. I have gone through almost every possible answer on StackOverflow before anyone downgrades my question, but none worked for me.</p>

<p>So here what I did:</p>

<ol>
<li><p>I installed nginx-ingress using Helm 3 (<a href="https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-helm/" rel="nofollow noreferrer">https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-helm/</a>) in a separate namespace - nginx-test:</p>

<p><code>helm install my-release nginx-stable/nginx-ingress</code></p></li>
</ol>

<p>A version of the ingress controller (<a href="https://hub.helm.sh/charts/nginx-edge/nginx-ingress" rel="nofollow noreferrer">https://hub.helm.sh/charts/nginx-edge/nginx-ingress</a>):</p>

<pre><code>$ POD_NAME=$(kubectl get pods -l app=nginx-controller-nginx-ingress -o jsonpath='{.items[0].metadata.name}')
$ 
$ kubectl exec -it $POD_NAME -- /nginx-ingress --version
Version=edge GitCommit=50e908aa
$ 
</code></pre>

<ol start="2">
<li><p>There are 2 basic nginx deployments, 2 services already configured in the same namespace, and working fine when I configure host-based routing for them.</p></li>
<li><p>Below one works fine for me (when I define host-based routing and get the required page index.html when I run both individual URLs):</p></li>
</ol>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: nginx1.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx1
          servicePort: 80

  - host: nginx2.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx2
          servicePort: 80
</code></pre>

<p>Now I wanted to achieve the same result using Path-Based routing, where there will be 1 URL and 2 Paths /nginx1 (pointing to nginx1 service) and /nginx2 (pointing to nginx2 service). So I configured the below ingress resource (and many permutations and combinations I applied based on different examples on internet), none of them worked for me.</p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-path-based
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: nginx.example.com
    http:
      paths:
      - path: /nginx1
        backend:
          serviceName: nginx1
          servicePort: 80
      - path: /nginx2
        backend:
          serviceName: nginx2
          servicePort: 80
</code></pre>

<p>When I access services directly, it works fine, however when I try to access - <code>curl http://nginx.example.com/nginx1</code> or <code>curl http://nginx.example.com/nginx2</code> - I get <code>404 Not Found</code> error.</p>

<p>I was expecting to receive the same response which I was getting for Host-Based routing. But it does not seem to work.</p>

<p>So finally I had to install the controller using manifests, instead of helm charts (edge version).</p>

<p>I installed it from here (<a href="https://kubernetes.github.io/ingress-nginx/deploy/#bare-metal" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/deploy/#bare-metal</a>), changed NodePort to LoadBalancer to get a LoadBalancer IP. I am using <a href="https://metallb.universe.tf/" rel="nofollow noreferrer">MetalLB</a> on BareMetal.</p>

<pre><code>$ POD_NAME=$(kubectl get pods -n $POD_NAMESPACE -l app.kubernetes.io/component=controller -o jsonpath='{.items[0].metadata.name}')
$ kubectl exec -it $POD_NAME -n $POD_NAMESPACE -- /nginx-ingress-controller --version
-------------------------------------------------------------------------------
NGINX Ingress controller
  Release:       0.32.0
  Build:         git-446845114
  Repository:    https://github.com/kubernetes/ingress-nginx
  nginx version: nginx/1.17.10

-------------------------------------------------------------------------------

$ 
</code></pre>

<p>My Ingress resource looks like the same which I posted while asking the question.</p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-path-based
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: nginx.gofork8s.com
    http:
      paths:
      - path: /nginx1
        backend:
          serviceName: nginx1
          servicePort: 80
      - path: /nginx2
        backend:
          serviceName: nginx2
          servicePort: 80
</code></pre>

<p>Modified the new LoadBalancer IP in /etc/hosts file to get the domain work.</p>

<pre><code>192.168.0.1 nginx.example.com
</code></pre>

<p>Now I am able to access - <a href="http://nginx.example.com/nginx1" rel="nofollow noreferrer">http://nginx.example.com/nginx1</a> and <a href="http://nginx.example.com/nginx2" rel="nofollow noreferrer">http://nginx.example.com/nginx2</a>.</p>

<p>I hope it will help someone. I still need to figure out settings with Helm Charts.</p>
