docs: Improve handle_errors docs; add examples

This commit is contained in:
Matthew Holt 2020-06-03 12:19:30 -06:00
parent 1d636ee43c
commit 5f028df5dc
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -6,7 +6,9 @@ title: handle_errors (Caddyfile directive)
Sets up error handlers.
When the normal HTTP request handlers return an error, normal processing stops and the error handlers are invoked. Error handlers form a route which is just like normal routes, and they can do anything that normal routes can do. This enables great control and flexibility when handling errors during HTTP requests.
When the normal HTTP request handlers return an error, normal processing stops and the error handlers are invoked. Error handlers form a route which is just like normal routes, and they can do anything that normal routes can do. This enables great control and flexibility when handling errors during HTTP requests. For example, you can serve static error pages, templated error pages, or reverse proxy to another backend to handle errors.
A request's context is carried into error routes, so any values set on the request context such as [site root](/docs/caddyfile/directives/root) will be preserved in error handlers, too. Additionally, new placeholders are available when handling errors. [The JSON docs for an HTTP server's error routes](/docs/json/apps/http/servers/errors/#routes) describe these placeholders. The `handle_errors` directive simply adds error routes, so you can use those placeholders within a `handle_errors` block.
## Syntax
@ -17,3 +19,34 @@ handle_errors {
```
- **<directives...>** is a list of HTTP handler directives, directive blocks, or matchers; one per line.
## Examples
Custom error pages based on the status code (i.e. a page called `404.html` for 404 errors):
```caddy
handle_errors {
rewrite * /{http.error.status_code}.html
file_server
}
```
A single error page that uses [`templates`](/docs/caddyfile/directives/templates) to write a custom error message:
```caddy
handle_errors {
rewrite * /error.html
templates
file_server
}
```
Reverse proxy to a local backend equipped to handle errors:
```caddy
handle_errors {
reverse_proxy localhost:9000
}
```