<p>I have an application that has two k8s deployments called <code>onboarding-server</code> and <code>frontend</code>
onboarding-server is a node server and frontend is a react application that is built and deployed using Nginx. </p>

<p>Here is a snippet which is returned when I run  <code>kubectl get all</code></p>

<pre><code>NAME                                       READY   STATUS    RESTARTS   AGE
pod/frontend-deployment-578f898ffb-cc7gc   1/1     Running   0          15s
pod/node-deployment-7f4754fdf5-fnmls       1/1     Running   0          7d10h

NAME                        TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)          AGE
service/frontend-service    LoadBalancer   10.100.200.64    10.167.198.105   80:31167/TCP     13d
service/node-service   LoadBalancer   10.100.200.71    10.167.199.136   3200:32276/TCP   13d

NAME                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/frontend-deployment   1/1     1            1           5d12h
deployment.apps/node-deployment       1/1     1            1           7d10h

NAME                                             DESIRED   CURRENT   READY   AGE
replicaset.apps/frontend-deployment-578f898ffb   1         1         1       17s
replicaset.apps/node-deployment-7f4754fdf5       1         1         1       7d10h
</code></pre>

<p>And my <code>nginx.conf</code> config looks something like this</p>

<pre><code>upstream node-service{
  server node-service;
}

server {

    listen 80;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }


    location = /api {
      proxy_pass http://node-service;
    }

    error_page 404 /index.html;
    location = / {
      root /usr/share/nginx/html;
      internal;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
}
</code></pre>

<p>Now I try to talk to my backend service from the frontend using this URL</p>

<p><code>/api/onboarding/v1/user/login</code></p>

<p>My understanding of Nginx is that when I request <code>/api</code> it should redirect me to 
<code>http://node-service/api/onboarding/v1/user/login</code>
and hence should return the appropriate response.
But what I get back is</p>

<pre><code>Fetch failed loading: POST
 http://192.168.5.21/api/onboarding/v1/user/login
</code></pre>

<p>How can I connect to node-service from Nginx?</p>

<p>EDIT 1 :
One thing that I forgot to mention is that when I go inside the container using the <code>exec</code> command I am able to get appropriate response using </p>

<pre><code>curl http://192.168.5.21/api/onboarding/v1/user/login
</code></pre>

Vidhyanshu jain
<p>For path-based routing in Kubernetes, I'll suggest you to take a look at <strong>Ingress</strong>. You can use <strong>Nginx Ingress</strong> for kubernetes in your case. You'll be able to map services based on path.</p>

<p>For Example,</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: demo-ingress
  annotations: {
    'kubernetes.io/ingress.class': nginx
  }
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /foo
        backend:
          serviceName: demo-backend
          servicePort: 80
</code></pre>

<p>Now, as you are using nginx as a reverse-proxy, you can expose your backend service as a Type NodePort and use the External IP of your backend service instead of using the service name-</p>

<pre><code>location = /api {
  proxy_pass http://external-ip:port;
}
</code></pre>
