6、Haproxy高级功能
6.7、自定义错误页面
Haproxy自定义错误页面的方式有两种
1、有haproxy本机提供错误页面,通过errorfile参数来设定
2、将错误转到一个指定的url地址,通过errorloc参数来设定
6.7.1、errorfile
通过errorfile将错误交给本机haproxy处理
1、修改haproxy配置
cat /etc/haproxy/haproxy.cfg
defaults
...
errorfile 500 /opt/500.http #捕捉错误码为500将其重定向,由本级来提供错误页面
errorfile 502 /opt/502.http
errorfile 503 /opt/503.http
2、准备502对应的错误页面,需要添加头部信息
cat /opt/503.http
HTTP/1.1 503
Content-Type:text/html;charset=utf-8
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>报错页面</title>
</head>
<body>
<center><h1>网站维护中..请稍候再试</h1></center>
<center><h2>请联系网站管理人员</h2></center>
<center><h3>503 Service Unavailable</h3></center>
</body>
</html>
3、将后端的web服务器停止后测试
6.7.2、errorloc
使用errorloc选项来捕捉错误状态码,然后将其重定向到其他url
1、修改haproxy配置
cat /etc/haproxy/haproxy.cfg
defaults
...
errorloc 503 https://www.xuliangwei.com/error/
2、测试访问,会发现请求被重定向到新的uri