haproxy 配置

本文档详细介绍了haproxy的安装过程,包括通过rpmbuild生成rpm包,然后安装haproxy。接着,展示了如何配置haproxy进行负载均衡,包括修改配置文件、设置后端服务器、开启httpd服务、启动haproxy服务,并通过测试验证了负载均衡的效果。此外,还讨论了haproxy的日志设置、用户认证、黑名单和网络限制的配置方法,以及实现动静分离和读写分离的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置环境:
server1:调度器1(172.25.254.1)
server2:real server1(172.25.254.2)
server3:real server2(172.25.254.3)
一、haproxy负载均衡
1、haproxy安装

haproxy可以使用源码安装,rpm直接安装和rpmbuild安装三种方式,本次采用rpmbuild安装
1、在官网下载haproxy安装包,并解压

[root@server1 ~]# ls
haproxy-1.6.11 haproxy-1.6.11.tar.gz

1
2

2、安装rpm-build
[root@server1 ~]# yum install rpm-build -y
3、执行命令生成rpmbuild目录
[root@server1 ~]# rpmbuild -bb haproxy-1.6.11
4、进入到解压的haproxy目录下

[root@server1 haproxy-1.6.11]# cd examples/
[root@server1 examples]# ls
acl-content-sw.cfg debug2html init.haproxy
auth.cfg debugfind option-http_proxy.cfg
check errorfiles seamless_reload.txt
check.conf haproxy.init ssl.cfg
content-sw-sample.cfg haproxy.spec stats_haproxy.sh
debug2ansi haproxy.vim transparent_proxy.cfg
[root@server1 examples]# rpmbuild -bb haproxy.spec ##rpmbuild来生成rpm包
error: Failed build dependencies:
pcre-devel is needed by haproxy-1.6.11-1.x86_64 ##报错缺少pcre-devel包
[root@server1 examples]# yum install pcre-devel ##安装
[root@server1 examples]# rpmbuild -bb haproxy.spec 继续执行

1
2
3
4
5
6
7
8
9
10
11
12
13

遇到此报错意思是缺少gcc,安装即可
这里写图片描述
[root@server1 examples]# rpmbuild -bb haproxy.spec继续

[root@server1 examples]# cd /root/rpmbuild/RPMS/x86_64/ ##成功后会生成x86_64目录下 会有haproxy的rpm包,rpm-ivh安装即可
[root@server1 x86_64]# ls
haproxy-1.6.11-1.x86_64.rpm
[root@server1 x86_64]# rpm -ivh haproxy-1.6.11-1.x86_64.rpm
Preparing… ########################################### [100%]
1:haproxy ########################################### [100%]

1
2
3
4
5
6

2、负载均衡配置

1、修改配置文件

/root/haproxy-1.6.11/examples
[root@server1 examples]# ls
acl-content-sw.cfg debug2html init.haproxy
auth.cfg debugfind option-http_proxy.cfg
check errorfiles seamless_reload.txt
check.conf haproxy.init ssl.cfg
content-sw-sample.cfg haproxy.spec stats_haproxy.sh
debug2ansi haproxy.vim transparent_proxy.cfg
[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
[root@server1 examples]# cd /etc/ha
-bash: cd: /etc/ha: No such file or directory
[root@server1 examples]# cd /etc/haproxy/
[root@server1 haproxy]# pwd
/etc/haproxy
[root@server1 haproxy]# vim haproxy.cfg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

10 global
11 maxconn 65535 ##最大连接数
12 stats socket /var/run/haproxy.stat mode 600 level admin
13 log 127.0.0.1 local0 ##日志
14 uid 200 ##指定uid
15 gid 200 ##指定gid
16 chroot /var/empty
17 daemon
18
19 defaults ##默认变量
20 mode http ##http服务
21 log global ##日志格式
22 option httplog
23 option dontlognull
24 monitor-uri /monitoruri ##调度器健康检查页面
25 maxconn 8000 ##默认最大连接
26 timeout client 30s ##客户端中断时间
27 stats uri /admin/stats ##后端健康检查页面
28 option redispatch
29 timeout connect 5s ##连接断开
30 timeout server 30s ##服务断开
31 retries 2 ##再次连接
32 # The public ‘www’ address in the DMZ
33 frontend public
34 bind *:80
35 #bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
36 #use_backend static if { hdr_beg(host) -i img }
37 #use_backend static if { path_beg /img /css }
38 default_backend dynamic
39
40
41 # the application servers go here
42 backend dynamic
43 balance roundrobin
44 server web1 172.25.254.2:80 check inter 1000
45 server web2 172.25.254.3:80 check inter 1000
2、修改限制文件
[root@server1 haproxy]# vim /etc/security/limits.conf
这里写图片描述
3、建立配置文件中指定的user和group
[root@server1 haproxy]# groupadd -g 200 haproxy
[root@server1 haproxy]# useradd -u 200 -g 200 -M -s /sbin/nologin haproxy
4、后端服务器开启httpd服务
这里写图片描述
这里写图片描述
5、调度器开启haproxy服务
[root@server1 haproxy]# /etc/init.d/haproxy start
Starting haproxy: [ OK ]
6、物理机测试:

[root@foundation77 ~]# for i in {1..20};do curl 172.25.254.1;done

server2

server3

server2

server3

server2

server3

server2

server3

server2

server3

server2

server3

server2

server3

server2

server3

server2

server3

server2

server3

负载均衡效果完成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

调度器检测页面
这里写图片描述
后端检测页面
这里写图片描述
3、haproxy日志设置

[root@server1 ~]# vim /etc/rsyslog.conf

13 ModLoadimudp14 M o d L o a d i m u d p 14 UDPServerRun 514
42 *.info;mail.none;authpriv.none;cron.none;local0.none /var/log /messages
62 local0.* /var/log/haproxy.log

1
2
3
4

这里写图片描述
4、开启用户认证

[root@server1 ~]# vim /etc/haproxy/haproxy.cfg

32 stats auth admin:linux

1

[root@server1 ~]# /etc/init.d/haproxy reload

这里写图片描述
这里写图片描述
5、haproxy黑名单,网络限制

1、黑名单

34 frontend public
35 bind *:80
36 acl denylist src 172.25.254.77 ##设置denylist来自77
37 #acl spaclelist src 172.25.254.177
38 http-request deny if denylist ##当denylist的访问时加入黑名单
39 #errorloc 403 http://baidu.com ##如碰到403报错,将访问转到百度

1
2
3
4
5
6

这里写图片描述
2、网络限制

34 frontend public
35 bind *:80
36 acl denylist src 172.25.254.77
37 acl spaclelist src 172.25.254.177
38 #http-request deny if denylist
39 #errorloc 403 http://www.baidu.com
40 redirect location http://taobao.com if denylist ##来自denylist的访问全部转到淘宝
41 redirect location http://jd.com if spaclelist 来自spaclelist的访问全部转到京东
42 #bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
43 #use_backend static if { hdr_beg(host) -i img }
44 #use_backend static if { path_beg /img /css }
45 default_backend dynamic

1
2
3
4
5
6
7
8
9
10
11
12

这里写图片描述
6、haproxy实现动静分离

server2安装php服务,并在http默认发布录下建php文件
这里写图片描述
server1修改配置文件

34 frontend public
35 bind *:80
36 #acl denylist src 172.25.254.77
37 #acl spaclelist src 172.25.254.177
38 #http-request deny if denylist
39 #errorloc 403 http://www.baidu.com
40 #redirect location http://taobao.com if denylist
41 #redirect location http://jd.com if spaclelist
42 #bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
43 use_backend static if { hdr_beg(host) -i img }
44 use_backend static if { path_beg /img /css }
45 use_backend static if { path_end -i .php } ##当访问以.php结尾的网页时。访问static域
46 default_backend dynamic
47
48
49 # the application servers go here
50 backend dynamic
51 balance roundrobin
52 #server web1 172.25.254.2:80 check inter 1000
53 server web2 172.25.254.3:80 check inter 1000
54 backend static
55 balance roundrobin
56 server web1 172.25.254.2:80 check inter 1000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

重启服务访问
这里写图片描述
7、hproxy实现读写分离

将读的请求交给server2处理
将写的请求交给server3处理

34 frontend public
35 bind *:80
36 acl write method POST
37 acl write method PUT
38 acl read method GET
39 acl read method HEAD
40 use_backend dynamic if write
41 use_backend static if read
42 #acl denylist src 172.25.254.77
43 #acl spaclelist src 172.25.254.177
44 #http-request deny if denylist
45 #errorloc 403 http://www.baidu.com
46 #redirect location http://taobao.com if denylist
47 #redirect location http://jd.com if spaclelist
48 #bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
49 #use_backend static if { hdr_beg(host) -i img }
50 #use_backend static if { path_beg /img /css }
51 #use_backend static if { path_end -i .php }
52 default_backend static
53
54
55 # the application servers go here
56 backend dynamic
57 balance roundrobin
58 #server web1 172.25.254.2:80 check inter 1000
59 server web2 172.25.254.3:80 check inter 1000
60 backend static
61 balance roundrobin
62 server web1 172.25.254.2:80 check inter 100

内容概要:该论文探讨了一种基于粒子群优化(PSO)的STAR-RIS辅助NOMA无线通信网络优化方法。STAR-RIS作为一种新型可重构智能表面,能同时反射和传输信号,与传统仅能反射的RIS不同。结合NOMA技术,STAR-RIS可以提升覆盖范围、用户容量和频谱效率。针对STAR-RIS元素众多导致获取完整信道状态信息(CSI)开销大的问题,作者提出一种在不依赖完整CSI的情况下,联合优化功率分配、基站波束成形以及STAR-RIS的传输和反射波束成形向量的方法,以最大化总可实现速率并确保每个用户的最低速率要求。仿真结果显示,该方案优于STAR-RIS辅助的OMA系统。 适合人群:具备一定无线通信理论基础、对智能反射面技术和非正交多址接入技术感兴趣的科研人员和工程师。 使用场景及目标:①适用于希望深入了解STAR-RIS与NOMA结合的研究者;②为解决无线通信中频谱资源紧张、提高系统性能提供新的思路和技术手段;③帮助理解PSO算法在无线通信优化问题中的应用。 其他说明:文中提供了详细的Python代码实现,涵盖系统参数设置、信道建模、速率计算、目标函数定义、约束条件设定、主优化函数设计及结果可视化等环节,便于读者理解和复现实验结果。此外,文章还对比了PSO与其他优化算法(如DDPG)的区别,强调了PSO在不需要显式CSI估计方面的优势。
### 关于 HAProxy配置方法及示例 HAProxy 是一款由法国开发者 Willy Tarreau 创建的高性能 TCP 和 HTTP 负载均衡器,其功能强大且灵活,适用于多种场景下的流量管理[^3]。 #### 查看默认配置文件路径 在安装完成之后,可以通过命令 `rpm -qc haproxy` 来获取 HAProxy配置文件列表。此操作可以帮助用户快速定位到系统的默认配置文件位置以及可能存在的其他重要文件[^1]。 #### SSL 配置实例 为了支持 HTTPS 流量处理,在 HAProxy 中可以启用 SSL 协议并绑定相应的证书文件。例如,通过如下配置实现对 443 端口的支持: ```plaintext bind :443 ssl crt /etc/haproxy/ssl/haproxy.pem ``` 上述语句表示监听 443 端口,并使用 `/etc/haproxy/ssl/haproxy.pem` 文件作为 SSL/TLS 证书来保障通信安全[^2]。 #### 基本配置模板 以下是 HAProxy 的一个基础配置示例,展示了如何设置前端、后端服务及其关联关系: ```plaintext global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon defaults mode http log global option httplog option dontlognull timeout connect 5000ms timeout client 50000ms timeout server 50000ms frontend main bind *:80 default_backend servers backend servers balance roundrobin server web1 192.168.1.10:80 check server web2 192.168.1.11:80 check ``` 在此配置中: - **Global Section**: 定义全局参数,如日志记录方式、进程运行模式等。 - **Defaults Section**: 设置默认行为选项,比如超时时间和服务模式 (http 或 tcp)。 - **Frontend Section**: 描述外部请求进入的方式,这里绑定了所有 IP 地址上的第 80 号端口。 - **Backend Section**: 列出了实际提供服务的一组服务器地址,并采用轮询算法分配连接给它们。 以上内容提供了从初始状态到具体应用层面的操作指南,帮助理解 HAProxy 如何被正确部署与调整以满足特定需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值