How to set ingress-nginx custom errors

8/8/2019

I use the kubernetes ingress-nginx controller and a set custom errors on GKE but I have some problems.

Goal: If a 50x error occurs in something-web-app, I'll return the HTTP status code 200 and JSON {"status":200, "message":"ok"}

Problems:

  1. I have read the custom-errors document but there is no example of how to customize the default-backend.

  2. I do not understand the difference between ConfigMap and Annotation.

  3. How does ingress-nginx controller work in the first place.

-- Kentaro Usui
google-kubernetes-engine
kubernetes
nginx-ingress

1 Answer

8/8/2019

You can do it using two way :

  1. Adding annotation in ingress
  2. Change in ingress controller configmap (which is more like beckend)

1. Try adding this annotation to kubernetes ingress :

nginx.ingress.kubernetes.io/default-backend: nginx-errors-svc

nginx.ingress.kubernetes.io/custom-http-errors: 404,503

nginx.ingress.kubernetes.io/default-backend: error-pages

If that doesn't work add this along with two :

nginx.ingress.kubernetes.io/server-snippet: |
      location @custom_503 {
        return 404;
      }
      error_page 503 @custom_503;

2. Configmap editing

You can apply this config map to ingress controller

apiVersion: v1
kind: ConfigMap
name: nginx-configuration-ext
data:
  custom-http-errors: 502,503,504
  proxy-next-upstream-tries: "2"
  server-tokens: "false"

You can also refer this blog : https://habr.com/ru/company/flant/blog/445596/

-- Harsh Manvar
Source: StackOverflow