how to make ingress nginx return 200 on each request

9/5/2019

I want my ingress nginx to return 200 on every request. If I had access to the nginx configuration, I would have done something like this:

location = /health {
  return 200;
}

But I'm not sure how to do it in ingress configuration YAML

-- Alex L
kubernetes-ingress
nginx-ingress

1 Answer

9/6/2019

Consider that the Kubernetes' ingress object, when using the Nginx controller, is mostly meant to do routing instead of serving requests by itself. What is actually serving them is the backend deployed in the cluster and, this part is what is returning the status codes, not the ingress.

The controller has a feature something similar to what you want, but for errors only. This only makes the ingress to add some headers so that, a backend can interpret them an return some non-standard code response.

It might be possible to make them respond 200 if you modify this backend. However, I find less disruptive and more straight-forward to just "catch-all" all the incoming requests in the ingress to redirect them to a custom Nginx backend that always responds 200 (you already have the Nginx configuration for that):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/use-regex: "true"
  labels:
    app: all-good
  name: happy-ingress
spec:
  rules:
  - host: "*"
    http:
      paths:
      - path: /(.*)
        backend:
          serviceName: ok-status-test
          servicePort: 80

With this approach, you can even add non-200's backends to it, and match them using regex, so the ingress can be fully reusable.

-- yyyyahir
Source: StackOverflow