首页 Haproxy教程haproxy-自定义HAProxy页面

haproxy-ACL案例

HAProxy-实现四层负载

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

x`## 自定义HAProxy错误界面

对指定的报错进行重定向,进行优雅的显示错误页面

haproxy-自定义HAProxy页面插图

默认情况下,所有后端服务器都down机后,会显示下面页面

haproxy-自定义HAProxy页面插图1

基于错误页面文件

使用errorfile和errorloc指令,可以自定义各种错误页面

#自定义错误页 
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
    errorfile 403 /etc/haproxy/errorfiles/403forbid.http
    errorfile 503 /etc/haproxy/errorfiles/503sorry.http 

#错误页面重定向
errorloc <code> <url>
#相当于errorloc302 <code> <url>,利用302重定向至指URL

#示例:
errorloc 503 http://www.magedu.com/error_pages/503.html

范例:

defaults
#option  forwardfor
#no option http-use-htx  此设置和版本有关,2.1不支持
#...... 
#加下面行

errorfile 500  /usr/local/haproxy/html/500.http
errorfile 502  /usr/local/haproxy/html/502.http
errorfile 503  /usr/local/haproxy/html/503.http

范例:

[root@centos7 ~]#vim /etc/haproxy/haproxy.cfg
defaults
option http-keep-alive
option  forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms
errorfile 503 /apps/haproxy/html/503.http  

listen
.......

[root@centos7 ~]#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>

[root@centos7 ~]#systemctl restart haproxy
#将后端服务器down,可以观察到以下页面

haproxy-自定义HAProxy页面插图2

5.9.2:基于http重定向

[root@centos7 ~]#vim /etc/haproxy/haproxy.cfg
defaults
#option http-keep-alive
#option  forwardfor
#no option http-use-htx
#...... 加以下一行
#errorfile 503 /apps/haproxy/html/503.http
errorloc 503 http://10.0.0.8/error_page/503.html

[root@centos8 ~]#cat /var/www/html/error_page/503.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>报错页面</title>
</head>
<body>
<center><h1>网站维护中......请稍侯再试</h1></center>
<center><h2>联系电话:400-123-4567</h2></center>
<center><h3>503 Service Unavailable</h3></center>
</body>

#浏览器访问http://haproxy/ 302自动跳转至下面页面

haproxy-自定义HAProxy页面插图3

本文链接:https://www.yunweipai.com/35306.html

haproxy-ACL案例

HAProxy-实现四层负载

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部