haproxy高级功能及配置

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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HAProxy配置负载均衡可以分为以下几个步骤: 1. 安装 HAProxy:在 Linux 系统上,可以使用包管理器来安装 HAProxy。例如在 Ubuntu 系统上,可以使用以下命令来安装: ``` sudo apt-get update sudo apt-get install haproxy ``` 2. 编辑 HAProxy 配置文件:HAProxy配置文件通常位于 /etc/haproxy/haproxy.cfg,可以使用任何文本编辑器来编辑。需要注意的是,在修改配置文件之前应该备份原来的配置文件。 3. 配置后端服务器:在 HAProxy 中,后端服务器也称为“服务器池”或“服务器组”。可以通过以下配置添加后端服务器: ``` backend mybackend balance roundrobin server server1 192.168.1.10:80 check server server2 192.168.1.11:80 check ``` 这里使用了 round-robin 算法来进行负载均衡,每个请求将轮流分配到不同的服务器上。`check` 表示 HAProxy 会定期检查服务器是否可用。 4. 配置前端服务器:在 HAProxy 中,前端服务器也称为“监听器”或“代理”。可以通过以下配置添加前端服务器: ``` frontend myfrontend bind *:80 default_backend mybackend ``` 这里将 HAProxy 监听在 80 端口,所有的请求都将转发到后端服务器池 `mybackend` 中。 5. 重启 HAProxy:修改配置文件之后,需要重启 HAProxy 服务才能使新的配置生效。可以使用以下命令来重启 HAProxy: ``` sudo systemctl restart haproxy ``` 以上是基本的 HAProxy 负载均衡配置步骤。当然,HAProxy 还支持多种高级功能,如 SSL/TLS 加密、会话保持、健康检查等。在实际使用中,可以根据需要进行配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值