2018-04-24 10:18:29 +00:00
# Custom Errors
2018-06-12 22:03:48 +00:00
This example demonstrates how to use a custom backend to render custom error pages.
2017-03-08 12:02:13 +00:00
2022-01-12 14:30:37 +00:00
If you are using Helm Chart, look at [example values ](https://github.com/kubernetes/ingress-nginx/blob/main/docs/examples/customization/custom-errors/custom-default-backend.helm.values.yaml ) and don't forget to add [configMap ](https://github.com/kubernetes/ingress-nginx/blob/main/docs/examples/customization/custom-errors/custom-default-backend-error_pages.configMap.yaml ) to your deployment, otherwise continue with [Customized default backend ](#customized-default-backend ) manual deployment.
2018-06-12 22:03:48 +00:00
## Customized default backend
2017-03-08 12:02:13 +00:00
2021-12-06 20:01:33 +00:00
First, create the custom `default-backend` . It will be used by the Ingress controller later on.
To do that, you can take a look at the [example manifest ](https://github.com/kubernetes/ingress-nginx/blob/main/docs/examples/customization/custom-errors/custom-default-backend.yaml )
in this project's GitHub repository.
2017-03-08 12:02:13 +00:00
```
$ kubectl create -f custom-default-backend.yaml
service "nginx-errors" created
2018-06-12 22:03:48 +00:00
deployment.apps "nginx-errors" created
2017-03-08 12:02:13 +00:00
```
2018-06-12 22:03:48 +00:00
This should have created a Deployment and a Service with the name `nginx-errors` .
2017-03-08 12:02:13 +00:00
```
2018-06-12 22:03:48 +00:00
$ kubectl get deploy,svc
NAME DESIRED CURRENT READY AGE
deployment.apps/nginx-errors 1 1 1 10s
2017-03-08 12:02:13 +00:00
2018-06-12 22:03:48 +00:00
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-errors ClusterIP 10.0.0.12 < none > 80/TCP 10s
2017-03-08 12:02:13 +00:00
```
2018-06-12 22:03:48 +00:00
## Ingress controller configuration
2023-05-05 16:31:13 +00:00
If you do not already have an instance of the Ingress-Nginx Controller running, deploy it according to the
2018-06-12 22:03:48 +00:00
[deployment guide][deploy], then follow these steps:
2020-05-17 18:27:56 +00:00
1. Edit the `ingress-nginx-controller` Deployment and set the value of the `--default-backend-service` flag to the name of the
2018-06-12 22:03:48 +00:00
newly created error backend.
2020-05-17 18:27:56 +00:00
2. Edit the `ingress-nginx-controller` ConfigMap and create the key `custom-http-errors` with a value of `404,503` .
2018-06-12 22:03:48 +00:00
2023-05-05 16:31:13 +00:00
3. Take note of the IP address assigned to the Ingress-Nginx Controller Service.
2018-06-12 22:03:48 +00:00
```
$ kubectl get svc ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx ClusterIP 10.0.0.13 < none > 80/TCP,443/TCP 10m
```
2020-02-09 23:50:27 +00:00
!!! note
2018-06-12 22:03:48 +00:00
The `ingress-nginx` Service is of type `ClusterIP` in this example. This may vary depending on your environment.
Make sure you can use the Service to reach NGINX before proceeding with the rest of this example.
[deploy]: ../../../deploy/
## Testing error pages
Let us send a couple of HTTP requests using cURL and validate everything is working as expected.
A request to the default backend returns a 404 error with a custom message:
2017-03-08 12:02:13 +00:00
```
2018-06-12 22:03:48 +00:00
$ curl -D- http://10.0.0.13/
HTTP/1.1 404 Not Found
Server: nginx/1.13.12
Date: Tue, 12 Jun 2018 19:11:24 GMT
Content-Type: */*
Transfer-Encoding: chunked
Connection: keep-alive
2017-03-08 12:02:13 +00:00
2018-06-12 22:03:48 +00:00
< span > The page you're looking for could not be found.< / span >
2017-03-08 12:02:13 +00:00
```
2018-06-12 22:03:48 +00:00
A request with a custom `Accept` header returns the corresponding document type (JSON):
2017-03-08 12:02:13 +00:00
```
2018-06-12 22:03:48 +00:00
$ curl -D- -H 'Accept: application/json' http://10.0.0.13/
HTTP/1.1 404 Not Found
Server: nginx/1.13.12
Date: Tue, 12 Jun 2018 19:12:36 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
2017-03-08 12:02:13 +00:00
2018-06-12 22:03:48 +00:00
{ "message": "The page you're looking for could not be found" }
2017-03-08 12:02:13 +00:00
```
2018-06-12 22:03:48 +00:00
To go further with this example, feel free to deploy your own applications and Ingress objects, and validate that the
responses are still in the correct format when a backend returns 503 (eg. if you scale a Deployment down to 0 replica).