一、基于cookie的会话保持
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind*:80
mode http
balance roundrobin
hash-type consistent
cookie WEBCOOKIE insert nocache indi rect #cookie值为WEBCOOKIE(cookie值可自定义)
server webl 172.25.254.10:80 cookie testl check inter 2 fall 3 rise 5 weight 1
server web2 172.25.254.20:80 cookie test2 check inter 2 fall 3 rise 5 weight 1
server web_sorry 172.25.254.100:8080 backup
[root@haproxy ~]# systemctl restart haproxy.service
二、IP透传
没有
[root@webserver1 ~]# systemctl disable nginx.service
Removed "/etc/systemd/system/multi-user.target.wants/nginx.service".
[root@webserver1 ~]# systemctl stop nginx.service
[root@webserver1 ~]# dnf install httpd -y
[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /var/www/html/index.html
[root@webserver1 ~]# systemctl start httpd
七层
测试:
四层
测试:
三、ACL列表
注:将之前的都删掉
3.1 hdr
以什么域匹配:
(以 .
为分隔符里面的字符串)
步骤:
以什么(.org)结尾:
以bbs开头:
3.2 base
base_sub:
.
之间包含的内容:
以lee结尾:
步骤:
[root@webserver1 ~]# mkdir -p /var/www/html/haha
[root@webserver1 ~]# echo 172.25.254.10 haha > /var/www/html/haha/index.html
3.3 path
路径子字符串匹配(path_sub)
3.4 多个ACL的组合调用方式
与
|| 或
!非
步骤:
[root@webserver2 ~]# mkdir -p /usr/share/nginx/html/haha
[root@webserver2 ~]# echo 172.25.254.20 haha > /usr/share/nginx/html/haha/index.html
3.5 ACL示例-域名匹配
说明:
结尾加/是代表这个目录
不加/代表这个文件
acl对字串进行过滤
基于域名的访问
基于网段的
符合ip的10,不符合ip的20
基于浏览器的
动静分离的
不同的路径访问不同的页面
php动态页面
10:dnf install php -y
步骤:
[root@webserver1 ~]# dnf install php -y
[root@webserver1 ~]# systemctl restart httpd
[root@webserver1 ~]# vim /var/www/html/index.php
[root@webserver1 ~]# cat /var/www/html/index.php
<?php
phpinfo();
?>
以什么路径结尾的
静态页面
步骤:
[root@webserver1 ~]# mkdir -p /var/www/html/php
[root@webserver1 ~]# cp /var/www/html/index.php /var/www/html/php/
[root@webserver2 ~]# mkdir -p /usr/share/nginx/html/static
[root@webserver2 ~]# echo static - 172.25.254.20 > /usr/share/nginx/html/static/index.html
[root@webserver2 ~]# curl 172.25.254.20/static/
static - 172.25.254.20
路径字符串匹配
四、自定义HAProxy错误界面
原始界面
指定页面内容
步骤:
[root@haproxy ~]# mkdir -p /etc/haproxy/errorpage
[root@haproxy ~]# vim /etc/haproxy/errorpage/503.http #在里面编写你想呈现的效果
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy.service
例如:
在配置文件里的defaults最后编写:
效果:
503跳转网站,重定向
在配置文件里的defaults最后编写:
五、HAProxy 四层负载
使用数据库来做
10和20都安装mariadb
步骤:
[root@webserver1 ~]# dnf install mariadb-server -y
[root@webserver1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[root@webserver1 ~]# systemctl start mariadb.service
[root@webserver2 ~]# dnf install mariadb-server -y
[root@webserver2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[root@webserver2 ~]# systemctl start mariadb.service
webserver1 里面
webserver2 里面
haproxy 里面:
[root@haproxy ~]# dnf install mariadb-server -y
[root@haproxy ~]# mysql -utest -p -h 172.25.254.10
测试:
六、HAProxy https实现
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# netstat -antlupe | grep haproxy
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 0 5834670 4458/haproxy
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 0 5834671 4458/haproxy
udp 0 0 0.0.0.0:39695 0.0.0.0:* 991 5834708 4458/haproxy
udp 0 0 0.0.0.0:41817 0.0.0.0:* 991 5835929 4458/haproxy
[root@haproxy ~]#
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy.service