在Caddy2内,使用handle_errors以自定义错误显示页面

示例

1.以下是使用/root/data/caddy/pages/errors内存储的静态页面处理页面错误

1
2
3
4
5
handle_errors {
    rewrite * /{err.status_code}.html #重定向到 /<错误代码>.html
    root * /root/data/caddy/pages/errors #静态文件目录
    file_server
}

2.使用http.cat的http状态图以处理错误页面

1
2
3
4
5
6
7
handle_errors {
	rewrite * /{err.status_code} 重定向到 /<错误代码>
	reverse_proxy https://http.cat {
		header_up Host {upstream_hostport}
		replace_status {err.status_code}
	}
}

3.使用不同页面处理不同的页面错误

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
handle_errors {
	@404-410 `{err.status_code} in [404, 410]`
	handle @404-410 {
		respond "It's a 404 or 410 error!"
	}

	@5xx `{err.status_code} >= 500 && {err.status_code} < 600`
	handle @5xx {
		respond "It's a 5xx error."
	}

	handle {
		respond "It's another error"
	}
}