# HAProxy

**HAProxy**（“high availability proxy”的缩写，即高可用代理）是一款用C语言编写的免费开源软件负载均衡器与反向代理，负责将TCP连接和HTTP请求分发到后端服务器池中<sup>[1](https://cdn.haproxy.com/blog/the-history-of-haproxy)</sup><sup> • </sup><sup>[2](https://rad.ort.edu.uy/bitstreams/951db8d7-85f5-4e80-883b-63cf5064f7e9/download)</sup>。它由Linux内核贡献者威利·塔罗（Willy Tarreau）创建，他至今仍是该项目的首席维护者；HAProxy脱胎于他1999年编写的一款基准测试工具<sup>[1](https://cdn.haproxy.com/blog/the-history-of-haproxy)</sup><sup> • </sup><sup>[2](https://rad.ort.edu.uy/bitstreams/951db8d7-85f5-4e80-883b-63cf5064f7e9/download)</sup>。HAProxy还充当SSL终结器、TCP与HTTP规范化器，以及能够实施限流、保护服务器免于过载的流量调节器<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 首次发布 | HAProxy 1.0，2001年，此前已作为基准测试与头部重写代理在内部使用两年<sup>[1](https://cdn.haproxy.com/blog/the-history-of-haproxy)</sup> |
| 代理模式 | 两大模式：`tcp`（第4层，按连接做决策）与`http`（第7层，按请求做决策）<sup>[4](https://cdn.haproxy.com/documentation/haproxy-configuration-manual/new/latest/)</sup><sup> • </sup><sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup> |
| 架构 | 事件驱动的非阻塞引擎，配备基于优先级的多线程调度器；在TCP或HTTP close模式下，HAProxy内的处理时间约占15%，内核约占85%<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup> |
| 规模 | 单个进程可运行多个代理实例；据报道，单进程配置30万个不同代理也能正常运行<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup> |
| 当前版本 | HAProxy 3.4.0，2026年6月3日发布，为LTS分支，支持至2031年第二季度<sup>[5](https://www.haproxy.org/news.html)</sup><sup> • </sup><sup>[6](https://www2.haproxy.org/)</sup> |
| 协议支持 | HTTP/1.x、HTTP/2，以及基于QUIC的HTTP/3；面向客户端的HTTP/3自HAProxy 2.7起可用<sup>[7](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/http/)</sup> |
| 商业版本 | HAProxy Technologies在社区版之外还销售HAProxy企业版与HAProxy ALOHA<sup>[8](https://www.haproxy.com/success-stories)</sup> |

## 历史

HAProxy起初是一款内部工具。1999年，威利·塔罗需要测试一个应用在28 Kbps调制解调器（而非单条100 Mbps链路）上服务大量客户端时的表现，于是他从自己早先编写的代理Zprox中剥离压缩代码，并加入带宽限制<sup>[1](https://cdn.haproxy.com/blog/the-history-of-haproxy)</sup>。一年后，他复用该工具修复一个互操作性问题：某款负载生成工具发出的HTTP头部语法不符合服务器预期；他为此加入了基于正则表达式的头部重写，HAProxy配置语言的雏形也由此诞生——那次工作中使用的`listen`和`server`关键字，十九年后在HAProxy 2.0中仍受支持<sup>[1](https://cdn.haproxy.com/blog/the-history-of-haproxy)</sup>。

转折点出现在2001年年中：一个重型应用偶尔出现超长响应时间，导致连接在硬件负载均衡器前堆积，而后者在负载下反复失效。塔罗的代理当时已被戏称为“威利的代理”，被部署用于卸载第7层工作：它检查头部，并基于cookie将会话保持在同一台服务器上，将硬件均衡器承担的第7层工作估计减少到十分之一乃至百分之一<sup>[1](https://cdn.haproxy.com/blog/the-history-of-haproxy)</sup>。HAProxy这一名称（意为“高可用代理”）正是在那时确定，HAProxy 1.0于2001年发布<sup>[1](https://cdn.haproxy.com/blog/the-history-of-haproxy)</sup>。

## 运行模式

**TCP模式。** 在TCP模式下，HAProxy在客户端与后端服务器池之间转发原始TCP流，而不对其进行检查；该流可承载任何上层协议，如HTTP、FTP、SMTP、Redis序列化协议或MySQL协议<sup>[9](https://cdn.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/tcp/)</sup>。可用的检查手段较少，但处理开销更低<sup>[9](https://cdn.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/tcp/)</sup>。对于连接保持时间不定的服务器（如数据库），文档建议采用最少连接算法，将每个新客户端发送到活动连接数最少的服务器<sup>[9](https://cdn.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/tcp/)</sup>。

**HTTP模式。** 在HTTP模式下（在`frontend`和`backend`段中设置`mode http`），HAProxy会完整分析并索引请求与响应，因此匹配条件几乎可以建立在报文内容的任何部分之上，代理也可检查并修改报文以执行协议特定操作<sup>[7](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/http/)</sup><sup> • </sup><sup>[4](https://cdn.haproxy.com/documentation/haproxy-configuration-manual/new/latest/)</sup>。在TCP模式下，负载均衡决策针对整个连接；在HTTP模式下，则针对每个请求<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup>。

**协议版本。** HAProxy可以在代理两侧任意组合使用HTTP/1.x或HTTP/2，并在TLS上使用ALPN时自动检测协议<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup>。它还能通过QUIC收发HTTP/3——QUIC是一种基于UDP的传输协议，在该协议版本中取代TCP；软件包必须捆绑兼容QUIC的TLS库<sup>[7](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/http/)</sup>。面向客户端的HTTPS上的HTTP/3自HAProxy 2.7起受支持，通过使用`quic4@`地址前缀的`bind`指令启用<sup>[7](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/http/)</sup>。

## 架构与性能

HAProxy是一款事件驱动的非阻塞引擎，将快速I/O层与基于优先级的多线程调度器相结合。其分层模型在每一层都设有旁路机制，使数据在不需要时不会进入更高的处理层，并将连接固定到同一CPU以保持缓存效率<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup>。大部分处理在内核中完成：典型数据显示，在TCP或HTTP close模式下，HAProxy内的处理时间约占15%，内核约占85%；在HTTP keep-alive模式下约为30%对70%，这意味着操作系统调优对整体性能影响很大<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup>。

该程序仅需`haproxy`可执行文件和一份配置文件。它在启动前解析配置、绑定所有监听套接字，任何环节失败即拒绝启动；过了这一阶段便不会再失败<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup>。多线程按每个CPU核心分配一个线程，这对SSL处理或40 Gbps以上的转发速率尤为重要<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup>。

## 可靠性特性

**健康检查。** HAProxy支持种类繁多的主动与被动健康检查，超时均可配置，因此能在客户察觉之前发现服务中断<sup>[10](https://www.haproxy.com/assets/content-library/datasheets/haproxy-microservices.pdf)</sup>。HAProxy 3.1新增了`init-state`参数，用于控制服务器启动时是直接可接收流量，还是必须先通过初始健康检查；3.2新增了`check-reuse-pool`，通过现有空闲连接发送检查，而非新开连接<sup>[11](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/reliability/health-checks/)</sup>。3.4版引入了可复用的`healthcheck`配置段，减少各服务器间的重复配置<sup>[11](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/reliability/health-checks/)</sup>。

**粘性表。** HAProxy的粘性表存储每客户端状态，如连接数与速率，支持会话持久性与限流；条目在配置的时间后过期<sup>[12](https://cdn.haproxy.com/documentation/haproxy-configuration-manual/new/2-2r1/)</sup>。

**流量调节。** 除负载均衡外，HAProxy还在多个环节实施限流、保护服务器免于过载、根据内容调整流量优先级，并可对数据包做标记，以便将信息传递给更低层及外部网络组件<sup>[3](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)</sup>。

## 版本发布

项目遵循每年发布一次小版本的节奏，并设有长期支持分支。HAProxy 3.2.0于2025年5月28日发布，为LTS分支，支持至2030年第二季度；HAProxy 3.3.0随后于2025年11月26日发布，新增实验性QUIC后端支持、用于[零拷贝](https://www.edgepedia.org/zh/zero-copy)TLS数据传输的KTLS集成、跨重载持久的统计数据，并将默认负载均衡算法切换为轻量级的`random(2)`<sup>[5](https://www.haproxy.org/news.html)</sup><sup> • </sup><sup>[6](https://www2.haproxy.org/)</sup>。

HAProxy 3.4.0于2026年6月3日发布，是当前的LTS分支，生命周期终止于2031年第二季度。其头号特性是动态后端，可完全通过CLI创建、填充服务器、发布和删除，朝着编排化、免重载部署迈进一步<sup>[5](https://www.haproxy.org/news.html)</sup>。该版本还新增了按流设置缓冲区大小的功能以实现更精细的内存控制、针对HTTP/2洪泛的加固（限制帧数与RST_STREAM处理）、成熟的ACME证书自动化（包括DNS-01以及用于通配符证书的“dns-persist-01”挑战）、对OpenSSL 4.0的支持、取代已弃用OpenTracing的[OpenTelemetry](https://www.edgepedia.org/zh/opentelemetry)过滤器，以及代理层的原生加密操作<sup>[5](https://www.haproxy.org/news.html)</sup><sup> • </sup><sup>[13](https://cdn.haproxy.com/blog/announcing-haproxy-3-4)</sup>。

## 部署与用户

HAProxy运行在高流量组织的边缘与基础设施内部。Booking.com用基于HAProxy企业版与等价多路径路由、并结合任播构建的内部“负载均衡即服务”平台取代了F5 BIG-IP硬件，以处理每天数十亿次请求，并通过名为Balancer的自定义API管理该平台<sup>[14](https://cdn.haproxy.com/success-stories/upgrading-to-haproxy-in-a-load-balancer-as-a-service-platform-with-booking-com)</sup>。Recreation.gov是由[博思艾伦汉密尔顿](https://www.edgepedia.org/zh/booz-allen-hamilton)（Booz Allen Hamilton）为12个联邦实体运营的美国公共土地预订平台，它使用HAProxy企业版配合Kubernetes，将其负载均衡器与Web应用防火墙两层合并，简化了架构并应对每月流量高峰<sup>[15](https://www.haproxy.com/success-stories/modernizing-government-infrastructure-with-haproxy-enterprise-and-kubernetes)</sup>。其他有记录的部署还包括Roblox、PayPal、微软Yammer、Criteo和SNCF Connect<sup>[8](https://www.haproxy.com/success-stories)</sup>。

HAProxy还充当[Kubernetes](https://www.edgepedia.org/zh/kubernetes)入口控制器和服务网格部署中的数据平面<sup>[16](https://haproxy-haptic.org/docs/latest/development/design/architecture-overview/)</sup><sup> • </sup><sup>[8](https://www.haproxy.com/success-stories)</sup>。

## 与其他方案比较

HAProxy常被与NGINX、Envoy、Traefik和Caddy相比较。NGINX起初是Web服务器，后来才加入负载均衡；而HAProxy从一开始就设计为纯粹的负载均衡器与代理<sup>[17](https://www.techeia.com/blog/haproxy-vs-nginx-load-balancer)</sup>。在Loggly于2018年在一台配备两个vCPU的AWS实例上对五款负载均衡器进行的基准测试中，Envoy吞吐量最高，而HAProxy在HTTP延迟最低一项上以微弱优势胜出，两者在HTTPS延迟上持平；该基准测试自身的结论是，不同配置与工作负载会产生不同结果<sup>[18](https://www.loggly.com/blog/benchmarking-5-popular-load-balancers-nginx-haproxy-envoy-traefik-and-alb/)</sup>。2026年在一台小型VPS上进行的HTTPS基准测试发现，nginx与HAProxy大致并列吞吐量领先，HAProxy在5000并发连接下达到每秒17,050次响应，且在高达20,000连接时未出现非200响应或客户端错误<sup>[19](https://tako.sh/blog/tako-vs-envoy-caddy-haproxy-nginx-https-proxy-benchmarks/)</sup>。

在运维层面，HAProxy完全没有默认超时，而Envoy默认将路由超时设为15秒；它需要显式指令来设置`X-Forwarded-*`头部，而Caddy和Traefik会自动设置<sup>[20](https://zigproxy.com/reverse-proxy/reverse-proxy-comparison/)</sup>。

## References

1. [Willy Tarreau (2019). The History of HAProxy. HAProxy Technologies blog.](https://cdn.haproxy.com/blog/the-history-of-haproxy)
2. [Universidad ORT Uruguay, description of HAProxy (Spanish).](https://rad.ort.edu.uy/bitstreams/951db8d7-85f5-4e80-883b-63cf5064f7e9/download)
3. [Introduction, HAProxy Enterprise 2.4r1 Configuration Manual.](https://www.haproxy.com/documentation/haproxy-configuration-manual/new/2-4r1/intro/)
4. [Configuration Manual, HAProxy Enterprise latest.](https://cdn.haproxy.com/documentation/haproxy-configuration-manual/new/latest/)
5. [News, haproxy.org.](https://www.haproxy.org/news.html)
6. [HAProxy project homepage, latest versions table.](https://www2.haproxy.org/)
7. [HTTP protocol support, HAProxy configuration tutorials.](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/http/)
8. [HAProxy Success Stories.](https://www.haproxy.com/success-stories)
9. [TCP protocol support, HAProxy configuration tutorials.](https://cdn.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/tcp/)
10. [HAProxy Application Delivery for Microservices datasheet.](https://www.haproxy.com/assets/content-library/datasheets/haproxy-microservices.pdf)
11. [Health checks, HAProxy configuration tutorials.](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/reliability/health-checks/)
12. [Configuration Manual, HAProxy Enterprise 2.2r1.](https://cdn.haproxy.com/documentation/haproxy-configuration-manual/new/2-2r1/)
13. [Nick Ramirez and Ashley Morris (2026). Announcing HAProxy 3.4. HAProxy Technologies blog.](https://cdn.haproxy.com/blog/announcing-haproxy-3-4)
14. [Upgrading to HAProxy in a Load-Balancer-as-a-Service Platform with Booking.com.](https://cdn.haproxy.com/success-stories/upgrading-to-haproxy-in-a-load-balancer-as-a-service-platform-with-booking-com)
15. [Modernizing Government Infrastructure with HAProxy Enterprise and Kubernetes (Recreation.gov / Booz Allen Hamilton).](https://www.haproxy.com/success-stories/modernizing-government-infrastructure-with-haproxy-enterprise-and-kubernetes)
16. [HAPTIC, the HAProxy Ingress Controller: architecture overview.](https://haproxy-haptic.org/docs/latest/development/design/architecture-overview/)
17. [HAProxy vs NGINX as a Load Balancer: Deep Technical Review, Techeia.](https://www.techeia.com/blog/haproxy-vs-nginx-load-balancer)
18. [Gerred Dillon (2018). Benchmarking 5 Popular Load Balancers: Nginx, HAProxy, Envoy, Traefik, and ALB. Loggly.](https://www.loggly.com/blog/benchmarking-5-popular-load-balancers-nginx-haproxy-envoy-traefik-and-alb/)
19. [Tako vs Envoy, Caddy, HAProxy, and Nginx: HTTPS Proxy Benchmarks on a Small VPS.](https://tako.sh/blog/tako-vs-envoy-caddy-haproxy-nginx-https-proxy-benchmarks/)
20. [nginx vs HAProxy vs Envoy vs Caddy vs Traefik, ZigProxy.](https://zigproxy.com/reverse-proxy/reverse-proxy-comparison/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Named software products and platforms › Web servers and content platforms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/haproxy

English original: https://www.edgepedia.org/haproxy
