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

目录
相关文章
|
数据采集 安全 数据中心
爬虫应该选择住宅ip代理还是数据中心代理?
爬虫应该选择住宅ip代理还是数据中心代理?
爬虫应该选择住宅ip代理还是数据中心代理?
|
监控 Apache
HAProxy的高级配置选项-Web服务器状态监测
这篇文章介绍了HAProxy的高级配置选项,特别是如何进行Web服务器状态监测,包括基于四层传输端口监测、基于指定URI监测和基于指定URI的request请求头部内容监测三种方式,并通过实战案例展示了配置过程和效果。
280 8
HAProxy的高级配置选项-Web服务器状态监测
|
应用服务中间件 nginx
HAProxy的高级配置选项-修改报文首部
这篇文章介绍了HAProxy的高级配置选项,特别是如何通过配置来修改HTTP报文首部,包括添加和删除特定的响应头信息,以及如何通过条件判断来控制这些修改。
201 8
|
12月前
|
JSON 运维 Kubernetes
|
12月前
|
安全 Java API
🌟探索Java宇宙:深入理解Java技术体系与JVM的奥秘
本文深入探讨了Java技术体系的全貌,从Java语言的概述到其优点,再到Java技术体系的构成,以及JVM的角色。旨在帮助Java开发者全面了解Java生态,提升对Java技术的认知,从而在编程实践中更好地发挥Java的优势。关键词:Java, JVM, 技术体系, 编程语言, 跨平台, 内存管理。
133 2
|
网络协议 Java Maven
多模块项目使用ElasticSearch报错
多模块项目使用ElasticSearch报错
308 6
|
C++ Python
python类方法中使用:修饰符@staticmethod和@classmethod的作用与区别,还有装饰器@property的使用
python类方法中使用:修饰符@staticmethod和@classmethod的作用与区别,还有装饰器@property的使用
459 1
|
Windows
Windows操作系统中环境变量的检索顺序
Windows操作系统中环境变量的检索顺序
322 3
|
存储 SQL 数据库
influxdb 连续查询使用总结
influxdb 连续查询使用总结
571 0
|
缓存 负载均衡 应用服务中间件