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

二.自定义错误跳转案例实战

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
    #自定义错误跳转,可以将错误页面放在一台web服务器上
    errorloc 503 http://node107.yinzhengjie.org.cn/monitor/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服务使得配置生效,可以观察到haproxy的状态页,如下图所示。
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

2>.在web服务器创建错误页面数据,并验证是否可以正常范围

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/",如下图所示。

目录
相关文章
|
定位技术 数据中心
全球历史台风轨迹数据的艰难寻找经历
全球历史台风轨迹数据的艰难寻找经历
2325 0
|
存储 传感器 算法
如何选择合适的 CRC 多项式
CRC(循环冗余校验)多项式的选取对数据传输的错误检测至关重要。选择时需考虑多项式的长度、检测性能及实现复杂度,常用多项式有CRC-8、CRC-16、CRC-32等,适用于不同场景以确保高效准确的错误检测。
1759 4
|
编解码 运维 负载均衡
envoy代理转发与L5 Cluster 负载均衡
envoy代理转发与L5 Cluster 负载均衡
537 0
envoy代理转发与L5 Cluster 负载均衡
|
Windows
Windows操作系统中环境变量的检索顺序
Windows操作系统中环境变量的检索顺序
496 3
|
UED 索引
震惊!大文件上传也能如此丝滑?揭秘断点续传黑科技,让你的文件传输快如闪电,再也不用担心中断烦恼!
【8月更文挑战第4天】互联网的发展使大文件上传成为应用常见需求,但易受网络等因素影响中断。断点续传技术将文件分块,每块独立上传,若中断可续传未完成部分,提升效率和体验。实现包括文件分块、初始化上传、逐块上传与校验、服务器合并文件,及处理续传逻辑。通过客户端与服务器协作,优化上传流程,适应网络波动,确保数据完整性。随着技术进步,断点续传方案将更高效可靠。
1005 13
|
存储 Python
数据包络分析(Data Envelopment Analysis, DEA)详解与Python代码示例
数据包络分析(Data Envelopment Analysis, DEA)详解与Python代码示例
|
存储 负载均衡 应用服务中间件
Web架构&OSS存储&负载均衡&CDN加速&反向代理&WAF防护
Web架构&OSS存储&负载均衡&CDN加速&反向代理&WAF防护
494 1
|
安全 Linux
Linux【问题记录 03】阿里云+腾讯云服务器的 kdevtmpfsi(H2Miner挖矿蠕虫变种)病毒处理(5个详细步骤)
Linux【问题记录 03】阿里云+腾讯云服务器的 kdevtmpfsi(H2Miner挖矿蠕虫变种)病毒处理(5个详细步骤)
467 1
|
存储 缓存 NoSQL
使用redis进行缓存加速
使用redis进行缓存加速
587 0
|
存储 数据可视化 atlas
对于组学数据的分析和展示来说,maftools算是一个宝藏“R包”,可用于MAF格式的组学数据的汇总,分析和可视化展示。
对于组学数据的分析和展示来说,maftools算是一个宝藏“R包”,可用于MAF格式的组学数据的汇总,分析和可视化展示。
947 0

热门文章

最新文章

下一篇
开通oss服务