1、cookie会话保持
[root@localhost ~]# cd /etc/haproxy
[root@localhost haproxy]# vim conf.d/app1.cfg
listen app1
# this is the address and port we'll listen to, the ones to aim the
# load generators at
bind :80
log global
# create a certificate and uncomment this for SSL
# bind :8443 ssl crt my-cert.pem alpn h2,http/1.1
mode http
cookie webname insert nocache indirect
# Put the server's IP address and port below
server web1 192.168.157.200:80 check cookie web1
server web2 192.168.157.201:80 check cookie web2
重启haproxy
2、HAProxy状态页
①stats hide-version——————隐藏版本
②stats admin if TRUE————设置可选
③健康性检查
curl -I http://haadmin:123456@192.168.157.10:9999/haproxy-status
如果关掉haproxy将会检测不到
④ip透传
第二台电脑
3、web服务器日志格式配置
虚拟机web1
vim /etc/httpd/conf/httpd.conf
#apache 配置
LogFormat "%{X-Forwarded-For}i %a %l %u %t "%r" %>s %b "%{Referer}i" "% {User-Agent}i"" combined
接下来重启http
虚拟机VIP
[root@localhost haproxy]# vim conf.d/app1.cfg
重启haproxy
web1电脑tail /var/log/httpd/access_log
136为VIP的动态ip 131为第四台电脑的ip
4、web服务检测状态
option httpchk GET /index.html HTTP/1.1
重启haproxy
如果两台web都宕机也会出现提示
接下来修改haproxy.cfg删掉HTTP/1.1重启haproxy
此时关掉web1的http就会宕掉
backup
VIP下载httpd
[root@localhost haproxy]# vim /etc/httpd/conf/httpd.conf
设置Listen为81
vim /var/www/html/index.html
服务器正在维护哦!
[root@localhost haproxy]# systemctl restart haproxy [root@localhost haproxy]# systemctl start httpd
此时关掉两台web服务器用第四台电脑curl
5、自定义haproxy错误界面
①vim /etc/haproxy/conf.d/app1.cfg
注释掉如下一行
②vim /etc/haproxy/haproxy.cfg
③503页面编辑
[root@localhost haproxy]# cd /usr/local/haproxy/
[root@localhost haproxy]# mkdir html
[root@localhost haproxy]# cd html
[root@localhost html]# vim 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>
此时重启haproxy
关掉两台web的http
网页访问VIP