自定义HAProxy错误界面: HAProny自定义错误页面有两种方式,一种是由haproxy本机提供错误页面,
第二种是将错误重定向到一台专用来提供错误界面的sorry server上来提供错误页面
1.基于错误页面文件:
errorfile <code> <file>
<code> #HTTP status code.支持200, 400, 403, 405, 408, 425, 429, 500, 502,503,504
<file> #包含完整HTTP响应的错误页文件的绝对路径。 建议后缀“ .http”,以和一般的html文件相区分
示例:errorfile 400 /etc/haproxy/errorfiles/400badreq.http
例子:
defaults
option http-keep-alive
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
errorfile 503 /usr/local/haproxy/html/503.http
#测试中发现一个有问题,这个文件不能随便写个文本,要html格式。否则会提示下面信息 proxy 'zzhz': invalid message for HTTP return code 500: unabled to parse headers (error offset: 0).
# vim /apps/haproxy/html/503.http
HTTP/1.1 503 Service Unavailable
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>联系电话:400-123-4567</h2></center>
<center><h3>503 Service Unavailable</h3></center>
</body>
2.基于http重定向:
当由sorry server来提供错误页面时,就需要使用errorloc 选项来捕捉错误状态码,然后将其重定向到后端的sorry server上,由sorry server来提供错误页面
errorloc <code> <url> #相当于errorloc302 <code> <url>,利用302重定向至指URL
示例:errorloc 503 http://www.magedu.com/error_pages/503.html
defaults
option http-keep-alive
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
errorloc 503 https://www.baidu.com