HAProxy的高级配置选项-自定义错误页面

简介: 这篇文章介绍了如何在HAProxy中配置自定义错误页面,通过修改配置文件指定不同HTTP状态码对应的错误页面路径,并展示了在后端服务不可用时如何向用户展示友好的错误提示。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.安装Apache Httpd及准备测试数据

1>.试验架构说明

  node102.yinzhengjie.org.cn:
    Haproxy服务器

  node105.yinzhengjie.org.cn:
    测试服务器,模拟客户端

  node106.yinzhengjie.org.cn:
    Apache httpd服务器

  node107.yinzhengjie.org.cn:
    Apache httpd服务器

  node108.yinzhengjie.org.cn:
    Apache httpd服务器

2>.安装Apache httpd服务

  此过程相对简单,我这里就直接略过了,可参考我之前的笔记:https://www.cnblogs.com/yinzhengjie/p/12114195.html

二.HAProxy自定义错误页面案例实战

1>.编辑haproxy的配置文件

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /yinzhengjie/softwares/haproxy
    stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
    nbthread 2
    pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
    log 127.0.0.1 local5 info

defaults
    option http-keep-alive
    option  forwardfor
    option redispatch
    option abortonclose
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms
    #自定义错误页面跳转,需要在本地系统存在哟
    errorfile 500 /yinzhengjie/softwares/haproxy/html/500.html
    errorfile 502 /yinzhengjie/softwares/haproxy/html/502.html
    errorfile 503 /yinzhengjie/softwares/haproxy/html/503.html

listen status_page
    bind 172.30.1.102:8888
    stats enable
    stats uri /haproxy-status
    stats auth    admin:yinzhengjie
    stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
    stats hide-version
    stats admin if TRUE
    stats refresh 5s

frontend WEB_PORT_80
    bind 172.30.1.102:80
    mode http
    acl my_pc_page hdr_dom(host) -i pc.yinzhengjie.org.cn
    acl firefox_agent hdr(User-Agent) -m sub -i "Firefox"
    redirect prefix https://www.cnblogs.com/yinzhengjie/ if my_pc_page
    use_backend firefox_web if firefox_agent
    default_backend backup_web

backend firefox_web
    server web01 172.30.1.106:80  check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80  check inter 3000 fall 3 rise 5

backend backup_web
    server web03 172.30.1.108:80  check inter 3000 fall 3 rise 5 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy              #别忘记了重启haproxy服务使得配置生效哟~可以查看状态页观察服务是否正常,如下图所示。
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

2>.在haproxy节点准备测试的错误页面(实际工作中我们应该让开发来准备,运维一把情况下并不负责页面开发)

[root@node102.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/haproxy/
total 4
drwxr-xr-x 3 root root 21 Jan  1 11:41 doc
-rw-r--r-- 1 root root  6 Jan  5 16:44 haproxy.pid
srw------- 1 root root  0 Jan  5 16:44 haproxy.sock
drwxr-xr-x 2 root root 21 Jan  1 11:41 sbin
drwxr-xr-x 3 root root 17 Jan  1 11:41 share
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/softwares/haproxy/html
mkdir: created directory ‘/yinzhengjie/softwares/haproxy/html’
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/haproxy/html/
total 12
-rw-r--r-- 1 root root 795 Jan  5 17:21 500.html
-rw-r--r-- 1 root root 795 Jan  5 17:21 502.html
-rw-r--r-- 1 root root 795 Jan  5 17:22 503.html
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/haproxy/html/500.html 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>

        <style type="text/css">
            .box1{
                color: red;
                font-family: "curlz mt","华文彩云","arial", "微软雅黑";
            }

            #box2{
                color: yellowgreen;
            }

            a:link{
                color: yellowgreen;
            }

            /**
             *     访问过的链接
             */
            a:visited{
                color: red;
            }

            /**
             *     鼠标移入
             */
            a:hover{
                color: orange;
            }

            /**
             *     正在点击
             */
            a:active{
                color: blue;
            }
        </style>
    </head>
    <body>
        <h1 class="box1">您访问的网站正在维护中,请稍后再来访问,报错码:500</h1>
        <h3 id = "box2">如有疑问请联系运维工程师:&nbsp;&nbsp;&nbsp;<a href="https://www.cnblogs.com/yinzhengjie/">尹正杰</a></h3>
    </body>
</html>
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/haproxy/html/500.html

[root@node102.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/haproxy/html/502.html 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>

        <style type="text/css">
            .box1{
                color: red;
                font-family: "curlz mt","华文彩云","arial", "微软雅黑";
            }

            #box2{
                color: yellowgreen;
            }

            a:link{
                color: yellowgreen;
            }

            /**
             *     访问过的链接
             */
            a:visited{
                color: red;
            }

            /**
             *     鼠标移入
             */
            a:hover{
                color: orange;
            }

            /**
             *     正在点击
             */
            a:active{
                color: blue;
            }
        </style>
    </head>
    <body>
        <h1 class="box1">您访问的网站正在维护中,请稍后再来访问,报错码:502</h1>
        <h3 id = "box2">如有疑问请联系运维工程师:&nbsp;&nbsp;&nbsp;<a href="https://www.cnblogs.com/yinzhengjie/">尹正杰</a></h3>
    </body>
</html>
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/haproxy/html/502.html

[root@node102.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/haproxy/html/503.html 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>

        <style type="text/css">
            .box1{
                color: red;
                font-family: "curlz mt","华文彩云","arial", "微软雅黑";
            }

            #box2{
                color: yellowgreen;
            }

            a:link{
                color: yellowgreen;
            }

            /**
             *     访问过的链接
             */
            a:visited{
                color: red;
            }

            /**
             *     鼠标移入
             */
            a:hover{
                color: orange;
            }

            /**
             *     正在点击
             */
            a:active{
                color: blue;
            }
        </style>
    </head>
    <body>
        <h1 class="box1">您访问的网站正在维护中,请稍后再来访问,报错码:503</h1>
        <h3 id = "box2">如有疑问请联系运维工程师:&nbsp;&nbsp;&nbsp;<a href="https://www.cnblogs.com/yinzhengjie/">尹正杰</a></h3>
    </body>
</html>
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/haproxy/html/503.html

3>.使用浏览器访问"http://node102.yinzhengjie.org.cn/",如下图所示。

4>.手动将后端的"node108.yinzhengjie.org.cn"节点的Apache httpd服务下线

[root@node108.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN     0      128                              *:80                                           *:*                  
LISTEN     0      128                              *:22                                           *:*                  
LISTEN     0      128                             :::22                                          :::*                  
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# systemctl stop httpd
[root@node108.yinzhengjie.org.cn ~]# 
[root@node108.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN     0      128                              *:22                                           *:*                  
LISTEN     0      128                             :::22                                          :::*                  
[root@node108.yinzhengjie.org.cn ~]#

5>.再次使用浏览器访问"http://node102.yinzhengjie.org.cn/",如下图所示。

目录
相关文章
|
应用服务中间件 nginx 数据安全/隐私保护
HAProxy的高级配置选项-配置haproxy的状态页
这篇文章详细介绍了如何配置HAProxy的状态页,包括隐藏版本信息、设置自动刷新时间、自定义访问URI、配置认证信息以及启用管理功能等,并通过实战案例展示了配置过程和效果。
1026 5
HAProxy的高级配置选项-配置haproxy的状态页
|
Java fastjson API
Springboot 整合 xxljob 动态API调度任务(进阶篇)
Springboot 整合 xxljob 动态API调度任务(进阶篇)
9206 0
Springboot 整合 xxljob 动态API调度任务(进阶篇)
|
Web App开发 编解码 安全
【WebRTC 入门教程】全面解析WebRTC:从底层原理到Qt和FFmpeg的集成应用
【WebRTC 入门教程】全面解析WebRTC:从底层原理到Qt和FFmpeg的集成应用
7991 2
|
监控 应用服务中间件 BI
nginx日志统计分析自动报表工具goaccess(推荐)
## 功能描述 - nginx日志统计分析自动报表工具goaccess(推荐) - 网站总访问量统计,按天统计访问量,按页面(不同URL)统计访问量(不包括JS、css),按静态页面统计访问量(包括JS、css),不存在的页面统计访问量 - 按不同的IP统计访问量,按不同的操作系统统计访问量,...
8947 0
|
6月前
|
数据采集 人工智能 文字识别
2026企业AI如何真正落地?深度拆解60+全球案例
2026年企业AI应用已告别“大模型万能论”。本文基于斯坦福《企业AI实战手册》及16家头部企业案例,提炼7条落地共性:AI须嵌入核心业务流程;高风险行业坚持“人先于AI”;数据质量决定成效上限;行业分化加剧,通用方案失效;价值重心从个人提效转向组织决策;AI需持续运营而非一次性项目;推荐以8周闭环验证真实场景,本质是经营能力的竞争。
|
XML 缓存 监控
Python中使用SOAP消息:全面指南
本文介绍了如何在Python环境中创建、发送和接收SOAP请求,包括安装`zeep`库、创建SOAP客户端、发送请求、处理复杂类型、错误处理、自定义SOAP头、性能优化、日志记录等内容。SOAP因其标准化、安全性、可靠性和互操作性,在企业级应用中仍被广泛采用。通过本文,读者可以掌握使用Python与SOAP Web服务交互的基本方法。
1056 3
|
数据安全/隐私保护 Docker 容器
配置Harbor支持https功能实战篇
关于如何配置Harbor支持HTTPS功能的详细教程。
1522 13
配置Harbor支持https功能实战篇
|
监控 Apache
HAProxy的高级配置选项-Web服务器状态监测
这篇文章介绍了HAProxy的高级配置选项,特别是如何进行Web服务器状态监测,包括基于四层传输端口监测、基于指定URI监测和基于指定URI的request请求头部内容监测三种方式,并通过实战案例展示了配置过程和效果。
702 8
HAProxy的高级配置选项-Web服务器状态监测
|
Kubernetes 网络协议 应用服务中间件
Kubernetes Ingress:灵活的集群外部网络访问的利器
《Kubernetes Ingress:集群外部访问的利器-打造灵活的集群网络》介绍了如何通过Ingress实现Kubernetes集群的外部访问。前提条件是已拥有Kubernetes集群并安装了kubectl工具。文章详细讲解了Ingress的基本组成(Ingress Controller和资源对象),选择合适的版本,以及具体的安装步骤,如下载配置文件、部署Nginx Ingress Controller等。此外,还提供了常见问题的解决方案,例如镜像下载失败的应对措施。最后,通过部署示例应用展示了Ingress的实际使用方法。
1378 2
|
Java 开发者
Java 中的锁是什么意思,有哪些分类?
在Java多线程编程中,锁用于控制多个线程对共享资源的访问,确保数据一致性和正确性。本文探讨锁的概念、作用及分类,包括乐观锁与悲观锁、自旋锁与适应性自旋锁、公平锁与非公平锁、可重入锁和读写锁,同时提供使用锁时的注意事项,帮助开发者提高程序性能和稳定性。
1026 3