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

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

                                       作者:尹正杰

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

 

 

 

 

一.安装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的配置文件

[[email protected] ~]# 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 
[[email protected] ~]# 
[[email protected] ~]# systemctl restart haproxy              #别忘记了重启haproxy服务使得配置生效哟~可以查看状态页观察服务是否正常,如下图所示。
[[email protected] ~]# 
[[email protected] ~]# 

HAProxy的高级配置选项-自定义错误页面_第1张图片

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

[[email protected] ~]# 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
[[email protected] ~]# 
[[email protected] ~]# mkdir -pv /yinzhengjie/softwares/haproxy/html
mkdir: created directory ‘/yinzhengjie/softwares/haproxy/html’
[[email protected] ~]# 
[[email protected] ~]# 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
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# cat /yinzhengjie/softwares/haproxy/html/500.html 


    <head>
        "utf-8" />
        
        
        
    head>
    
        

"box1">您访问的网站正在维护中,请稍后再来访问,报错码:500

id = "box2">如有疑问请联系运维工程师:   "https://www.cnblogs.com/yinzhengjie/">尹正杰

[[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# cat /yinzhengjie/softwares/haproxy/html/500.html
[[email protected] ~]# cat /yinzhengjie/softwares/haproxy/html/502.html 


    <head>
        "utf-8" />
        
        
        
    head>
    
        

"box1">您访问的网站正在维护中,请稍后再来访问,报错码:502

id = "box2">如有疑问请联系运维工程师:   "https://www.cnblogs.com/yinzhengjie/">尹正杰

[[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# cat /yinzhengjie/softwares/haproxy/html/502.html
[[email protected] ~]# cat /yinzhengjie/softwares/haproxy/html/503.html 


    <head>
        "utf-8" />
        
        
        
    head>
    
        

"box1">您访问的网站正在维护中,请稍后再来访问,报错码:503

id = "box2">如有疑问请联系运维工程师:   "https://www.cnblogs.com/yinzhengjie/">尹正杰

[[email protected] ~]# [[email protected] ~]#
[[email protected] ~]# cat /yinzhengjie/softwares/haproxy/html/503.html

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

HAProxy的高级配置选项-自定义错误页面_第2张图片

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

[[email protected] ~]# 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                                          :::*                  
[[email protected] ~]# 
[[email protected] ~]# systemctl stop httpd
[[email protected] ~]# 
[[email protected] ~]# ss -ntl
State      Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN     0      128                              *:22                                           *:*                  
LISTEN     0      128                             :::22                                          :::*                  
[[email protected] ~]# 

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

HAProxy的高级配置选项-自定义错误页面_第3张图片

 

你可能感兴趣的:(HAProxy的高级配置选项-自定义错误页面)