🎉 DevOps Interview Prep Bundle is live — 1000+ Q&A across 20 topicsGet it →
All Fixes
Today I Fixed

Nginx Ingress returning 502 Bad Gateway — wrong targetPort in Service

kubernetesJun 29, 202620 minutes to fixkubernetesnetworkingtroubleshooting

Nginx ingress was returning 502 Bad Gateway for every request. The pod was Running, the Ingress resource looked fine, and kubectl describe ingress showed no obvious errors.

Root cause: The Service was pointing port: 8080targetPort: 8080, but the Node.js app inside the container was actually listening on port 3000. Nginx couldn't reach the backend, so it returned 502.

Fix:

First, verify what port the app is really on:

bash
kubectl exec -it <pod-name> -- curl localhost:3000

Then fix the Service spec:

yaml
# service.yaml — corrected targetPort
spec:
  ports:
    - port: 80
      targetPort: 3000   # was 8080 — must match EXPOSE in Dockerfile
  selector:
    app: my-app

Apply and test:

bash
kubectl apply -f service.yaml
kubectl exec -it <pod-name> -- curl http://my-service.default.svc.cluster.local

Lesson: 502 from Nginx almost always means the Service can't reach the pod — check targetPort matches the actual port the app listens on, not just what's in the Dockerfile EXPOSE.