Nginx Ingress Controller Returns 404 for path other than '/' AKS - Kubernetes 1.17
Your defined route is /health
not /api/health
.
The path you define in your Ingress
, must be handled by your back-end server.
Your Ingress
sends all your requests prefixed with /api
to your service backend-webapi
, but it doesn't discard the path /api
from the route itself.
So, when you send request to http://ingress-domain.westeurope.cloudapp.azure.com.westeurope.cloudapp.azure.com/api/health, your service must handle /api/health
not /health
.
By default nginx sends traffic to whatever is mentioned in path.You can use rewrite-target
annotation to specify target URI where the traffic must be redirected.
# singlebackend-ingress-rule.yamlapiVersion: networking.k8s.io/v1beta1 # AKS is with 1.17, and supports only apiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata: name: ingress-rules annotations: nginx.ingress.kubernetes.io/rewrite-target: /spec: rules: - host: ingress-domain.westeurope.cloudapp.azure.com http: paths: - backend: serviceName: backend-webapi servicePort: 80 path: /api
As an alternative to rewrite-target
you could use app-root annotation
# singlebackend-ingress-rule.yamlapiVersion: networking.k8s.io/v1beta1 # AKS is with 1.17, and supports only apiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata: name: ingress-rules annotations: nginx.ingress.kubernetes.io/app-root: /spec: rules: - host: ingress-domain.westeurope.cloudapp.azure.com http: paths: - backend: serviceName: backend-webapi servicePort: 80 path: /api
With above change below curl should work without needing to have a http handler handling /api/health
in the backend. You can simply have a http handler at /health
curl http://ingress-domain.westeurope.cloudapp.azure.com.westeurope.cloudapp.azure.com/health