易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 4852|回复: 0
收起左侧

HaProxy详解

[复制链接]
发表于 2016-8-17 13:17:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x
本帖最后由 rui 于 2016-8-17 13:32 编辑
/ p  n8 i0 H7 x" |1 }5 v# z2 F9 X. [4 w: _6 i( O5 l" M
一、HAProxy简介

(1)HAProxy 是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。 HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在时下的硬件上,完全可以支持数以万计的 并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

(2)HAProxy 实现了一种事件驱动、单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

(3)HAProxy 支持连接拒绝 : 因为维护一个连接的打开的开销是很低的,有时我们很需要限制攻击蠕虫(attack bots),也就是说限制它们的连接打开从而限制它们的危害。 这个已经为一个陷于小型DDoS攻击的网站开发了而且已经拯救

了很多站点,这个优点也是其它负载均衡器没有的。

(4)HAProxy 支持全透明代理(已具备硬件防火墙的典型特点): 可以用客户端IP地址或者任何其他地址来连接后端服务器. 这个特性仅在Linux 2.4/2.6内核打了cttproxy补丁后才可以使用. 这个特性也使得为某特殊服务器处理部分流量同时又不修改服务器的地址成为可能。

性能

HAProxy借助于OS上几种常见的技术来实现性能的最大化。

1,单进程、事件驱动模型显著降低了上下文切换的开销及内存占用。

2,O(1)事件检查器(event checker)允许其在高并发连接中对任何连接的任何事件实现即时探测。

3,在任何可用的情况下,单缓冲(single buffering)机制能以不复制任何数据的方式完成读写操作,这会节约大量的CPU时钟周期及内存带宽;

4,借助于Linux 2.6 (>= 2.6.27.19)上的splice()系统调用,HAProxy可以实现零复制转发(Zero-copy forwarding),在Linux 3.5及以上的OS中还可以实现零复制启动(zero-starting);

5,内存分配器在固定大小的内存池中可实现即时内存分配,这能够显著减少创建一个会话的时长;

6,树型存储:侧重于使用作者多年前开发的弹性二叉树,实现了以O(log(N))的低开销来保持计时器命令、保持运行队列命令及管理轮询及最少连接队列;

7,优化的HTTP首部分析:优化的首部分析功能避免了在HTTP首部分析过程中重读任何内存区域;

8,精心地降低了昂贵的系统调用,大部分工作都在用户空间完成,如时间读取、缓冲聚合及文件描述符的启用和禁用等;

所有的这些细微之处的优化实现了在中等规模负载之上依然有着相当低的CPU负载,甚至于在非常高的负载场景中,5%的用户空间占用率和95%的系统空间占用率也是非常普遍的现象,这意味着HAProxy进程消耗比系统空间消耗低20倍以上。因此,对OS进行性能调优是非常重要的。即使用户空间的占用率提高一倍,其CPU占用率也仅为10%,这也解释了为何7层处理对性能影响有限这一现象。由此,在高端系统上HAProxy的7层性能可轻易超过硬件负载均衡设备。

在生产环境中,在7层处理上使用HAProxy作为昂贵的高端硬件负载均衡设备故障故障时的紧急解决方案也时长可见。硬件负载均衡设备在“报文”级别处理请求,这在支持跨报文请求(request across multiple packets)有着较高的难度,并且它们不缓冲任何数据,因此有着较长的响应时间。对应地,软件负载均衡设备使用TCP缓冲,可建立极长的请求,且有着较大的响应时间。

HAProxy目前主要有三个版本: 1.3 , 1.4 ,1.5,CentOS6.6 自带的RPM包为 1.5 的。

二,安装配置HAProxy

以下实验环境均为CentOS6.6 i686平台。

1,安装haproxy1 Q1 K9 c+ W% `$ d3 B
2 `, j$ ]2 H5 B. w) Y
1

: p* A% J# J" l# M0 j, a2 k- O
2
" d2 Z! E) P! Y7 N
3

- @5 i% U% z1 R( i% r
4

& b- D. S: {- {4 S+ U% E9 h+ z
5
$ t6 V. n( k3 Q2 v) P; h# b
6
& O6 I; l% B% q8 i9 u: L0 r; K
7
, J# `/ @! }7 d% ?" T* C) H$ P; G
8

( l6 B( ?/ Q( I" ^' T) |6 S
9
' W: I+ U5 N; i, h% ]: `2 N
10

/ m- ]4 _' k0 g0 Q3 j
11
+ \/ I# c' `; d  d9 L
12
4 m! n& H/ Q4 l3 v3 T/ ?# x: R# G
13
" O0 {3 k4 f% \
14

8 n: ]8 S" r  o$ W& \8 x
15
- M+ D* T" t3 c; [
16
! g4 ?' T! D& F1 |
17

& E* a) h3 V2 e$ F/ Q) w
18

, a1 l2 e& l) D& _, ?& i0 h2 \
19

) ?' H" J& [& f
20

! Y2 c6 t( y/ I2 _, K! b
21

0 ~" Q- q8 U) `+ g3 C
22
! Z; S5 T+ _1 d: |( [6 L( M

$ Y0 I( q/ R, e5 `1 D/ f* u
[color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]root[color=rgb(216, 216, 216) !important]@[color=rgb(189, 183, 107) !important]LB[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]~[color=rgb(216, 216, 216) !important][color=rgb(155, 155, 139) !important]# yum install -y haproxy #直接使用RPM来安装/ J2 p9 [9 ]4 m0 ]0 |% W6 S
[color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]root[color=rgb(216, 216, 216) !important]@[color=rgb(189, 183, 107) !important]LB[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]~[color=rgb(216, 216, 216) !important][color=rgb(155, 155, 139) !important]# rpm -qi haproxy #版本为1.5.4
$ {( \& J: C$ N& H/ z  s! _[color=rgb(189, 183, 107) !important]Name[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]haproxy [color=rgb(189, 183, 107) !important]Relocations[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]([color=rgb(32, 176, 218) !important]not[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]relocatable[color=rgb(216, 216, 216) !important])( T, c' [4 Z, F
[color=rgb(189, 183, 107) !important]Version[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1.5.4[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]Vendor[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]CentOS
7 ^/ h8 w& B1 I8 w& c, A" V+ S[color=rgb(189, 183, 107) !important]Release[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2.el6[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]Build [color=rgb(189, 183, 107) !important]Date[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]Thu[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]23[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]Jul[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2015[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]04[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]26[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]35[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]PM [color=rgb(255, 128, 0) !important]PDT
5 {  S+ y6 O- X+ ~# V* w' S[color=rgb(255, 128, 0) !important]Install [color=rgb(189, 183, 107) !important]Date[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]Sat[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]29[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]Aug[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2015[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]06[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]49[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]30[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]PM [color=rgb(255, 128, 0) !important]PDT [color=rgb(255, 128, 0) !important]Build [color=rgb(189, 183, 107) !important]Host[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]c6b9[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]bsys[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]dev[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]centos[color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]org6 _7 e* n$ a  V. l, y
[color=rgb(189, 183, 107) !important]Group[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]System [color=rgb(189, 183, 107) !important]Environment[color=rgb(218, 218, 218) !important]/[color=rgb(255, 128, 0) !important]Daemons [color=rgb(255, 128, 0) !important]Source [color=rgb(189, 183, 107) !important]RPM[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]haproxy[color=rgb(218, 218, 218) !important]-[color=rgb(231, 163, 122) !important]1.5.4[color=rgb(218, 218, 218) !important]-[color=rgb(231, 163, 122) !important]2.el6.src.rpm" A1 X& b* E; Y1 P+ Y' k$ A! R# }
[color=rgb(189, 183, 107) !important]Size[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2542578[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]License[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]GPLv2[color=rgb(218, 218, 218) !important]++ G8 r! r9 {' L, f% l  c" F
[color=rgb(189, 183, 107) !important]Signature[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]RSA[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]SHA1[color=rgb(216, 216, 216) !important],[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]Fri[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]24[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]Jul[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2015[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]01[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]39[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]18[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]PM [color=rgb(189, 183, 107) !important]PDT[color=rgb(216, 216, 216) !important],[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]Key [color=rgb(220, 220, 220) !important]ID[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]0946fca2c105b9de
" z' `; j8 v7 Y! ~0 b8 i  q[color=rgb(189, 183, 107) !important]Packager[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]CentOS [color=rgb(189, 183, 107) !important]BuildSystem[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]:[color=rgb(87, 166, 74) !important]//bugs.centos.org>- ?3 w3 k$ P% H" i1 U
[color=rgb(189, 183, 107) !important]URL[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]:[color=rgb(87, 166, 74) !important]//www.haproxy.org/
0 B+ z2 {( q1 }& P# v& E[color=rgb(189, 183, 107) !important]Summary[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]HAProxy [color=rgb(32, 176, 218) !important]is[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]a[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]TCP[color=rgb(218, 218, 218) !important]/[color=rgb(255, 128, 0) !important]HTTP [color=rgb(255, 128, 0) !important]reverse [color=rgb(255, 128, 0) !important]proxy [color=rgb(32, 176, 218) !important]for[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]high [color=rgb(255, 128, 0) !important]availability [color=rgb(220, 220, 220) !important]environments& `/ j8 c% |7 W6 K; n
[color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]root[color=rgb(216, 216, 216) !important]@[color=rgb(189, 183, 107) !important]LB[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]~[color=rgb(216, 216, 216) !important][color=rgb(155, 155, 139) !important]# rpm -ql haproxy1 u  F. j% Q% w/ ?$ x
[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy0 T' T: E1 M  _6 c& Z1 {  U
[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]cfg[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]>配置文件
7 [% w1 T. q! B[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]logrotate[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]d[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy5 |# r7 x% W) I
[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]rc[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]d[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]init[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]d[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy0 B1 b" ?& ^* q4 M
[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]sysconfig[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy+ {( S* x* y, Q' N7 T0 }$ ]
[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]usr[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]bin[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]halog
. z7 Z4 j1 @! _" e5 C; G[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]usr[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]bin[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]iprange
& S7 O) M. B/ K* B7 n, U[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]usr[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]sbin[color=rgb(218, 218, 218) !important]/[color=rgb(220, 220, 220) !important]haproxy7 T- g$ }# [" H% y3 b2 a
[color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]root[color=rgb(216, 216, 216) !important]@[color=rgb(189, 183, 107) !important]LB[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]~[color=rgb(216, 216, 216) !important][color=rgb(155, 155, 139) !important]# cd /etc/haproxy/
& z2 t) w' H9 I+ a" h; W* N: P3 F8 Q7 P4 k  M. a' s  R. |( `8 E
' V* W  D+ O; O

' ^$ b3 \+ b0 q" d# q/ z- t  R
! \$ q  J5 h: o- ~! |: g2 \* e( B# y2,详解配置文件

haproxy 的配置文件由两部分组成:全局设定和对代理的设定,共分为五段:global,defaults,frontend,backend,listen。

2.1 配置文件格式

HAProxy的配置处理3类来主要参数来源:

——最优先处理的命令行参数;

——“global”配置段,用于设定全局配置参数;

——proxy相关配置段,如“defaults”、“listen”、“frontend”和“backend”;

2.2 时间格式

一些包含了值的参数表示时间,如超时时长。这些值一般以毫秒为单位,但也可以使用其它的时间单位后缀。

" j6 {# T) y' l
1
2 w# D3 w) j" q, j! E2 z
2
) w* a5 h4 p. @+ r8 |
3

/ V3 A. I4 M8 ^$ [1 G
4
6 X3 }2 i: f2 j% J4 [
5

. u5 R3 l7 \# q+ v4 P4 }+ L
6
, V9 x  r. i2 _* w" D

1 |) Y" v/ Q5 k3 ~4 j) s
[color=rgb(189, 183, 107) !important]us[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] 微秒[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]microseconds[color=rgb(216, 216, 216) !important]),即[color=rgb(231, 163, 122) !important]1[color=rgb(218, 218, 218) !important]/[color=rgb(231, 163, 122) !important]1000000秒;
* t# q5 }7 R; E# E, ^% G[color=rgb(189, 183, 107) !important]ms[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] 毫秒[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]milliseconds[color=rgb(216, 216, 216) !important]),即[color=rgb(231, 163, 122) !important]1[color=rgb(218, 218, 218) !important]/[color=rgb(231, 163, 122) !important]1000秒;+ Q2 z2 ]8 @- B0 l, Q; \
[color=rgb(189, 183, 107) !important]s[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] 秒[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]seconds[color=rgb(216, 216, 216) !important])! L5 g! ]( t# ]( z4 O2 _4 e5 x
[color=rgb(189, 183, 107) !important]m[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] 分钟[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]minutes[color=rgb(216, 216, 216) !important])9 W7 K3 ]9 _3 P+ w$ K& M5 y
[color=rgb(220, 220, 220) !important]h:小时[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]hours[color=rgb(216, 216, 216) !important])% [6 d$ G* c  R7 h6 u* l
[color=rgb(189, 183, 107) !important]d[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] 天[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]days[color=rgb(216, 216, 216) !important])
9 s/ S4 w5 b& d: N2 ^; o) m
, Q5 Y  K7 A/ |; W) ~3 N! `8 U  U
; w# _4 s9 R5 i7 h' A
- K7 K( a* |& f. M: Y- s  Z4 g

8 \' h* B# M/ H2 _/ D6 p5 w2 n6 @9 ]2.3 全局配置

* 进程管理及安全相关的参数

– chroot <jail dir>:修改haproxy的工作目录至指定的目录并在放弃权限之前执行chroot()操作,可以提升haproxy的安全级别,不过需要注意的是要确保指定的目录为空目录且任何用户均不能有写权限;

– daemon:让haproxy以守护进程的方式工作于后台,其等同于“-D”选项的功能,当然,也可以在命令行中以“-db”选项将其禁用;

– gid <number>:以指定的GID运行haproxy,建议使用专用于运行haproxy的GID,以免因权限问题带来风险;

– group <group name>:同gid,不过指定的组名;

– log <address> <facility> [max level [min level]]:定义全局的syslog服务器,最多可以定义两个;

– log-send-hostname [<string>]:在syslog信息的首部添加当前主机名,可以为“string”指定的名称,也可以缺省使用当前主机名;

– nbproc <number>:指定启动的haproxy进程的个数,只能用于守护进程模式的haproxy;默认只启动一个进程,鉴于调试困难等多方面的原因,一般只在单进程仅能打开少数文件描述符的场景中才使用多进程模式;

– pidfile:

– uid:以指定的UID身份运行haproxy进程;

– ulimit-n:设定每进程所能够打开的最大文件描述符数目,默认情况下其会自动进行计算,因此不推荐修改此选项;Linux默认单进程打开文件数为1024个

– user:同uid,但使用的是用户名;

– stats:用户访问统计数据的接口

– node:定义当前节点的名称,用于HA场景中多haproxy进程共享同一个IP地址时;

– description:当前实例的描述信息;

, E" c$ ~# A6 u  s- P

* 性能调整相关的参数

– maxconn <number>:设定每个haproxy进程所接受的最大并发连接数,其等同于命令行选项“-n”;“ulimit -n”自动计算的结果正是参照此参数设定的;

– maxpipes <number>:haproxy使用pipe完成基于内核的tcp报文重组,此选项则用于设定每进程所允许使用的最大pipe个数;每个pipe会打开两个文件描述符,因此,“ulimit -n”自动计算时会根据需要调大此值;默认为maxconn/4,其通常会显得过大;

– noepoll:在Linux系统上禁用epoll机制;

– nokqueue:在BSE系统上禁用kqueue机制;

– nopoll:禁用poll机制;

– nosepoll:在Linux禁用启发式epoll机制;

– nosplice:禁止在Linux套接字上使用内核tcp重组,这会导致更多的recv/send系统调用;不过,在Linux 2.6.25-28系列的内核上,tcp重组功能有bug存在;

– spread-checks <0..50, in percent>:在haproxy后端有着众多服务器的场景中,在精确的时间间隔后统一对众服务器进行健康状况检查可能会带来意外问题;此选项用于将其检查的时间间隔长度上增加或减小一定的随机时长;

– tune.bufsize <number>:设定buffer的大小,同样的内存条件小,较小的值可以让haproxy有能力接受更多的并发连接,较大的值可以让某些应用程序使用较大的cookie信息;默认为16384,其可以在编译时修改,不过强烈建议使用默认值;

– tune.chksize <number>:设定检查缓冲区的大小,单位为字节;更大的值有助于在较大的页面中完成基于字符串或模式的文本查找,但也会占用更多的系统资源;不建议修改;

– tune.maxaccept <number>:设定haproxy进程内核调度运行时一次性可以接受的连接的个数,较大的值可以带来较大的吞吐率,默认在单进程模式下为100,多进程模式下为8,设定为-1可以禁止此限制;一般不建议修改;

– tune.maxpollevents <number>:设定一次系统调用可以处理的事件最大数,默认值取决于OS;其值小于200时可节约带宽,但会略微增大网络延迟,而大于200时会降低延迟,但会稍稍增加网络带宽的占用量;

– tune.maxrewrite <number>:设定为首部重写或追加而预留的缓冲空间,建议使用1024左右的大小;在需要使用更大的空间时,haproxy会自动增加其值;

– tune.rcvbuf.client <number>:

– tune.rcvbuf.server <number>:设定内核套接字中服务端或客户端接收缓冲的大小,单位为字节;强烈推荐使用默认值;

– tune.sndbuf.client:

– tune.sndbuf.server:

  M9 T" a4 X6 k7 R

* Debug相关的参数


+ T/ y) o8 m. g& q
1
* c) u' i4 S5 T3 B
2
' K8 ?1 Z( c; z' o0 V" n: K
3
: G* n" M/ j! T6 z4 h9 n0 ]6 b: b
& [9 B8 h8 I  }7 Z& b& \1 Y
[color=rgb(218, 218, 218) !important]-[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]debug$ Q3 A0 k1 e/ G7 d& T" v
5 ~+ K2 o" H) C# D0 A, P
[color=rgb(218, 218, 218) !important]-[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]quiet- J) w7 A6 u2 _2 c: z4 C  c
; T% _4 x+ }" z  z
# e$ ?( k" N+ v; @$ V
- a( o( Z3 R( w- \

* 超时时长

8 ?8 G( q; k" ^5 B' C! F0 d7 b
1

7 w4 U" M4 w3 R
2
5 u: }9 r* `" X) Z+ j
3
- w9 r6 p# D" m7 u  n* y* |* I; U2 a
4

9 Y5 F8 u% S/ y, k( C" ~
5
/ C  D* r- |8 |4 t# ^. a0 q0 }3 _# V
6
& I  J" ]5 h. k* O/ y! {* Y  M
7

! l! i9 L2 g6 F9 p& d
8
8 s* [, ^4 V, ~0 N. l: Y
9

" \: z# S) f( b( U) [$ M
10

9 H- `" V6 C9 q" [& @' \
11
: E' o3 i; U% M( @# R8 g

4 }$ o- F+ [- M3 w7 `! F1 y
[color=rgb(255, 128, 0) !important]timeout [color=rgb(255, 128, 0) !important]http [color=rgb(220, 220, 220) !important]request[color=rgb(0, 111, 224) !important] :在客户端建立连接但不请求数据时,关闭客户端连接7 b4 q2 V; d, y! n6 v3 Q; R7 W
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]queue[color=rgb(0, 111, 224) !important] :等待最大时长
& z6 a( t' {& Y2 O[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]connect:[color=rgb(0, 111, 224) !important] 定义[color=rgb(220, 220, 220) !important]haproxy将客户端请求转发至后端服务器所等待的超时时长
, b* G, s7 T& t; z: [. a  ?[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]client:客户端非活动状态的超时时长
7 \+ o& V5 @& r5 @& v# B( W6 i[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]server:客户端与服务器端建立连接后,等待服务器端的超时时长,
8 u9 ^& K! M# }9 x0 D' X8 L3 K[color=rgb(255, 128, 0) !important]timeout [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]keep[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]alive[color=rgb(0, 111, 224) !important] :定义保持连接的超时时长" V3 ~3 M5 M* A. r! j" k& R/ o
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]check:健康状态监测时的超时时间,过短会误判,过长资源消耗
( J  H0 `5 A# e- a: \[color=rgb(189, 183, 107) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:每个[color=rgb(220, 220, 220) !important]server最大的连接数" J2 y3 z3 _8 Z" E, G
% x/ ~1 K$ u2 ^6 I" @: X' `( D
[color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]server[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]close[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] 在使用长连接时,为了避免客户端超时没有关闭长连接,此功能可以使服务器端关闭长连接" r% N! o% H" W; D& X7 r
[color=rgb(220, 220, 220) !important]redispatch:[color=rgb(0, 111, 224) !important] 在使用基于[color=rgb(220, 220, 220) !important]cookie定向时,一旦后端某一[color=rgb(220, 220, 220) !important]server宕机时,会将会话重新定向至某一上游服务器,必须使用[color=rgb(0, 111, 224) !important] 的选项
6 s/ b6 _: h. o2 Y9 U. r
$ e1 Y* P8 _- E3 ~' \% x8 J% c

/ _5 b4 `% w5 Z9 _  p1 S) w
) F+ H7 l( m' w! n$ C

* 实现访问控制:

) T1 R! R( D; \; z
1
9 h8 X3 y; r) F" K  _
2
6 U2 J& Z; L# l1 h* {* b
' Y- S6 v0 }8 @* S. @
[color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]request[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]7层过滤
/ R, R' X2 z  t* q! I[color=rgb(189, 183, 107) !important]tcp[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]request [color=rgb(189, 183, 107) !important]content[color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]tcp层过滤,四层过滤& S6 ]! \& R: w

1 g6 H# Z, _( j' }
  G* _% I- C3 y& j/ ]; e7 |8 Z8 Z

/ D4 p( p# W& Q8 f( M% X+ M  h
4 G8 Y/ q. t3 ^) {# G+ q5 n2.4 代理

代理相关的配置可以如下配置段中。


8 c/ d& c8 N6 q7 G* {" S
1
3 u$ r& }# k4 l3 U& ~+ i- [
2

' n' }* e( W) G& _9 k
3
; E/ |5 V% l0 y: U5 l+ b9 F, V
4

; Y  R: l* B2 k  p' X+ y# m' p% H; T$ A
–[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]defaults[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>
; I( i8 s- g1 Q0 t" Z6 A–[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]frontend[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>
3 \1 `" t: f% L! F! [. e1 g/ }. N+ a–[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]backend[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>$ X+ P  a0 ^: x9 S6 k$ h; L
–[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]listen[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>% W6 ^9 b! N% v, k+ D( A/ K% T
* e. r9 @3 ]2 o  L* K

* T+ s, h. |8 m2 z) x/ o
. L: N8 [; Q" J" H# @2 j) x5 f

“defaults”段用于为所有其它配置段提供默认参数,这配置默认配置参数可由下一个“defaults”所重新设定。, \  ~. e$ b1 \. r) j' n
“frontend”段用于定义一系列监听的套接字,这些套接字可接受客户端请求并与之建立连接。
# ?) r( Q9 b0 F, `; b: }“backend”段用于定义一系列“后端”服务器,代理将会将对应客户端的请求转发至这些服务器。
6 @4 B$ ]* G; t, `2 k5 h0 c“listen”段通过关联“frontend”和“backend”定义了一个完整的代理,通常只对TCP流量有用。

所有代理的名称只能使用大写字母、小写字母、数字、-(中线)、_(下划线)、.(点号)和:(冒号)。此外,ACL名称会区分字母大小写。

三、配置文件中的关键字参考3.1 balance3 w" `9 c; Q9 `; R' W& r& @

7 f1 j6 ^! e9 a4 G, F; s8 e
1

" w0 r  C! n3 W* O/ K2 J' n
2

2 r" m& E' Z8 g7 U3 F% b
3
$ Z* }' W1 p2 `; L7 k

  }- C! ?2 |; w" n8 k/ ~# K, J
[color=rgb(220, 220, 220) !important]balance[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]
4 R5 z; m1 t4 v4 ]
, h6 w" |+ F; R[color=rgb(255, 128, 0) !important]balance [color=rgb(189, 183, 107) !important]url[color=rgb(216, 216, 216) !important]_param[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]check[color=rgb(216, 216, 216) !important]_post[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(216, 216, 216) !important][color=rgb(216, 216, 216) !important]
- \5 k( K- s$ [3 @% C$ N7 Z
4 K. N! ~) j# x1 I5 b7 z" B( e$ s
. J/ [- |  f) \6 a: T( i
& ?" Y  g  W7 x3 B  Y9 ]

定义负载均衡算法,可用于“defaults”、“listen”和“backend”。用于在负载均衡场景中挑选一个server,其仅应用于持久信息不可用的条件下或需要将一个连接重新派发至另一个服务器时。支持的算法有:

3.11 roundrobin:基于权重进行轮叫,在服务器的处理时间保持均匀分布时,这是最平衡、最公平的算法。此算法是动态的,这表示其权重可以在运行时进行调整,不过,在设计上,每个后端服务器仅能最多接受4128个连接;并支持慢启动。

3.12 static-rr:基于权重进行轮叫,与roundrobin类似,但是为静态方法,在运行时调整其服务器权重不会生效;不过,其在后端服务器连接数上没有限制;不支持慢启动,在高负荷的情况下,服务器重新上线时会立即被分配大量连接。

3.13 leastconn(WLC):适用于长连接的会话,新的连接请求被派发至具有最少连接数目的后端服务器;在有着较长时间会话的场景中推荐使用此算法,如LDAP、SQL等,其并不太适用于较短会话的应用层协议,如HTTP;此算法是动态的,

可以在运行时调整其权重;

3.14 source:将请求的源地址进行hash运算,并由后端服务器的权重总数相除后派发至某匹配的服务器;这可以使得同一个客户端IP的请求始终被派发至某特定的服务器;不过,当服务器权重总数发生变化时,如某服务器宕机或添加了新的服务器,许多客户端的请求可能会被派发至与此前请求不同的服务器;常用于负载均衡无cookie功能的基于TCP的协议;其默认为静态,不过也可以使用hash-type修改此特性;

1,对原地址hash,第一次调度时使用WLC% a. g# K- A/ A* k3 a! }
source:IP层,位于同一个NAT服务器背后的多个请求都会定向至同一个upstream server,不利于负载均衡,一般只有不支持使用cookie插入又需要保持会话时使用
& U8 e- H+ R( wcookie:应用层,有更好的负载均衡效果;

2,hash/weight%ip :除以权重取模

3.15 uri:对URI的左半部分(“问题”标记之前的部分)或整个URI进行hash运算,并由服务器的总权重相除后派发至某匹配的服务器;这可以使得对同一个URI的请求总是被派发至某特定的服务器,除非服务器的权重总数发生了变化;此算法常用于代理缓存或反病毒代理以提高缓存的命中率;需要注意的是,此算法仅应用于HTTP后端服务器场景;其默认为静态算法,不过也可以使用hash-type修改此特性;

3.16 url_param:通过<argument>为URL指定的参数在每个HTTP GET请求中将会被检索;如果找到了指定的参数且其通过等于号“=”被赋予了一个值,那么此值将被执行hash运算并被服务器的总权重相除后派发至某匹配的服务器;此算法可以通过追踪请求中的用户标识进而确保同一个用户ID的请求将被送往同一个特定的服务器,除非服务器的总权重发生了变化;如果某请求中没有出现指定的参数或其没有有效值,则使用轮叫算法对相应请求进行调度;此算法默认为静态的,不过其也可以使用hash-type修改此特性;

3.17 hdr(<name>):对于每个HTTP请求,通过<name>指定的HTTP首部将会被检索;如果相应的首部没有出现或其没有有效值,则使用轮叫算法对相应请求进行调度;其有一个可选选项“use_domain_only”,可在指定检索类似Host类的首部时仅计算域名部分(比如通过www.feiyu.com来说,仅计算feiyu字符串的hash值)以降低hash算法的运算量;此算法默认为静态的,不过其也可以使用hash-type修改此特性;

3.18 rdp-cookie(name)

,表示根据据cookie(name)来锁定并哈希每一次TCP请求。

3.2 bind
2 C2 m9 R0 ?" I/ J6 ^/ o& u
& M; D8 R) j. g$ f; c
1
' b* ?8 X4 F  [' A1 ]2 \
2
, Q" l5 W8 \$ o% P6 d; d7 |
3
. r0 T; e% T& x+ U+ Q( t
8 s# ?4 e1 c& q* Z0 d6 i4 M! z
[color=rgb(220, 220, 220) !important]bind[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]address[color=rgb(218, 218, 218) !important]>[color=rgb(216, 216, 216) !important][color=rgb(218, 218, 218) !important]:[color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]port_range[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(216, 216, 216) !important],[color=rgb(0, 111, 224) !important] …[color=rgb(216, 216, 216) !important]
7 I4 ~8 S9 w2 M2 z$ [0 Q8 y( J
, q% t0 ?# L, a( H. \7 O[color=rgb(220, 220, 220) !important]bind[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]address[color=rgb(218, 218, 218) !important]>[color=rgb(216, 216, 216) !important][color=rgb(218, 218, 218) !important]:[color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]port_range[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(216, 216, 216) !important],[color=rgb(0, 111, 224) !important] …[color=rgb(216, 216, 216) !important][color=rgb(0, 111, 224) !important] [color=rgb(244, 187, 21) !important]interface[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(244, 187, 21) !important]interface[color=rgb(218, 218, 218) !important]>  [1 [" v% `5 x& f( x, G$ K( N

( `# g* K, w% V7 m

# I, ^( b1 {8 I! y
' Q# w/ p! {6 L  E! {  A

此指令仅能用于frontend和listen区段,用于定义一个或几个监听的套接字。

<address>:可选选项,其可以为主机名、IPv4地址、IPv6地址或*;省略此选项、将其指定为*或0.0.0.0时,将监听当前系统的所有IPv4地址;<port_range>:可以是一个特定的TCP端口,也可是一个端口范围(如5005-5010),代理服务器将通过指定的端口来接收客户端请求;需要注意的是,每组监听的套接字<address:port>在同一个实例上只能使用一次,而且小于1024的端口需要有特定权限的用户才能使用,这可能需要通过uid参数来定义;<interface>:指定物理接口的名称,仅能在Linux系统上使用;其不能使用接口别名,而仅能使用物理接口名称,而且只有管理有权限指定绑定的物理接口;

3.3 mode
- `1 ], L3 T# h; T( h' ]) O4 p+ R0 h7 t5 r/ c  k* \5 N! [
1

& \7 u2 h5 e: z
  j* G; \! S  S  f: X! r* H
[color=rgb(255, 128, 0) !important]mode[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]{[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]tcp[color=rgb(218, 218, 218) !important]|[color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]|[color=rgb(220, 220, 220) !important]health[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]}
/ v. T$ y- Q/ ~- x
$ i% m' h, `4 r7 U8 C
; q" o5 w! a# a+ T* v7 w* }

8 s3 M3 a! m! c# j' v

设定实例的运行模式或协议。当实现内容交换时,前端和后端必须工作于同一种模式(一般说来都是HTTP模式),否则将无法启动实例。

tcp:实例运行于纯TCP模式,在客户端和服务器端之间将建立一个全双工的连接,且不会对7层报文做任何类型的检查;通常用于SSL、SSH、SMTP等应用;
$ T0 q# L' c2 Shttp:实例运行于HTTP模式,客户端请求在转发至后端服务器之前将被深度分析,所有不与RFC格式兼容的请求都会被拒绝;此为默认模式;* {. D, D" Y0 _; b/ O; X
health:实例工作于health模式,其对入站请求仅响应“OK”信息并关闭连接,且不会记录任何日志信息;此模式将用于响应外部组件的健康状态检查请求;目前来讲,此模式已经废弃,因为tcp或http模式中的monitor关键字可完成类似功能;

* T  F+ o* H( M* {
3.4 hash-type, ~- Z! m/ P2 z8 z0 V
# e0 J& U. b) Y. L! q5 y. R. a
1
! v0 d. W2 }% e/ X
) y8 X! r3 T! y- z; c+ J
[color=rgb(189, 183, 107) !important]hash[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]type[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]method[color=rgb(218, 218, 218) !important]>
6 A& q4 |# V( O4 P1 V% }( h; }% G) f! }8 U

, n1 c. Y! ]$ ]

2 U+ P% _" u9 c- B

定义用于将hash码映射至后端服务器的方法;其不能用于frontend区段;可用方法有map-based和consistent,在大多数场景下推荐使用默认的map-based方法。

map-based:hash表是一个包含了所有在线服务器的静态数组。其hash值将会非常平滑,会将权重考虑在列,但其为静态方法,对在线服务器的权重进行调整将不会生效,这意味着其不支持慢速启动。此外,挑选服务器是根据其在数组中的

位置进行的,因此,当一台服务器宕机或添加了一台新的服务器时,大多数连接将会被重新派发至一个与此前不同的服务器上,对于缓存服务器的工作场景来说,此方法不甚适用。

consistent:“一致性哈希算法”,hash表是一个由各服务器填充而成的树状结构,将服务器散列在hash环上;基于hash键在hash树中查找相应的服务器时,最近的服务器将被选中。此方法是动态的,支持在运行时修改服务器权重,因此兼

容慢速启动的特性。添加一个新的服务器时,仅会对一小部分请求产生影响,因此,尤其适用于后端服务器为cache的场景。不过,此算法不甚平滑,派发至各服务器的请求未必能达到理想的均衡效果,因此,可能需要不时的调整服务器的权

重以获得更好的均衡性。


  d( e; n2 \( @  X3.5 log7 L# s* W/ c! o2 P8 t
3 v0 J- o( A) u8 [1 c3 u
1

- \- V, ^' P# k. b& l  p
2

1 i% I. D: n5 l8 |
3

) ~- P# k( S" G" A0 u, y& ~8 p, H0 I
* u3 K$ X" k* _( z8 N5 y
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global: ~7 ^" B: X/ N9 _

2 X  v3 N5 x$ O2 g" j[color=rgb(189, 183, 107) !important]log[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]address[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]facility[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]level[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]minlevel[color=rgb(218, 218, 218) !important]>[color=rgb(216, 216, 216) !important][color=rgb(216, 216, 216) !important]7 \+ Q- K9 }) @/ N  x

0 y/ g6 W0 T. g2 B$ B( }$ b3 a

4 w; g( O% b" R( G  R5 M1 z

2 t+ B$ u7 X, o5 z9 E' z

为每个实例启用事件和流量日志,因此可用于所有区段。每个实例最多可以指定两个log参数,不过,如果使用了“log global”且”global”段已经定了两个log参数时,多余了log参数将被忽略。

global:当前实例的日志系统参数同”global”段中的定义时,将使用此格式;每个实例仅能定义一次“log global”语句,且其没有任何额外参数;

<address>:定义日志发往的位置,其格式之一可以为<IPv4_address:PORT>,其中的port为UDP协议端口,默认为514;格式之二为Unix套接字文件路径,但需要留心chroot应用及用户的读写权限;

<facility>:可以为syslog系统的标准facility之一;

<level>:定义日志级别,即输出信息过滤器,默认为所有信息;指定级别时,所有等于或高于此级别的日志信息将会被发送;


5 M* A2 G+ |$ c3.6 maxconn
+ h2 v( k& h! P2 L% r2 z3 ~$ a$ T: @$ a3 \. E
1

2 q1 a# f- }4 T
& ?4 R. p- z* g! y* f. ~8 ~- W
[color=rgb(189, 183, 107) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]conns[color=rgb(218, 218, 218) !important]># o9 {: U8 b* R9 w

! F7 b, N9 l+ n6 h' z: i

$ \) Z8 j! a& @5 A
# o$ e7 _+ i' n4 N

设定一个前端的最大并发连接数,因此,其不能用于backend区段。对于大型站点来说,可以尽可能提高此值以便让haproxy管理连接队列,从而避免无法应答用户请求。当然,此最大值不能超出“global”段中的定义。此外,需要留心的是,haproxy会为每个连接维持两个缓冲,每个缓冲的大小为8KB,再加上其它的数据,每个连接将大约占用17KB的RAM空间。这意味着经过适当优化后,有着1GB的可用RAM空间时将能维护40000-50000并发连接。

如果为<conns>指定了一个过大值,极端场景下,其最终占据的空间可能会超出当前主机的可用内存,这可能会带来意想不到的结果;因此,将其设定了一个可接受值方为明智决定。其默认为2000。

3.7 default_backend
& ]' d0 i5 k  i( Y- ]$ d9 Q# F: e5 }8 j
1
* W( Z4 F8 b/ v
. _2 m9 c/ c: V& u" k7 I4 V- C
[color=rgb(189, 183, 107) !important]default_backend[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]backend[color=rgb(218, 218, 218) !important]>! d: U" `0 G+ A0 _7 S- S. Z

' o% ^6 S0 X5 n. W

' h* V. ~  l0 ]3 I. m& i2 T- s% i. _# Z
7 M- B' u4 z* P% m( T

在没有匹配的”use_backend”规则时为实例指定使用的默认后端,因此,其不可应用于backend区段。在”frontend”和”backend”之间进行内容交换时,通常使用”use-backend”定义其匹配规则;而没有被规则匹配到的请求将由此参数指定的后端接收。

<backend>:指定使用的后端的名称;

使用案例:


' T2 C' d& R/ b$ s  _
1
% D  Q: i1 k0 b! P. a
2

+ i$ m) E- ?" y! J7 }8 K8 V
3
. ]) b9 r8 k( f. ?) N* d5 d

2 m, L5 m% @0 A; t/ E
[color=rgb(255, 128, 0) !important]use_backend [color=rgb(255, 128, 0) !important]dynamic [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]url_dyn
6 M- {% }: Q8 E. W% Q" Y[color=rgb(255, 128, 0) !important]use_backend [color=rgb(86, 156, 214) !important]static[color=rgb(0, 111, 224) !important] [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]url_css [color=rgb(255, 128, 0) !important]url_img [color=rgb(255, 128, 0) !important]extension_img
% _8 ~) @5 ]' a[color=rgb(255, 128, 0) !important]default_backend [color=rgb(189, 183, 107) !important]dynamic
+ |8 x! k+ p& L3 ?7 _( f( i, h* ~. Y$ a3 e: c
: {0 E, _* T! W/ ]$ [2 a

. X! @8 [; E$ Q7 K8 D6 i3 C/ D. N9 F3 p, H' Z3 [3 S
3.8 server
1 M+ L4 ~) `# q4 Z9 O" i- B
& [- d: f2 @* |9 a! o( }; q
1

! |: F6 Z1 c1 b! u/ L" S. g
- ~$ a/ d+ Q) ^5 z8 u' g8 S2 |
[color=rgb(189, 183, 107) !important]server[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]address[color=rgb(218, 218, 218) !important]>[color=rgb(216, 216, 216) !important][[color=rgb(218, 218, 218) !important]:[color=rgb(189, 183, 107) !important]port[color=rgb(216, 216, 216) !important][color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(255, 128, 0) !important]param*[color=rgb(216, 216, 216) !important]0 z& S" {% Y9 b% z7 n

/ @. F+ X7 ^% g% u+ K

* x9 p) ?, E1 ]- A5 t

  p% E, D5 R/ U7 r, Y, T7 D+ l* k

为后端声明一个server,因此,不能用于defaults和frontend区段。

<name>:为此服务器指定的内部名称,其将出现在日志及警告信息中;如果设定了”http-send-server-name”,它还将被添加至发往此服务器的请求首部中;

<address>:此服务器的的IPv4地址,也支持使用可解析的主机名,只不过在启动时需要解析主机名至相应的IPv4地址;

[:port]:指定将连接请求所发往的此服务器时的目标端口,其为可选项;未设定时,将使用客户端请求时的同一相端口;

[param*]:为此服务器设定的一系参数;其可用的参数非常多,具体请参考官方文档中的说明,下面仅说明几个常用的参数;

服务器或默认服务器参数:

backup:设定为备用服务器,仅在负载均衡场景中的其它server均不可用于启用此server;

check:启动对此server执行健康状态检查,其可以借助于额外的其它参数完成更精细的设定,如:

inter <delay>:设定健康状态检查的时间间隔,单位为毫秒,默认为2000;也可以使用fastinter和downinter来根据服务器端状态优化此时间延迟;

rise <count>:设定健康状态检查中,某离线的server从离线状态转换至正常状态需要成功检查的次数;

fall <count>:确认server从正常状态转换为不可用状态需要检查的次数;

cookie <value>:为指定server设定cookie值,此处指定的值将在请求入站时被检查,第一次为此值挑选的server将在后续的请求中被选中,其目的在于实现持久连接的功能;

maxconn <maxconn>:指定此服务器接受的最大并发连接数;如果发往此服务器的连接数目高于此处指定的值,其将被放置于请求队列,以等待其它连接被释放;

haproxy 有n个进程,每个支持m个连接,后端有x个服务器,每个最大支持y个连接,则 n*m <= x*y,如果后端服务器支持排队,则n*m <= x*(y+z),z为每个服务器的排队队列

maxqueue <maxqueue>:设定请求队列的最大长度;

observe <mode>:通过观察服务器的通信状况来判定其健康状态,默认为禁用,其支持的类型有“layer4”和“layer7”,“layer7”仅能用于http代理场景;

redir <prefix>:启用重定向功能,将发往此服务器的GET和HEAD请求均以302状态码响应;需要注意的是,在prefix后面不能使用/,且不能使用相对地址,以免造成循环;例如:

8 }  E4 Z+ G3 @
1

* H8 v. _( @8 K9 i0 H0 m2 B+ y' h5 w$ [! R0 j
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]srv1[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.6[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]redir [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]:[color=rgb(87, 166, 74) !important]//imageserver.feiyu.com check: T8 |0 i0 p6 O2 \$ Q9 m) V

9 `0 J0 J4 D/ c" d! B8 P# L

1 Y7 @7 q$ z2 ^- {

: h, g# }: q! ~+ P- S6 o

weight <weight>:权重,默认为1,最大值为256,0表示不参与负载均衡(不被调度);

检查方法:

  d" O) L- t: z" I
1

; d) S/ L( r0 U6 p
2

8 U/ Z  q# }# O5 ?
3
! B& \& W/ y- n8 I
4

' w1 k, k) h7 Y0 q5 P  H
5
+ H7 R( ~' m4 Z2 S% {' @+ c1 P8 A
6

8 L/ E+ s& {# @. v
7

* Z! l- G* ^4 i. p3 r
8

( ^1 T5 C/ S1 z  s0 D& k6 E/ j* [0 k$ B
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpchk
' r/ u) s1 X/ d% ][color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpchk& m) i1 K' U5 N; N; J" t
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpchk, R' w! n( t( u6 Q
[color=rgb(255, 128, 0) !important]option [color=rgb(220, 220, 220) !important]httpchk[color=rgb(0, 111, 224) !important] :不能用于[color=rgb(220, 220, 220) !important]frontend段,例如:) J) i, B; B3 |; k7 Y6 ?6 D% m! |
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]https_relay; |; ^$ j* F' a
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]tcp
8 ?; X4 S: F% d  H2 x[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpchk [color=rgb(255, 128, 0) !important]OPTIONS *[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]HTTP[color=rgb(218, 218, 218) !important]/[color=rgb(231, 163, 122) !important]1.1[color=rgb(216, 216, 216) !important]\[color=rgb(189, 183, 107) !important]r[color=rgb(216, 216, 216) !important]\[color=rgb(189, 183, 107) !important]nHost[color=rgb(218, 218, 218) !important]:[color=rgb(216, 216, 216) !important]\[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]www[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]feiyu[color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]com6 G4 e" Q" v9 d* d
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]apache1[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.1[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]443[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]port[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]806 ^1 j1 y3 y2 u. Q
( V2 F4 z9 N5 C( U
3 U& `% {2 C- J

, y7 f2 n5 R% E# F

使用案例:


, T! ?% `$ a2 Y4 D' n! @% z( B8 I
1

1 v$ l! s, Z, y6 C& L6 A
2

) E! I% ~4 a' X3 b3 d' y8 N6 J- Y( w* s0 V9 L$ V0 N
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]first[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.7[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]1080[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]cookie [color=rgb(255, 128, 0) !important]first [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]inter[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10005 M& c9 ~6 T5 K# ~0 Q. @
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]second[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.8[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]1080[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]cookie [color=rgb(255, 128, 0) !important]second [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]inter[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10003 j/ ~3 y' _! m6 j) |0 u  H  J

* F5 @2 v+ a  a; B+ b7 y0 _4 D
9 t+ w7 v$ ?' K! h; u: `

& P7 o4 O. G+ i8 h5 N: Y, a' C: s( l) L5 m# D
3.9 capture request header1 S- ]7 l0 O" W& h
: ?5 g! t! M' v/ V# }; F/ H
1

) H6 Y; z" `$ a6 T* I7 V9 ?* O4 t9 m
[color=rgb(255, 128, 0) !important]capture [color=rgb(255, 128, 0) !important]request [color=rgb(189, 183, 107) !important]header[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]len[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]length[color=rgb(218, 218, 218) !important]>6 T$ [) s9 P2 ^/ L; t4 v; X

8 V8 Z  m+ a) h1 s
5 h& r4 O1 A0 f! }0 X3 d" B# T

) T- _3 Y& [( R* f7 N9 J

捕获并记录指定的请求首部最近一次出现时的第一个值,仅能用于“frontend”和“listen”区段。捕获的首部值使用花括号{}括起来后添加进日志中。如果需要捕获多个首部值,它们将以指定的次序出现在日志文件中,并以竖线“|”作为分隔符。不存在的首部记录为空字符串,最常需要捕获的首部包括在虚拟主机环境中使用的“Host”、上传请求首部中的“Content-length”、快速区别真实用户和网络机器人的“User-agent”,以及代理环境中记录真实请求来源的“X-Forward-For”。

<name>:要捕获的首部的名称,此名称不区分字符大小写,但建议与它们出现在首部中的格式相同,比如大写首字母。需要注意的是,记录在日志中的是首部对应的值,而非首部名称。

<length>:指定记录首部值时所记录的精确长度,超出的部分将会被忽略。

可以捕获的请求首部的个数没有限制,但每个捕获最多只能记录64个字符。为了保证同一个frontend中日志格式的统一性,首部捕获仅能在frontend中定义。


! a6 x4 k9 `5 ^* i6 \3.10 capture response header
/ I& j6 p2 J6 @+ B  D: x7 d9 L( U$ Q+ |1 t0 @
1

$ W3 Q" a" S; }6 Y2 l9 |+ b3 Y
( m7 P; [$ x+ ]' e9 S0 `: X
[color=rgb(255, 128, 0) !important]capture [color=rgb(255, 128, 0) !important]response [color=rgb(189, 183, 107) !important]header[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]len[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]length[color=rgb(218, 218, 218) !important]>% n' K8 M3 G7 H: I: s
6 T& _$ a$ p7 ], c* P4 n
* h( ]$ i: b/ b, l2 q

: ]/ p' x2 M& z5 G; c2 u+ C/ X+ @0 Z1 r9 d

捕获并记录响应首部,其格式和要点同请求首部。


/ Q% ^! K% H1 i% e+ H- ^3.11 stats enable

启用基于程序编译时默认设置的统计报告,不能用于“frontend”区段。只要没有另外的其它设定,它们就会使用如下的配置:

  \# L  d% S2 ?+ R! R5 d, w7 e
1
# }' @0 r2 L, l# ?6 X  l
2

5 A/ S0 e; R5 p
3
3 g2 B( U! g, `! p1 b+ J
4

/ [& N' O/ X& n5 G9 l
" k4 t+ [" Z0 D6 Q* \% i
[color=rgb(218, 218, 218) !important]-[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]uri[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important]?[color=rgb(189, 183, 107) !important]stats- s5 s' z+ d: z7 v- A" ?  b6 w
[color=rgb(218, 218, 218) !important]-[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]realm[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(214, 157, 133) !important]"HAProxy Statistics"' d9 W& X0 I8 b: o) ^
[color=rgb(218, 218, 218) !important]-[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]auth[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]no [color=rgb(189, 183, 107) !important]authentication
9 S0 s- W5 T# k* P$ f' E[color=rgb(218, 218, 218) !important]-[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]scope[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]no [color=rgb(189, 183, 107) !important]restriction. y+ j- a* B) A* t, q* q
" i! S. l' t- `9 |* U
: h! {( q) Q% K

8 O/ I& K, v; }* R/ \5 ^! \5 s! H" [

尽管“stats enable”一条就能够启用统计报告,但还是建议设定其它所有的参数,以免其依赖于默认设定而带来非期后果。下面是一个配置案例。

1 w. o2 a5 S' T5 ?6 J
1
7 H8 |; i7 l) x
2
# Y" n1 `+ ^; b- o2 A8 K0 H
3

/ C% C2 ?2 R( T# n. m
4

3 n; s( D( D6 l2 k
5

6 t9 O# g9 E6 z% ?1 m1 }1 Q+ u
6
) c' m9 E$ R% ?# e' V
7
7 E5 C6 l: N) T0 d5 M
8

4 d" b& ?& W9 u  `/ O+ i: F
9
5 m6 J2 x3 N7 Y, Y+ w! G; r# x
5 e9 F, z8 e5 a1 @# M
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]public_www
/ f; `, h* g9 \& {5 T[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]websrv1[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.11[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80" W8 B- T; X) N2 s+ n5 r& O
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable
5 n7 `# s; I/ ~# `* i6 ~* |/ d$ I% Q% r3 _[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]hide[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]version
7 t/ T' \8 I( B$ e7 I. o+ l1 Q( h3 ^7 ?[color=rgb(255, 128, 0) !important]stats [color=rgb(220, 220, 220) !important]scope[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].
3 v$ y% m/ G8 B5 f' C8 ]# x/ n[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]uri[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxyadmin[color=rgb(216, 216, 216) !important]?[color=rgb(255, 128, 0) !important]stats
& n3 r- y+ @3 z: o# _; _[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]realm [color=rgb(189, 183, 107) !important]Haproxy[color=rgb(216, 216, 216) !important]\[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]Statistics
  e! q2 N2 }# x[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]auth [color=rgb(189, 183, 107) !important]statsadmin[color=rgb(218, 218, 218) !important]:[color=rgb(255, 128, 0) !important]password( g6 G0 z6 @/ {! h9 H4 B) ^( x
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]auth [color=rgb(189, 183, 107) !important]statsmaster[color=rgb(218, 218, 218) !important]:[color=rgb(189, 183, 107) !important]password( w: @1 w+ ^* ]7 l) j3 K
* l* S2 @4 A% Z+ h. Q

; h* k& d* e# s2 q5 }

. N" Q* m6 r! k6 X% d& a/ a7 z3 B( N  ]
3.12 stats hide-version
  e4 W9 p; t9 n; e5 n
* q/ O( o. M. F8 g& C+ g) ^
1

( x: W& H2 `, m. ?7 S$ j- n7 G5 Z/ H2 z: ]
[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]hide[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]version
3 Q: D+ Q4 j" Q+ P# H# v- f% d. H! x( v* R7 c/ i! M4 |3 F

- A2 l1 I3 r" h, u
' }1 Y( b8 D( ?7 |+ H

启用统计报告并隐藏HAProxy版本报告,不能用于“frontend”区段。默认情况下,统计页面会显示一些有用信息,包括HAProxy的版本号,然而,向所有人公开HAProxy的精确版本号是非常有风险的,因为它能帮助恶意用户快速定位版本的缺陷和漏洞。尽管“stats hide-version”一条就能够启用统计报告,但还是建议设定其它所有的参数,以免其依赖于默认设定而带来非期后果。具体请参照“stats enable”一节的说明。

3.13 stats realm3 a# ~" S6 ~) F* F3 e
# l8 X0 f3 q5 c' ?: x( b& ~
1
( r, e8 n- l0 Z1 @5 x! n$ T0 P
& o0 W, T  m  }- i; T2 T/ |. n0 }
[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]realm[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]realm[color=rgb(218, 218, 218) !important]>/ j) m1 W( M/ o8 _" t7 ^6 c3 b
# J) N' {) v- x( ?4 @/ Y7 ^
  M2 C% A/ b+ Q. K9 V. z

* z0 R9 b, u+ i% D$ I

启用统计报告并高精认证领域,不能用于“frontend”区段。haproxy在读取realm时会将其视作一个单词,因此,中间的任何空白字符都必须使用反斜线进行转义。此参数仅在与“stats auth”配置使用时有意义。

<realm>:实现HTTP基本认证时显示在浏览器中的领域名称,用于提示用户输入一个用户名和密码。

尽管“stats realm”一条就能够启用统计报告,但还是建议设定其它所有的参数,以免其依赖于默认设定而带来非期后果。具体请参照“stats enable”一节的说明。

3.14 stats scope  Z5 U* A! {) S& L1 Y
& \" C+ {! f: a; K! l9 Y& c4 K
1

0 x) m- m" `2 F6 W+ g$ `7 \! D' A$ Y( b/ r8 u  V
[color=rgb(255, 128, 0) !important]stats[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]scope[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]{[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]|[color=rgb(0, 111, 224) !important] [color=rgb(214, 157, 133) !important]"."[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]}. d0 m+ O) c0 {  e/ f5 M% ^/ `; g9 Z

9 H% Y  {0 X" J

0 ?/ G# S/ X" h

* W7 y7 v# p7 z: m0 _/ `

启用统计报告并限定报告的区段,不能用于“frontend”区段。当指定此语句时,统计报告将仅显示其列举出区段的报告信息,所有其它区段的信息将被隐藏。如果需要显示多个区段的统计报告,此语句可以定义多次。需要注意的是,区段名称检测仅仅是以字符串比较的方式进行,它不会真检测指定的区段是否真正存在。

<name>:可以是一个“listen”、“frontend”或“backend”区段的名称,而“.”则表示stats scope语句所定义的当前区段。

尽管“stats scope”一条就能够启用统计报告,但还是建议设定其它所有的参数,以免其依赖于默认设定而带来非期后果。下面是一个配置案例。


* c# T- ^, Y* }. k9 Q' L
1

" T* f- N" u, @
2
: m0 C6 t/ S+ G9 }* o  I: @4 F# Y4 c. o
3

  [! B7 H' ]8 A4 g! A, \
4
) y% g$ r+ q. S: x2 Q

' o- {3 Q/ I7 J+ r  }, s9 b/ N" j/ {
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]private_monitoring
8 f! V) E; e/ J/ a3 x7 W[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable
& |) p8 ]" i* }9 }1 i+ c[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]uri[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxyadmin[color=rgb(216, 216, 216) !important]?[color=rgb(255, 128, 0) !important]stats0 f* b' t# w% D$ k0 T& @0 k. D
[color=rgb(255, 128, 0) !important]stats [color=rgb(220, 220, 220) !important]refresh[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s
; X6 s  O! E3 B, Q1 k8 N* }* O) g% l

' H2 a) j' s1 S5 F8 n; K4 L( V
7 N8 Z" L$ I: e- x& {9 V
; g. y$ Y1 o0 h* {
3.15 stats auth+ e6 H2 h" f) p# o- X$ c9 m

7 q: I$ M- s& F- z
1

% T  U' F+ o6 i
7 ]2 y4 t7 ~  b4 O: r6 t
[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]auth[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]user[color=rgb(218, 218, 218) !important]>[color=rgb(218, 218, 218) !important]:[color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]passwd[color=rgb(218, 218, 218) !important]>
6 c1 ]3 e) q  n% E( n! v4 |( t3 R/ g* c4 L* S( k5 L
1 W: ^; m/ B- S
2 o& P5 G( R% F0 T9 \' `

启用带认证的统计报告功能并授权一个用户帐号,其不能用于“frontend”区段。

<user>:授权进行访问的用户名;

<passwd>:此用户的访问密码,明文格式;

此语句将基于默认设定启用统计报告功能,并仅允许其定义的用户访问,其也可以定义多次以授权多个用户帐号。可以结合“stats realm”参数在提示用户认证时给出一个领域说明信息。在使用非法用户访问统计功能时,其将会响应一个“401 Forbidden”页面。其认证方式为HTTP Basic认证,密码传输会以明文方式进行,因此,配置文件中也使用明文方式存储以说明其非保密信息故此不能相同于其它关键性帐号的密码。

尽管“stats auth”一条就能够启用统计报告,但还是建议设定其它所有的参数,以免其依赖于默认设定而带来非期后果。

3.16 stats admin
. f  Y4 _* z; Q) s6 ~; L8 B- H
8 w3 B. @. m9 H1 V' X) i
1
6 j7 n2 w7 j5 [: a( c& }, c

! m) ~# B/ w! }# q, K1 E9 ?/ p
[color=rgb(255, 128, 0) !important]stats[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]admin[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]{[color=rgb(0, 111, 224) !important] [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]|[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]unless[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]}[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]cond[color=rgb(218, 218, 218) !important]># W" v, c& y+ ?9 ?
$ J, v# z, _0 ^# ~+ g% {
# E- J5 V- ^- |; J1 Y

5 P( E3 }% K( g8 D& r* l

在指定的条件满足时启用统计报告页面的管理级别功能,它允许通过web接口启用或禁用服务器,不过,基于安全的角度考虑,统计报告页面应该尽可能为只读的。此外,如果启用了HAProxy的多进程模式,启用此管理级别将有可能导致异常行为。

目前来说,POST请求方法被限制于仅能使用缓冲区减去保留部分之外的空间,因此,服务器列表不能过长,否则,此请求将无法正常工作。因此,建议一次仅调整少数几个服务器。下面是两个案例,第一个限制了仅能在本机打开报告页面时启用管理级别功能,第二个定义了仅允许通过认证的用户使用管理级别功能。

. P: R/ R% p+ x- a" A; A; X  Y
1

5 c% w# U- b* e: b! ?
2
, C9 j: M" T  t- f3 c# K
3

& {) A8 i. `  Z1 ~6 z
4
5 u0 j4 C; l4 l; x. p
5
2 ^$ P6 F- z' R
6
- J9 ]7 c& c3 [8 {8 f" N0 r6 ?
7

6 D) C; Y( S5 U- V  S
6 f  D, r+ ?: i3 w: w* B- k+ ]$ j
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]stats_localhost
3 Z+ C. h9 a: e3 a! ^  P& k" i[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable
: F  W( `) F% o[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]admin [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]LOCALHOST
9 p8 {4 l9 H, K  k9 |[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]stats_auth" M9 k+ ^) c& w6 ]; u
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable8 i( e% T; g. K; p" ^( H9 P3 Q
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]auth [color=rgb(189, 183, 107) !important]haproxyadmin[color=rgb(218, 218, 218) !important]:[color=rgb(255, 128, 0) !important]password
( R. b% \+ D! J: Y[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]admin [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(244, 187, 21) !important]TRUE
* x# R7 j: ]% N* e2 P6 E/ P6 T+ G' X
, A( U/ q: X2 W0 }+ e
, |8 d: w% @9 _
9 M* U" J* n+ W+ a
3.17 option httplog
- _: m* s; `0 o9 E: U) w+ K$ y: M; a6 r( j* r5 C, }! I4 Z
1

% Z& v# G7 D& ]* N+ B
  P. K6 s' B2 \/ F: m8 a$ A: [
[color=rgb(255, 128, 0) !important]option [color=rgb(220, 220, 220) !important]httplog[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]clf[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]) h' t+ e( T: g1 A1 k: Z

0 j+ a; a, I6 r9 G3 ?
; Y) t+ a4 R; w& m- ]1 V# f( c

+ K0 |/ ^7 [2 t

启用记录HTTP请求、会话状态和计时器的功能。

clf:使用CLF格式来代替HAProxy默认的HTTP格式,通常在使用仅支持CLF格式的特定日志分析器时才需要使用此格式。

默认情况下,日志输入格式非常简陋,因为其仅包括源地址、目标地址和实例名称,而“option httplog”参数将会使得日志格式变得丰富许多,其通常包括但不限于HTTP请求、连接计时器、会话状态、连接数、捕获的首部及cookie、“frontend”、“backend”及服务器名称,当然也包括源地址和端口号等。

3.18 option logasap
$ d5 M! }5 w% f1 m1 A! q' O4 k5 [
, S- f! f3 V( _/ }/ |- a3 t5 g7 p  m
1
7 t4 }. [4 P9 l1 i/ J0 c
2
. j9 h/ ?7 E2 D( {8 l1 c  q% X

: G. d- O. x! D% e) u
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]logasap' g! S7 _- P' H0 S9 I. J0 \0 [
[color=rgb(255, 128, 0) !important]no [color=rgb(255, 128, 0) !important]option [color=rgb(189, 183, 107) !important]logasap2 B5 _) _2 F" @/ @2 t

1 S9 T: i# D. `

: r  H( E! g* I5 o

2 e% y" ^. h6 [  s

启用或禁用提前将HTTP请求记入日志,不能用于“backend”区段。

默认情况下,HTTP请求是在请求结束时进行记录以便能将其整体传输时长和字节数记入日志,由此,传较大的对象时,其记入日志的时长可能会略有延迟。“option logasap”参数能够在服务器发送complete首部时即时记录日志,只不过,此时将不记录整体传输时长和字节数。此情形下,捕获“Content-Length”响应首部来记录传输的字节数是一个较好选择。下面是一个例子。


; G0 G; Y  a4 z2 F
1
; d/ D4 u1 @# x# n5 Z
2

" K% L, N6 f; X2 y( z7 K: k7 ~# c$ [7 n
3
, e  C# V+ Y- @% B
4
+ z4 r3 I  D! m
5
' i* o2 t8 Z3 T( v

+ B5 E. ^2 {. L% l+ w0 o: o% e7 Q' A
[color=rgb(255, 128, 0) !important]listen [color=rgb(189, 183, 107) !important]http[color=rgb(216, 216, 216) !important]_proxy[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]0.0.0.0[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80
" f+ N- I1 v8 J6 f; n* y. b[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http
1 _8 D6 x( g9 Q( x[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httplog
  |, z. [* B. Z5 \[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]logasap
! q% h( J6 E2 S! K0 b( v- y% i7 ]) z[color=rgb(220, 220, 220) !important]log[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.9[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]local2
4 y' J. |: V7 Y" I# P0 x
6 L" F& M3 I, g& }8 y; ?) D: i  p. K
; Z: C2 P; O- L3 x

6 b" U5 t" F" ?  S. q& Z+ B1 F4 v2 J
3.19 option forwardfor
7 S  ^/ P4 x# p3 r1 t! W# {( d/ a4 q
1
) P. h! V3 N' N+ ]" ?$ g" d1 G+ P! k
7 |( @% C4 }6 H! a+ {
[color=rgb(255, 128, 0) !important]option [color=rgb(220, 220, 220) !important]forwardfor[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]except[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]network[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]header[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]name[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(0, 111, 224) !important] [color=rgb(32, 176, 218) !important]if[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]none[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important]
# ~3 {+ p1 T/ T& ?3 a& P4 U/ B6 w. N5 A3 A& @% k0 k: c( ?3 b

) z" _. ?2 z( i+ K
7 r% J5 e0 U/ q+ ]/ a

允许在发往服务器的请求首部中插入“X-Forwarded-For”首部。

<network>:可选参数,当指定时,源地址为匹配至此网络中的请求都禁用此功能。

<name>:可选参数,可使用一个自定义的首部,如“X-Client”来替代“X-Forwarded-For”。有些独特的web服务器的确需要用于一个独特的首部。

if-none:仅在此首部不存在时才将其添加至请求报文问道中。

HAProxy工作于反向代理模式,其发往服务器的请求中的客户端IP均为HAProxy主机的地址而非真正客户端的地址,这会使得服务器端的日志信息记录不了真正的请求来源,“X-Forwarded-For”首部则可用于解决此问题。HAProxy可以向每个发往服务器的请求上添加此首部,并以客户端IP为其value。

需要注意的是,HAProxy工作于隧道模式,其仅检查每一个连接的第一个请求,因此,仅第一个请求报文被附加此首部。如果想为每一个请求都附加此首部,请确保同时使用了“option httpclose”、“option forceclose”和“option http-server-close”几个option。

下面是一个例子。


, P$ z  l6 U" N0 b/ _7 ]* [9 `
1
/ B. L9 [3 E5 e1 [1 n4 q% q8 r8 a: F7 _
2

" u' C% h* F! I* E% z" j$ k: p- k
3

2 k. c; x) k+ u4 ^7 D$ p8 i) N/ C
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]www
. m$ ?+ ]( A5 g# g[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http
$ [' D1 x8 v% i1 M% F8 D* R) v[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor [color=rgb(220, 220, 220) !important]except[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.1+ ]' A, t( `: D9 u& F! f  k1 s
2 V% n! t8 y7 |6 U' l8 K
9 Q( j0 y& [8 N7 g

9 A1 w' x. r9 ]* \7 E* j
+ f8 E" J; b2 r2 ?/ _6 A% H( O3.20 errorfile
9 V" t5 P& F! p+ Z* \8 d" f' i0 {% T! l( V' t4 g; {% k' ~
1
- H+ I5 r9 S' E4 Z- ^" _) v% D

; ]& m9 I: T$ d7 ]6 Q% C. @( K2 ~
[color=rgb(189, 183, 107) !important]errorfile[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]code[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]file[color=rgb(218, 218, 218) !important]>
" R1 _( H$ z' O" }
( G7 @9 ]2 |; i

0 ~' j! w' V7 s1 u- P2 |1 _8 ?8 q
7 B, v; X7 M. W. D6 q7 E% h8 L

在用户请求不存在的页面时,返回一个页面文件给客户端而非由haproxy生成的错误代码;可用于所有段中。

<code>:指定对HTTP的哪些状态码返回指定的页面;这里可用的状态码有200、400、403、408、500、502、503和504;

<file>:指定用于响应的页面文件;

例如:

* w7 p( u' V+ Q/ Q
1
5 q2 h' A: G6 ^! L
2

8 }; j  d7 Y! R2 ]* W9 I1 x
3

  Z) t9 L& t" `! j. R% F) a2 G8 z+ |4 c& i+ `7 }
[color=rgb(220, 220, 220) !important]errorfile[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]400[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]errorpages[color=rgb(218, 218, 218) !important]/[color=rgb(231, 163, 122) !important]400badreq.http3 t  c: Y' v4 \) k8 T: T* g
[color=rgb(220, 220, 220) !important]errorfile[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]403[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]errorpages[color=rgb(218, 218, 218) !important]/[color=rgb(231, 163, 122) !important]403forbid.http
7 l" a& G$ l6 l- {5 H; ?' U[color=rgb(220, 220, 220) !important]errorfile[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]503[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]errorpages[color=rgb(218, 218, 218) !important]/[color=rgb(231, 163, 122) !important]503sorry.http% ^; _0 n( A% B' p
' `4 U7 w% d' y/ [2 X" K

1 H$ C7 L* h3 ?6 @
2 {) ?; E7 y- q2 W

5 s9 P. \* m( y) f9 ~3.21 errorloc 和 errorloc302
' g! [" R! ]' m+ y
! F5 B3 ]" b$ ~' p/ l" e
1
) F, y7 {- S! H/ O2 q
2

8 Q* X& j; O. F2 r: q
2 v' R3 h6 b+ ]) r6 }
[color=rgb(189, 183, 107) !important]errorloc[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]code[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]url[color=rgb(218, 218, 218) !important]>5 i6 k0 q2 }* S4 i
[color=rgb(189, 183, 107) !important]errorloc302[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]code[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]url[color=rgb(218, 218, 218) !important]>3 _" [( s7 P1 z  X% m
; v! y8 m: p' c+ m& y/ v" z2 P

' H& ]1 G% W5 y( M2 s) P

" f6 D" |: G* A' e% i3 I1 t' ]

请求错误时,返回一个HTTP重定向至某URL的信息;可用于所有配置段中。

<code>:指定对HTTP的哪些状态码返回指定的页面;这里可用的状态码有200、400、403、408、500、502、503和504;

<url>:Location首部中指定的页面位置的具体路径,可以是在当前服务器上的页面的相对路径,也可以使用绝对路径;需要注意的是,如果URI自身错误时产生某特定状态码信息的话,有可能会导致循环定向;

需要留意的是,这两个关键字都会返回302状态吗,这将使得客户端使用同样的HTTP方法获取指定的URL,对于非GET法的场景(如POST)来说会产生问题,因为返回客户的URL是不允许使用GET以外的其它方法的。如果的确有这种问题,可以使用errorloc303来返回303状态码给客户端。

3.22 errorloc303# A4 A8 O; b- X2 d

6 P; s. E0 {" T( c% J, V3 [
1
4 A2 p+ t- Z7 N# E
1 v2 _' u/ H& ]; P) t+ x
[color=rgb(189, 183, 107) !important]errorloc303[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]code[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]url[color=rgb(218, 218, 218) !important]>$ a- e' z, E4 z3 T* \  }

! t- F5 A3 l0 x! ^5 C

0 t2 A) D3 G# X( F
% q  o% w  y! S1 ^& T9 \

请求错误时,返回一个HTTP重定向至某URL的信息给客户端;可用于所有配置段中。

<code>:指定对HTTP的哪些状态码返回指定的页面;这里可用的状态码有400、403、408、500、502、503和504;

<url>:Location首部中指定的页面位置的具体路径,可以是在当前服务器上的页面的相对路径,也可以使用绝对路径;需要注意的是,如果URI自身错误时产生某特定状态码信息的话,有可能会导致循环定向;

例如:


; M7 w' G0 ^( Y/ l. ~  L" h
1
- I- m" r. D5 J
2

4 b& q0 q" t: K  O9 E
3
. t6 R) A# X7 t5 A/ V6 S, J/ M
4

& m" f6 x3 i5 N3 C1 I4 F! U
5

# x6 z! e. P$ N' X5 B# d! H4 e: e+ g& R' p* o/ W7 Q
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]webserver3 h, B; A, j5 u" g+ H$ ]
[color=rgb(220, 220, 220) !important]server[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.6[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.6[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3000[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]cookie [color=rgb(255, 128, 0) !important]srv01. y( f) @, k) r8 ^$ R
[color=rgb(220, 220, 220) !important]server[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.7[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]172.16.100.7[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3000[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]cookie [color=rgb(255, 128, 0) !important]srv02
& H3 ?% H  T- ?$ O3 _$ O; J[color=rgb(220, 220, 220) !important]errorloc[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]403[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]errorpages[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]sorry[color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]htm
* u& m& e# Q8 I6 i% z[color=rgb(220, 220, 220) !important]errorloc[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]503[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]etc[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]errorpages[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]sorry[color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]htm/ r% P( x$ t5 W

% |& W2 r1 M% m9 X
3 x+ \9 {$ R8 f# n
, M+ P7 Z0 X- U& ]3 O

3 U/ @0 n* z- I9 Z3 P. Q四、ACL

haproxy的ACL用于实现基于请求报文的首部、响应报文的内容或其它的环境状态信息来做出转发决策,这大大增强了其配置弹性。其配置法则通常分为两步,首先去定义ACL,即定义一个测试条件,而后在条件得到满足时执行某特定的动作,如阻止请求或转发至某特定的后端。定义ACL的语法格式如下。

; X" B/ e3 {- k* H. B7 b1 ?' L! h! T+ K2 S
1
9 f; M6 C; m- M8 k; Z0 k; c
  E+ O6 ^; \6 f" l
[color=rgb(189, 183, 107) !important]acl[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]aclname[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]criterion[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]flags[color=rgb(216, 216, 216) !important][color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]operator[color=rgb(216, 216, 216) !important][color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(189, 183, 107) !important]value[color=rgb(218, 218, 218) !important]>[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(216, 216, 216) !important].[color=rgb(216, 216, 216) !important].
2 U# K- t* r3 Y+ S  O4 T" D1 M& Q) O6 m

- |0 h4 M/ J; n" z  B- t! E
4 \9 j9 g! }$ K+ G5 j

<aclname>:ACL名称,区分字符大小写,且其只能包含大小写字母、数字、-(连接线)、_(下划线)、.(点号)和:(冒号);haproxy中,acl可以重名,这可以把多个测试条件定义为一个共同的acl;

<criterion>:测试标准,即对什么信息发起测试;测试方式可以由[flags]指定的标志进行调整;而有些测试标准也可以需要为其在之前指定一个操作符[operator];

[flags]:目前haproxy的acl支持的标志位有3个:

-i:不区分中模式字符的大小写;

-f:从指定的文件中加载模式;

--:标志符的强制结束标记,在模式中的字符串像标记符时使用;

<value>:acl测试条件支持的值有以下四类:

整数或整数范围:如1024:65535表示从1024至65535;仅支持使用正整数(如果出现类似小数的标识,其为通常为版本测试),且支持使用的操作符有5个,分别为eq、ge、gt、le和lt;

字符串:支持使用“-i”以忽略字符大小写,支持使用“\”进行转义;如果在模式首部出现了-i,可以在其之前使用“–”标志位;

正则表达式:其机制类同字符串匹配;

IP地址及网络地址;

同一个acl中可以指定多个测试条件,这些测试条件需要由逻辑操作符指定其关系。条件间的组合测试关系有三种:“与”(默认即为与操作)、“或”(使用“||”操作符)以及“非”(使用“!”操作符)。

5.1 常用的测试标准(criteria)

5.1.1 be_sess_rate

6 ]  \$ v: v( x5 ^1 i
1
: n/ D8 v& {* Q! f/ k, o9 l

) H, f- f. ]- @# {: z  e
[color=rgb(255, 128, 0) !important]be_sess_rate[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]backend[color=rgb(216, 216, 216) !important])[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(244, 187, 21) !important]integer[color=rgb(218, 218, 218) !important]>  C3 F' j% V# a2 d

; t( B1 e$ r. a( v

8 a% @& m. k$ {: E! b) U" v: o( [
" T: G; p' r. d) j2 l* {7 E

用于测试指定的backend上会话创建的速率(即每秒创建的会话数)是否满足指定的条件;常用于在指定backend上的会话速率过高时将用户请求转发至另外的backend,或用于阻止攻击行为。例如:


$ Y  i$ k0 }6 W  l
1

2 X: _8 B6 @  j& w
2
6 [# ~+ t  @" _& X9 p$ I: N
3
2 S  W0 z. N) w7 L) }1 G( h: i
4

" `; _  c* C, l1 {+ G" Q$ K5 M: ^' ~& R
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]dynamic# V9 H) s) C* q
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http6 I$ @( j* d- q2 R
[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]being_scanned [color=rgb(255, 128, 0) !important]be_sess_rate [color=rgb(220, 220, 220) !important]gt[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]50
) R& j; C6 R3 A- |, ?3 w# D! C[color=rgb(255, 128, 0) !important]redirect [color=rgb(189, 183, 107) !important]location[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]error_pages[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]denied[color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]html [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]being_scanned
. @* D8 ~6 k; s* @9 q6 l3 M+ Q2 h6 d7 F8 Z( F: v

  }/ h7 T0 L; T, M% m6 c
- a4 Y3 ^! G7 t8 f

5.1.2 fe_sess_rate


, e% [4 O/ D! q7 H' Q9 a
1
: B! C# W0 S- i' t. i' c% l! @

' w* r3 g; \: [* l# b, F
[color=rgb(255, 128, 0) !important]fe_sess_rate[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]frontend[color=rgb(216, 216, 216) !important])[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(244, 187, 21) !important]integer[color=rgb(218, 218, 218) !important]>
4 E) v  d& V, _4 d9 X. `$ P9 Q: X$ _( X/ W7 m8 z

! Z0 {8 Z  o) |  `7 X0 r/ g
. t. B  k2 W' h

用于测试指定的frontend(或当前frontend)上的会话创建速率是否满足指定的条件;常用于为frontend指定一个合理的会话创建速率的上限以防止服务被滥用。例如下面的例子限定入站邮件速率不能大于50封/秒,所有在此指定范围之外的请求都将被延时50毫秒。


8 _, ?* v$ Y4 s6 \6 Y7 G3 t
1
& ~' X+ q2 J  b. |; I) M% ]
2
9 c$ v% W6 I& U9 u" }
3
% @7 @* f$ q, u! A, P# I1 n- ~
4
2 {# `, B! t# p+ p' Q' E/ ~# h4 W/ ]
5
  y7 Q! H% [  W, g5 _3 ~, I5 M0 G
6
2 |* }0 b$ |! \- o1 e9 X
7

/ W+ @1 L; r4 X: t
8
0 g" t, l+ J" }9 j" v; i% x

# h: M8 l2 X% n7 m! Z
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]mail
! E+ g$ Q3 p) s[color=rgb(189, 183, 107) !important]bind[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]25. B' Q, M3 K' }7 Y$ f$ G
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]tcp
  r# ^( D' N: }+ x  H[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]500' H& @  m3 H0 B) v/ S
[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]too_fast [color=rgb(255, 128, 0) !important]fe_sess_rate [color=rgb(220, 220, 220) !important]ge[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]506 C7 e: S& |" k/ ]1 C
[color=rgb(189, 183, 107) !important]tcp[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]request [color=rgb(189, 183, 107) !important]inspect[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]delay[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]50ms8 Y, g4 a/ @# o0 N! Y
[color=rgb(189, 183, 107) !important]tcp[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]request [color=rgb(255, 128, 0) !important]content [color=rgb(255, 128, 0) !important]accept [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]![color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]too_fast& U' Z/ l* e, Y4 W- D, v! z. f
[color=rgb(189, 183, 107) !important]tcp[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]request [color=rgb(255, 128, 0) !important]content [color=rgb(255, 128, 0) !important]accept [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]WAIT_END
, y; K1 a  O( y8 H! w- p& C  i" {8 a( q3 {
1 K9 L) b5 Q) T2 V
0 r9 Z9 Z9 C0 X' c' u4 R. x  a

5.1.3 hdr <string>


/ \% n  Z0 a: d7 p0 p+ v5 }/ j
1

' s3 f" N2 T3 i$ A. F- q; G9 S
9 V" C' {; X2 x9 ^! s2 f
[color=rgb(255, 128, 0) !important]hdr[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]header[color=rgb(216, 216, 216) !important])[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(244, 187, 21) !important]string[color=rgb(218, 218, 218) !important]>) X' U/ P; d$ b" H
0 O3 j' z3 f" D% f
' w: C: i6 r' V* y9 f" s

/ t  J& I+ Z) }9 \5 G! C

用于测试请求报文中的所有首部或指定首部是否满足指定的条件;指定首部时,其名称不区分大小写,且在括号“()”中不能有任何多余的空白字符。测试服务器端的响应报文时可以使用shdr()。例如下面的例子用于测试首部Connection的值是否为close。

! d* b/ f9 s0 P' K
1

/ ^6 _& [) O. D9 }9 K7 ]8 r8 I  B+ g& y7 x
[color=rgb(255, 128, 0) !important]hdr[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]Connection[color=rgb(216, 216, 216) !important])[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]i[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]close
% r; R4 Z" H9 m  t8 y2 V: W9 U3 O5 F+ ?5 G; ~

4 L+ l; W$ l% L- i7 g5 L

5 @3 M  f- d( X! ^! Y; I

5.1.4 method <string>

. V, B9 }" w7 i7 b7 @7 w
1
/ Y# K, S" c" h, G. o

# B1 T2 Y& i# m3 ~  `3 B. y8 ]
[color=rgb(189, 183, 107) !important]method[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]<[color=rgb(244, 187, 21) !important]string[color=rgb(218, 218, 218) !important]>
4 x7 o! N; T2 p( n3 `/ E! O
9 J* ^* J2 l# l) `, V( L

$ X) q9 Y! t8 b# o
+ V& Z! ^8 c$ v- W5 q+ a

测试HTTP请求报文中使用的方法。

5.1.5 path_beg <string>

用于测试请求的URL是否以指定的模式开头。下面的例子用于测试URL是否以/static、/images、/javascript或/stylesheets头。

5 `+ O* l/ ?9 B
1
/ U, T9 U) R: B$ J; P

: O: R+ Q9 C- ?! O. P3 e& Y% V
[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]url_static [color=rgb(189, 183, 107) !important]path_beg[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]i[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(86, 156, 214) !important]static[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]images[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]javascript[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]stylesheets
. K3 W. t  X" U; F; y0 b4 a3 ^
- `( q0 T( ]% P" h$ M* I' T7 F

$ C/ d' c  c6 t/ S* C9 ?" A

! [- |( Z( p  @9 Y/ i' J$ L

5.1.6 path_end <string>

用于测试请求的URL是否以<string>指定的模式结尾。例如,下面的例子用户测试URL是否以jpg、gif、png、css或js结尾。


, f3 |9 J3 M. O, K6 d% M/ c$ s
1
* Y/ v9 g  {& h* D7 H

# h) u/ Q2 W% W  X6 s* f
[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]url_static [color=rgb(189, 183, 107) !important]path_end[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]i[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]jpg[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]gif[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]png[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]css[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(189, 183, 107) !important]js
; r- T, w; q7 ^; z# K) ^2 J1 M, i& G% d& J+ G# T4 b

' b' r4 \4 C. t7 f
. p0 M$ n7 C# n( {' j; r" v! a

5.1.7 hdr_beg <string>

用于测试请求报文的指定首部的开头部分是否符合<string>指定的模式。例如,下面的例子用记测试请求是否为提供静态内容的主机img、video、download或ftp。


" M9 V+ j4 x3 |. V, M
1

0 V' r6 r! o* _4 w' f& B- n" Z" C5 b8 P( X8 ]2 ~
[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]host_static [color=rgb(255, 128, 0) !important]hdr_beg[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]host[color=rgb(216, 216, 216) !important])[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]i[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]img[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]video[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]download[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]ftp[color=rgb(216, 216, 216) !important].
9 h7 V& q# I2 c& e6 ^6 q
1 h$ D5 A! s4 k0 p2 e
5 P6 z5 H9 v9 I# F2 k

8 }/ O8 Q  v) ?5 P& z

5.1.8 hdr_end <string>

用于测试请求报文的指定首部的结尾部分是否符合<string>指定的模式。

五、配置案例

前端调度器IP:192.168.1.2102 U' ?: D# V1 N+ k* q3 `7 I5 L; i
后端应用服务器IP: 192.168.1.111 和 192.168.1.112

定义独立日志文件9 F3 r( K6 L, k9 c: b  K3 C

2 u, i9 B6 a( U
1
7 u  q9 {$ s, s6 s
2
, m4 r7 a; ]4 J1 l: n6 X6 f3 |" i
3
1 n6 h* _1 e- R0 j
4

: t# d8 @$ T+ `. _6 s% e- U
5
8 q3 `, l9 T2 O$ X6 d3 a3 {0 ]: W
6
6 s% A7 m+ s+ V/ p' q! |! F
7

2 ], H+ v, S( w2 Q; k% o9 r
8
% j, L  m9 M6 u. \5 W
9
9 z( t6 X# k- C- p- ^
10
# ^# l4 h8 g/ G  ~# O
11
# ], ?* s3 Z( G& n; s; p
12

2 U) z7 A4 o0 I1 l5 F. M
0 Z! V  k# |! L+ k: b# E
[color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]root[color=rgb(216, 216, 216) !important]@[color=rgb(255, 128, 0) !important]node1 [color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important][color=rgb(155, 155, 139) !important]# vim /etc/rsyslog.conf #为其添加日志功能
3 L9 \$ C  S, k( n# a( [# {3 m[color=rgb(155, 155, 139) !important]# Provides UDP syslog reception
% V8 |9 ?- f8 Q. H4 O9 e[color=rgb(216, 216, 216) !important]$[color=rgb(255, 128, 0) !important]ModLoad [color=rgb(220, 220, 220) !important]imudp: C% ~# `; W2 H
[color=rgb(216, 216, 216) !important]$[color=rgb(220, 220, 220) !important]UDPServerRun[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]514[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]>启动[color=rgb(220, 220, 220) !important]udp,启动端口后将作为服务器工作
  l+ d3 m4 [, l* H; ?[color=rgb(155, 155, 139) !important]# Provides TCP syslog reception& k1 J! E/ [# E
[color=rgb(216, 216, 216) !important]$[color=rgb(255, 128, 0) !important]ModLoad [color=rgb(220, 220, 220) !important]imtcp
* n/ s5 k/ D- }# h* @3 |) _& Q[color=rgb(216, 216, 216) !important]$[color=rgb(220, 220, 220) !important]InputTCPServerRun[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]514[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]>启动[color=rgb(220, 220, 220) !important]tcp监听端口
- |4 w8 w; S. }  B7 o. A8 H[color=rgb(189, 183, 107) !important]local2[color=rgb(216, 216, 216) !important].[color=rgb(218, 218, 218) !important]*[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(244, 187, 21) !important]var[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]log[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]log1 P# W4 p3 E" g2 S( `% k  j
, H4 z$ U! b: _- `; r% H; P+ d
[color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]root[color=rgb(216, 216, 216) !important]@[color=rgb(255, 128, 0) !important]node1 [color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important][color=rgb(155, 155, 139) !important]# service rsyslog restar
- o; \& c0 _$ |  _( z+ A[color=rgb(216, 216, 216) !important][[color=rgb(189, 183, 107) !important]root[color=rgb(216, 216, 216) !important]@[color=rgb(255, 128, 0) !important]LB [color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important][color=rgb(155, 155, 139) !important]# vim haproxy.cfg
- h( @- d! a7 W" |( E: n( c0 [[color=rgb(220, 220, 220) !important]log[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.1[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]local2[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]--[color=rgb(218, 218, 218) !important]->在[color=rgb(86, 156, 214) !important]global端中添加此行
4 q3 S8 W' ?7 h6 z3 {0 W" B2 F7 m) c+ o3 Q- P$ ?

) b# K0 Z: O% c+ B. U- F

6 K7 i* [6 C2 F; b  q) @
9 u, ]! ~# t" |- X$ v一个最简单的http服务的配置
, o& v$ `, E8 J6 Z$ i
1 X' o  Q/ z, ?  U0 a. O
1
  N5 d0 G# p% J" B4 M
2
( g/ v! s  D( v) L5 T, {
3
3 ~* @( b3 [3 T. a+ m
4

$ h: K0 G! `2 B' G
5
3 ?' j1 E1 u3 M/ G# E
6

5 L6 Q& N% m/ h4 N
7
, s) `) K- Z% Y& c" ~1 O
8
0 A( I8 d  Z' }% G6 a
9
3 v2 g3 y* ?: y+ |" o+ b8 Z, d$ ?
10
2 Y$ K7 s2 E! p. d! \
11
- r: D$ K3 n" |5 n  h) |8 y
12

- ]; b% }5 B6 B# @* ?2 U8 H3 U
13

7 x7 E, X8 }* |! @
14

$ n0 i/ p5 T" r! e! h6 C
15
* c- X3 U( _* n( t1 Y( B% e7 U4 E
16

1 h. c6 V1 |* F7 @, a
17

0 Y, s4 P1 n8 H+ v  f2 B% c2 K
18
/ F# b/ p1 z/ L' J8 t+ ]$ u+ Z( E
19

4 Y2 _) B" M7 P8 j) Z  w( x
20
0 k5 l2 o* G& B; Q  b+ y) [' \
21

" [6 @" b; E9 S( a: q, }' f
22

# @6 m6 `  Q1 G2 N
23

1 m. f+ t6 A0 w% _0 t/ G
24
& J. t3 b& T$ a! K
25

, M. W7 \+ B9 x! o! ~* K% p6 x
26

! H( M/ s# G, B5 k
27

* |4 f8 j, S% l$ U. B: e5 F5 V
28

( x2 M* M9 s2 h6 y1 l4 b
29

7 I: h  K8 T! |+ T6 N/ ~
30
2 d/ `* S* T3 L/ G6 f+ F& ^( j: F5 w
31

; f/ ^' Z7 w5 i4 o
32

6 s+ t/ ^, r/ q% }5 B
33
' O: S' T$ S% A9 x2 V2 @
34

, r, s. K/ ^9 F; ~4 Y5 i# W7 X" c  c' V
[color=rgb(86, 156, 214) !important]global
6 P( V1 u- C$ _4 N[color=rgb(220, 220, 220) !important]log[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.1[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]local2
& H; F1 C6 U6 e: X[color=rgb(189, 183, 107) !important]chroot[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(244, 187, 21) !important]var[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]lib[color=rgb(218, 218, 218) !important]/[color=rgb(255, 128, 0) !important]haproxy" ^: ^$ L& ]; e/ k  q" s% x- H
[color=rgb(189, 183, 107) !important]pidfile[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(244, 187, 21) !important]var[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]run[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]pid) E& ]7 R+ t4 [: _. L' D$ ?4 A" C8 v
[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]4000
+ T% t; l$ ~3 c/ Y. D[color=rgb(255, 128, 0) !important]user [color=rgb(255, 128, 0) !important]haproxy8 w( Y' u" L$ J. ]3 R
[color=rgb(255, 128, 0) !important]group [color=rgb(255, 128, 0) !important]haproxy
3 X% ?' F  {* G( o& ]4 c3 E[color=rgb(255, 128, 0) !important]daemon
9 {) X1 D2 n4 e  B" W( d+ l# G9 U# ?[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]socket[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(244, 187, 21) !important]var[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]lib[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(218, 218, 218) !important]/[color=rgb(255, 128, 0) !important]stats
/ H* x5 V5 A  x6 v4 v[color=rgb(255, 128, 0) !important]defaults6 {+ W( y) t+ O9 ~
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http+ \! o6 w4 j* i; I3 `- j
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global& w" N# N5 Q% U" P9 h* I9 e
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httplog
6 P. `: ~3 o$ v$ z% R" z  \. O[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]dontlognull' J& g0 T% r# d$ ]" |5 E  [- w1 L
[color=rgb(255, 128, 0) !important]option [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]server[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]close
9 j% S# j% q8 E[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor [color=rgb(220, 220, 220) !important]except[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.0[color=rgb(218, 218, 218) !important]/[color=rgb(231, 163, 122) !important]86 ]9 s! ?6 V( D
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]redispatch# z) F. @* h8 }0 A- p- Z7 P# u! O2 I$ V
[color=rgb(220, 220, 220) !important]retries[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]34 \" m% f; q- b5 v! M' K' k4 j
[color=rgb(255, 128, 0) !important]timeout [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]request[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s; X% P4 T$ u" [3 N9 K
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]queue[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m) F, e, l: B% ]  h+ `
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]connect[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s
% q7 ]- k- h$ D3 Y4 _; S[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]client[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m
1 }  b: B3 t) F! h1 J- w[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]server[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m
8 w, g7 ?2 {5 M  j" Y6 l) H[color=rgb(255, 128, 0) !important]timeout [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]keep[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]alive[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s8 ^: e# K! ~; j4 ~( F
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]check[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s& y  C: _: [$ v1 i
[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3000$ `0 j3 b. }# X+ p" ^0 n
[color=rgb(255, 128, 0) !important]frontend [color=rgb(189, 183, 107) !important]webser[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]#webser为名称
9 b+ g. ^9 L2 H. ^! l, l7 ?( b[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor
& Q8 g. t) h) A  g6 _[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80" \, `" l% [6 @4 D- M0 b
[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]app. [7 o- B8 y, y- l  q( ?* m- `
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]app9 r4 n: k; G* _! u6 _! E
[color=rgb(255, 128, 0) !important]balance [color=rgb(189, 183, 107) !important]roundrobin[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]#使拥roundrobin 算法& Z0 b4 c7 x/ G
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]app1[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.111[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check
2 h+ T: A+ g8 z0 m[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]app2[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.112[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]check3 J/ g( a( h0 _9 k" Q8 h1 C# e

( P' L9 t4 ~( d  P) ?
8 U' D; m+ l* a( r2 c& ^- w/ U
2 y4 t) a1 N) _  s; o

' h- H+ r9 a  l* q, N! Ghaproxy统计页面的输出机制
0 T' P- O) F. d, P9 K
2 a7 a' b& i" y
1
% k/ p  T$ k/ U& F
2

  F% ]5 \2 M% d) s' f
3
4 f/ \- b) |3 V3 p* V# v
4
0 P+ R8 T. P5 q+ R4 G* O! Q
5
+ B. |8 j# C8 d+ T/ L
6

# J1 o' ^  H" w- \7 i  a$ O
7

7 Y! k' z3 I- B; U
8

, u' e8 A. B8 M) v
9
8 G" k  m# m9 f2 I* o/ q8 j2 N
10

7 P* T- M; I/ p2 I
11

" J  B+ l" B+ R' ?# |5 {2 z+ ]
12
2 H9 n9 b2 C) C! K1 f; e7 p
13
/ J3 @2 R' n4 m
14
3 @0 U, l6 S; t
15
( ]% A" E. L. W2 a( O7 D+ m
16
2 A7 v. h  c) }5 f+ v# K9 }9 F2 b
17
: y  c0 u4 D& Q2 E! }4 c
18
! K- s3 u7 {  I- k; V
19
, }- B: x' W' y& j# Y8 o' a
20

  E0 b0 r: s& R5 c: Y3 w' E. g! o1 v8 ]8 K5 v: J  q+ r1 @9 |
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]webser
* [: B! W1 [5 @: T* A[color=rgb(220, 220, 220) !important]log[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.1[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]local3
3 a* v0 D: r: H[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor/ a9 U( i( D' \& g9 h
[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80
' {5 n1 S* h- g" _; }! \9 j[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]app
6 }$ f  m& L% Q. |[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]app
. n1 q' |7 Y* [& d  m[color=rgb(255, 128, 0) !important]cookie [color=rgb(255, 128, 0) !important]node [color=rgb(255, 128, 0) !important]insert [color=rgb(255, 128, 0) !important]nocache
) _$ u, n; `+ q8 T: a[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]roundrobin
% _; o7 n3 g4 A, K1 b: }[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]app1[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.111[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(255, 128, 0) !important]cookie [color=rgb(255, 128, 0) !important]node1 [color=rgb(220, 220, 220) !important]intval[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]rise[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]fall[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2
6 c) }$ x! x  Y  H0 E% z9 E[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]app2[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.112[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(255, 128, 0) !important]cookie [color=rgb(255, 128, 0) !important]node2 [color=rgb(220, 220, 220) !important]intval[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]rise[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]fall[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]28 R* x- v( P; ]& d$ r. V
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]backup[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.1[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]8010[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(255, 128, 0) !important]backup3 U9 _$ y" d' K4 q% w3 I% h! t
[color=rgb(255, 128, 0) !important]listen [color=rgb(255, 128, 0) !important]statistics
5 ]/ r+ q- ]: g# U* }, ~& k[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]8009[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]# 自定义监听端口
* ?" }( ~3 W% r9 R9 g[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]enable[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]# 启用基于程序编译时默认设置的统计报告
  N3 C1 C6 O& N& h2 K[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]auth [color=rgb(189, 183, 107) !important]admin[color=rgb(218, 218, 218) !important]:[color=rgb(189, 183, 107) !important]admin[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]# 统计页面用户名和密码设置
- d! h) Z, g1 a- t  U[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]uri[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]admin[color=rgb(216, 216, 216) !important]?[color=rgb(189, 183, 107) !important]stats[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]# 自定义统计页面的URL,默认为/haproxy?stats
8 a: S& a- G( w# S& v& \7 E$ r[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]hide[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]version[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]# 隐藏统计页面上HAProxy的版本信息: ^3 H1 @9 v: l2 _( F# H5 c! ^
[color=rgb(255, 128, 0) !important]stats [color=rgb(220, 220, 220) !important]refresh[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]30s[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]# 统计页面自动刷新时间/ X* w$ @7 J. X7 {3 m
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]admin [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(244, 187, 21) !important]TRUE[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]#如果认证通过就做管理功能,可以管理后端的服务器* @# Z; ^2 J5 J- K0 ^
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]realm [color=rgb(189, 183, 107) !important]Hapadmin[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]# 统计页面密码框上提示文本,默认为Haproxy\ Statistics, H6 ^9 y1 m9 [, D/ {) u
# ^, m- F& j( ]: E9 p, r9 H( T

: X/ R8 N' u! i1 T2 Y  P$ f8 E& P- l
0 l" e  ]; u6 R$ n
- b" V. I9 x( h) a% z  Y0 p
动静分离示例:
( c: O3 Y: q* y$ h* m2 e! J3 B* P0 r* J
1

8 D2 j( a- l3 y& n1 V
2

# M; T2 {: h$ m1 a: s% O! ~0 w" i6 m5 }
3

7 r, M# M% S* W$ Q" b$ ^' @
4

& E/ `) i: U$ R8 x, w
5
7 t& [- _+ e" t* X$ K9 V
6

5 s) I! b! @5 B- D' `
7

" f$ t) \  H+ e
8
& d3 h1 e7 \9 }# q; R
9
! W2 F7 N! ?7 _" u
10
% \6 |% T. b. h' A
11

; k8 |3 l2 h( V/ Y9 f
12
# d+ ~3 W$ k0 l9 c# H& U* |
13
* k8 N/ |0 }; [
14
8 \8 p% H/ X+ @& E* _6 N8 ]- n
15

# K5 D$ I) P1 N1 L2 l5 \
8 L2 ?0 |' y9 Q$ p" m+ S# g* }' v- g
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]webservs
) d  i8 Y% F, d6 O[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]801 q% l4 i" s2 i
[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]url_static [color=rgb(189, 183, 107) !important]path_beg[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]i[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(86, 156, 214) !important]static[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]images[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]javascript[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(255, 128, 0) !important]stylesheets
5 o* j$ h- c: A+ i7 A[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]url_static [color=rgb(189, 183, 107) !important]path_end[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]i[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]jpg[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]gif[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]png[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]css[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(220, 220, 220) !important]js[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]html
4 C/ M" `% f% y[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]url_php [color=rgb(189, 183, 107) !important]path_end[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]i[color=rgb(0, 111, 224) !important] [color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]php
+ g. J( f/ E- I2 A% [[color=rgb(255, 128, 0) !important]acl [color=rgb(255, 128, 0) !important]host_static [color=rgb(255, 128, 0) !important]hdr_beg[color=rgb(216, 216, 216) !important]([color=rgb(189, 183, 107) !important]host[color=rgb(216, 216, 216) !important])[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]i[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]img[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]imgs[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]video[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]videos[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]ftp[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]image[color=rgb(216, 216, 216) !important].[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]download[color=rgb(216, 216, 216) !important].
3 x# ~3 @5 Y$ z* w[color=rgb(255, 128, 0) !important]use_backend [color=rgb(86, 156, 214) !important]static[color=rgb(0, 111, 224) !important] [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]url_static [color=rgb(32, 176, 218) !important]or[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]host_static) @! ]5 V; Y  t. g9 ]
[color=rgb(255, 128, 0) !important]use_backend [color=rgb(255, 128, 0) !important]dynamic [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]url_php& [. z; h1 {5 E3 ~2 n3 \
[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]dynamic& ~% y3 Q* y/ \" v; w/ u4 m
[color=rgb(255, 128, 0) !important]backend [color=rgb(86, 156, 214) !important]static  Z# ~, M$ d( s8 ?
[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]roundrobin8 }1 T# E$ y4 x
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]node1[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.111[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3000
. M  [, b/ X: L5 G2 R# @( C[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]dynamic1 P9 v4 @0 E; e# T4 U# u% z- j
[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]roundrobin
; o3 h+ @- I6 S$ h2 H[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]node2[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.112[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1000" T9 K* ~& C  z: y% h
# P! F3 `* `7 q; ?4 [

, I& Z3 j! q  h! ]6 ^
8 m0 n5 z4 t- L) H  ^( }5 ?/ u
. y  V  v) v/ k+ C1 f
http服务器配置完整示例
, Y/ y0 [5 y5 D' |) E3 f8 A1 `, B2 z6 H# C
1
5 U$ q0 U8 u* T
2
6 t6 \5 M, @6 z4 F
3

5 Q4 ]5 V3 K  t1 m
4
0 g& U/ W8 K- X4 w* `3 u  B
5
9 q4 `2 z. N; H5 Q" }8 C' K! u
6
, e& Q- N/ Y! d; I9 A/ _
7

  W9 V; m1 O8 E5 P0 x0 a
8

) V; E! _0 T! q  k8 O4 i4 _
9

+ ~! D+ `( s! l7 n1 \+ K
10

8 k3 D# k3 n9 e5 t' j  t% h
11
, S9 q- v* h5 r
12
6 i. Q; z+ L! F8 r7 Z2 _- ]
13

- x* G* N4 H) v8 j! W$ t
14

4 t. k5 A; T8 P7 C7 U1 l9 A: G
15

, ?: ^$ X5 C( J* E- u/ U# V5 D
16

7 R. G( X6 _8 m/ M
17

& F1 ^5 H  X' j& U0 A  z
18
9 k% b# z1 s: V; K
19

4 _, J9 [! |" X0 A
20

$ M8 ^! _* k% Y0 ?* x- i! e
21
" J4 u0 e7 Q( R3 n0 y, w
22

. b' E  L6 R! p# ]4 e
23
% W/ K7 b) P7 [) |1 @1 \: b( m
24
# `+ I3 o5 x8 W' |. L6 ^4 o
25

0 @6 I% B* F) b! z! o  b
26

. p% j' t) K7 ~
27

- Q; Y. |5 T: R8 b5 q: y
28

- c% A9 b& D: J/ |+ w) k9 l
29
# F, Z- a/ ]# i
30

6 q9 ]4 T$ ?$ o" z8 W2 [0 Q
31

$ B9 n$ f# x- Q$ ]' Z
32

4 ^; g+ D8 }  N, G
33

7 P+ B  O% V) H$ e5 K; ^% Q
34
  O3 O& v* |" p
35
! n' }# i" W7 T* G# v, n8 v
36

$ N" m1 D* Q2 g
37

  D  S* [, G; s7 P
38
& u7 @- Y" R  B. c. n6 A
39

1 j0 M0 N& v) |2 Z, p
40

' Y2 L; Q0 L8 X! l
41

$ X) L- R+ W# Q3 O' F1 [
42

" I. o7 z: c. ^9 B. T5 N8 Q7 e0 d
43
4 A( X  Q% V+ }/ J& g% B
44

' O5 C. X3 e* ]" u% A# M5 O( L! x
45
% u9 m& {5 b, ?2 i& T; H
46

- c3 h5 P3 \# K7 \, W
47

) n' I7 a& N; ^  ^% f
48
0 {5 S$ k% ^& w/ ~  u( e. e
49
, c) x" T2 r6 M, w
50

) k0 K- W9 ?4 G" `
51

" a6 T% c& E2 ~" y# b
52
( |9 v1 ]# l$ p7 [' w
53
' L( D* b, j* G$ _$ k
54
/ E5 i- P7 B) U  X. W
55

1 r$ T! r5 W+ d7 }: c! |+ S
56

4 Z0 _, R  Z$ z" E( v4 }
57

6 ]% H+ I9 W2 s: D0 [5 |) [
58
4 c  e( K1 f8 L1 _
59

% X# l  w( `, {% y. ~7 P9 j
60

# y  S* s% v. h* a* r% j5 _
61

& m% w/ u7 @9 Q/ e9 @( k
62
, g1 G4 k+ ~5 f, J% Q! t, ?
63

/ E: [! O2 i( o5 n
64
7 l# R: _# \4 ^" o
65
9 I: J2 }; n  L
66
" C, X0 M* i, ]- q9 f
67
' ?7 d* v+ }' t. m/ d8 |
68
, g* R$ m3 D4 }- `; E# I) y
69
4 b: o! _) ]  f- k, C; Q
70
1 _+ G7 x- o- r: i$ r4 }3 ]
6 l, p+ Z4 g8 m& U: q* M3 Y/ l
[color=rgb(155, 155, 139) !important]#---------------------------------------------------------------------
) p+ b" X+ T3 X3 F' t[color=rgb(155, 155, 139) !important]# Global settings! w* q6 Y' e  E0 r
[color=rgb(155, 155, 139) !important]#---------------------------------------------------------------------! t, a5 r; N3 _4 Y* Y
[color=rgb(86, 156, 214) !important]global( d3 k+ K; N8 ?, K  W+ p/ z6 N
[color=rgb(155, 155, 139) !important]# to have these messages end up in /var/log/haproxy.log you will: f) _, T$ n0 w+ U+ q8 ^3 F
[color=rgb(155, 155, 139) !important]# need to:
$ A# W( n- l  E8 x$ J6 Y[color=rgb(155, 155, 139) !important]#4 L7 R- P1 l6 i
[color=rgb(155, 155, 139) !important]# 1) configure syslog to accept network log events. This is done
) o: O9 a2 b; q' x, y[color=rgb(155, 155, 139) !important]# by adding the '-r' option to the SYSLOGD_OPTIONS in
8 w* U8 z6 u8 ]. b' Z/ r[color=rgb(155, 155, 139) !important]# /etc/sysconfig/syslog- y0 t! l& L0 l7 C7 N3 ^+ C
[color=rgb(155, 155, 139) !important]#* G$ r! M, `5 B/ ?9 u
[color=rgb(155, 155, 139) !important]# 2) configure local2 events to go to the /var/log/haproxy.log
* V4 K9 D# U5 K; O2 }& i[color=rgb(155, 155, 139) !important]# file. A line like the following can be added to  {3 V' N& T2 `; R0 o$ j" g" [
[color=rgb(155, 155, 139) !important]# /etc/sysconfig/syslog) v. e) d& e- {% x& \7 i- P+ ~& J
[color=rgb(155, 155, 139) !important]#
  C- W5 {8 F4 h/ X[color=rgb(155, 155, 139) !important]# local2.* /var/log/haproxy.log
% A# I4 n0 w0 n( |0 W) ]. [  ^$ s[color=rgb(155, 155, 139) !important]#3 a% \) f9 z6 G" `( e
[color=rgb(220, 220, 220) !important]log[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.1[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]local2/ A4 z/ s& S& |8 p/ }- z( b
[color=rgb(189, 183, 107) !important]chroot[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(244, 187, 21) !important]var[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]lib[color=rgb(218, 218, 218) !important]/[color=rgb(255, 128, 0) !important]haproxy4 {1 K& m( i. b1 m& i# o) ~. P
[color=rgb(189, 183, 107) !important]pidfile[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(244, 187, 21) !important]var[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]run[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]pid
  f+ l. b# l6 {# Y: h3 O* }[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]4000
7 ]+ w) l* I8 j  U6 ^[color=rgb(255, 128, 0) !important]user [color=rgb(255, 128, 0) !important]haproxy
9 n' u- ^+ H9 _[color=rgb(255, 128, 0) !important]group [color=rgb(255, 128, 0) !important]haproxy/ k/ o# ?6 H9 G% Y9 A6 j
[color=rgb(255, 128, 0) !important]daemon' O* M& q& f2 |9 Q4 [1 g  |1 ~2 w
[color=rgb(255, 128, 0) !important]defaults
* N6 S4 F* G6 p0 {! n# U[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http+ |1 R# b/ }) Y$ e, M
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global" d1 t7 a1 a5 j; z& b" W2 U
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httplog
- u" M8 d9 N) F. i[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]dontlognull
4 B& j% u3 N9 Q. j$ `( P[color=rgb(255, 128, 0) !important]option [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]server[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]close" O, Z9 y* l, ^; D$ s" d5 [
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor [color=rgb(220, 220, 220) !important]except[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.0[color=rgb(218, 218, 218) !important]/[color=rgb(231, 163, 122) !important]8& K3 _7 _/ L; x. _  A( }9 h; k. H
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]redispatch/ Z) f' B  u7 q* P: r+ \
[color=rgb(220, 220, 220) !important]retries[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3. `% H3 C% u) k: {! w! M
[color=rgb(255, 128, 0) !important]timeout [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]request[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s
  l) v- M6 U$ b, o[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]queue[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m
9 I. L& s4 ?* O4 T# w[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]connect[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s' q8 T' `: l) Z. _0 _
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]client[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m4 O5 Q' a  F) L" p; d
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]server[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m
' h( F; u% R! Y8 K( n( `0 r7 h[color=rgb(255, 128, 0) !important]timeout [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]keep[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]alive[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s- k/ s! M: m$ k0 C7 n- K) u6 e
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]check[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s
' G5 w3 X4 ~! l- i8 K; r7 L[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]30000# m4 W6 E  n0 j' s. r
[color=rgb(255, 128, 0) !important]listen [color=rgb(255, 128, 0) !important]stats
' a. c: ?5 g: x, X" t0 R, n/ h[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http& o+ ^% _" z: }3 d; X
[color=rgb(220, 220, 220) !important]bind[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]0.0.0.0[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]1080
8 o% X# X  R4 d) Q9 N: l[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable
- b- e6 X' A8 D0 B[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]hide[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]version% u) o% _" ]6 H
[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]uri[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxyadmin[color=rgb(216, 216, 216) !important]?[color=rgb(255, 128, 0) !important]stats6 @7 k7 S# G. \1 f+ N3 W, y" H- }
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]realm [color=rgb(189, 183, 107) !important]Haproxy[color=rgb(216, 216, 216) !important]\[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]Statistics
: I4 n6 W, I4 f9 N/ H% ?3 e# ~) b[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]auth [color=rgb(189, 183, 107) !important]admin[color=rgb(218, 218, 218) !important]:[color=rgb(255, 128, 0) !important]admin9 z. s& n4 C: @  W' T1 V
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]admin [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(244, 187, 21) !important]TRUE
! m) [- R; V2 }& w3 a/ _[color=rgb(255, 128, 0) !important]frontend [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(32, 176, 218) !important]in
7 W/ o1 }1 z8 g: ?% @. |[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80. N2 t* D7 k4 a
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http
. P% W+ d+ w) e+ |: @! e  J[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global, L- o6 k5 v; Q7 G
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpclose/ O8 d/ X; Q, S" y
[color=rgb(255, 128, 0) !important]option [color=rgb(189, 183, 107) !important]logasap[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]#不等待响应结束就记录日志,表示提前记录日志,一般日志会记录响应时长,此不记录响应时长) j, c0 F( \3 ]8 F# L
[color=rgb(255, 128, 0) !important]option [color=rgb(189, 183, 107) !important]dontlognull[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]#不记录空信息
& M6 w1 a5 U6 ?( H; ?[color=rgb(255, 128, 0) !important]capture [color=rgb(255, 128, 0) !important]request [color=rgb(255, 128, 0) !important]header [color=rgb(255, 128, 0) !important]Host [color=rgb(220, 220, 220) !important]len[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]20[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]#记录请求首部的前20个字符7 @3 j8 `( j& [$ r" {/ ~2 h9 z
[color=rgb(255, 128, 0) !important]capture [color=rgb(255, 128, 0) !important]request [color=rgb(255, 128, 0) !important]header [color=rgb(255, 128, 0) !important]Referer [color=rgb(220, 220, 220) !important]len[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]60[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]#referer跳转引用,就是上一级* M4 ?2 C- C+ y6 v& _( L6 x# p" k
[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]servers- u' u9 Z7 F3 W% _5 T. D
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]healthcheck
  {0 [* }6 v) b: V9 g[color=rgb(189, 183, 107) !important]bind[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]1099[color=rgb(0, 111, 224) !important] [color=rgb(155, 155, 139) !important]#定义外部检测机制& u  R. Q7 g" m4 x& ?8 K
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http7 D. V/ O5 j8 l
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpclose
7 ^8 I, L% J/ L" l2 _7 D% @" g[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor
3 R! L- |5 r( Q3 Z& Q7 f' _% e( z7 T[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]servers
' V. `! d& T, E3 L5 D- z4 T% v* z6 v[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]servers+ j/ p# N8 D2 Z# w
[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]roundrobin. P4 V6 I1 A0 P0 D
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]websrv1[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.111[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2000
5 Z0 p" G9 w$ |# R* E  p$ n' z[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]websrv2[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.112[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2000
: s& Y& G4 j5 u6 d( F# [5 ^# \2 a

, [" U4 t& a, T7 C  N( \
% h& C/ W; U! r0 \: I6 l8 d
# o; }, _/ u5 j/ c
负载均衡MySQL服务的配置示例
: G. q! v. N4 |; g* t1 }/ }  p# N/ m4 l
1

& y6 \3 p5 w1 |, |3 |% ^+ V- S
2
# J5 _, C) m8 r& r
3

& \# V% C: }0 m, k9 G; \
4

: f1 z: y  i3 x" Y- I
5
. x" A3 G6 Y( r. T8 m) Y8 K5 \
6

8 L$ `; o' _) `8 @/ q! W$ w5 I1 a# ?
7
" r* j3 j+ w9 z: T! k5 o
8
8 q9 O% V" t0 h6 q+ n& x# `. q# t  u
9

; c" {" w: Q- q( ^
10
" ?3 D% z" m+ q. K+ C
11

( z0 F3 {( O. i/ D" T# \  M
12

0 M4 H$ S  a( P" K
13
. Y% M* O8 @2 Q) o0 k" F
14
, p: U$ ]2 G8 ]5 R3 U; ~
15
; @% N9 L( J7 ?
16
. |+ A* K  |! H# Y; C+ `+ V9 t. G/ A
17

) j6 C7 f: r8 C, Y0 m* D2 ~$ \3 ?5 W3 i
18
( X# `. H! k: C- ^: c) W+ ]
19
! e4 U+ }, u  ]* {' C+ z
20

( X% m4 D" O0 |. Y9 l5 w/ r: D: p# M
21
! d7 T! j/ j6 X
22
1 k# I$ B$ t+ ]5 k" ]
23

. L# O; v4 M7 o& F3 a
24
: Q4 B+ K. x! I' y% Y+ z& U/ s
25

% Q; Y* f, D# `  n' |. e
26
" z5 Y% \2 `) p0 E& V& q- Y2 r/ D
27

% ?  _9 s4 |8 s) l3 i
28

; n  U" X$ N  U7 _
29
- B; ~# t' R/ c9 S( _# b; b7 V5 E# O
30

$ ~; Y1 I2 H+ R7 Y. R1 p
31
) G) a/ V8 N  x( z
32
- z  O% \" O& T2 D( b! F
33
( F% }# u6 A/ N9 u# k0 c% w
34

8 S, n* A5 V5 z/ x& D( q% m
35
+ G4 l- G' Z4 F6 K9 ]# A
36
9 Z. f4 z" {5 s- _& l4 n  F# q
37

6 d: W- F8 k$ F: s9 D' @6 j
38
" K! p, S4 N( T6 M
39

+ F) s' Y. o; v, j; t/ {( F
40

5 o4 y* w: D8 V0 w; `
41

9 A  g( [; t% [! \
42
2 Q) k, l8 j' k( C2 X' d7 f
43

# L/ q% Y1 D5 T9 _: r
44
& Z7 ~1 m4 l: F  ?4 \6 X1 h
45
+ |! X) O- I2 E0 R
46
/ P  J; f* Z1 _( G) U
47

: X+ s  e/ H, e" C) M; ~) x: B
48
5 o5 ?  C3 c/ [: u/ l6 J  c4 N
49
  Y8 ^1 W: _6 l7 q4 A
50
. u5 P" Q, H- y5 |1 q1 ]
51
) \1 k4 X+ V2 _! H# n2 g, ~( ?
52
9 I8 s) p- f7 [) F) \
53
# Y$ n  I$ N# G
54
0 w" l' `% H/ w! A( Z
55

1 e3 g9 G' \8 R) R3 j% W! k8 P
56

4 L1 a, B) X1 ]. j6 T
! M0 V2 V! @8 j
[color=rgb(155, 155, 139) !important]#---------------------------------------------------------------------$ I7 |* C- p. O8 ^! i: m' N
[color=rgb(155, 155, 139) !important]# Global settings' u% W4 B# z# P, O3 c: t) S
[color=rgb(155, 155, 139) !important]#---------------------------------------------------------------------
4 m" G9 k: d- H" f8 I/ F[color=rgb(86, 156, 214) !important]global: B0 [/ O+ G. {
[color=rgb(155, 155, 139) !important]# to have these messages end up in /var/log/haproxy.log you will
8 A) i2 m/ {& G; e' ^: w[color=rgb(155, 155, 139) !important]# need to:
, u: ^" ]4 X% Z. v8 a* A[color=rgb(155, 155, 139) !important]#* P; G; ]7 |2 c. U$ h3 x! z
[color=rgb(155, 155, 139) !important]# 1) configure syslog to accept network log events. This is done/ {2 y3 n9 T9 Q. U4 C
[color=rgb(155, 155, 139) !important]# by adding the '-r' option to the SYSLOGD_OPTIONS in3 @# R. N7 m! s" P5 y  {  ?- S" a
[color=rgb(155, 155, 139) !important]# /etc/sysconfig/syslog. b% Y+ _6 d' c* Q, q+ R
[color=rgb(155, 155, 139) !important]#/ J; l1 R7 [2 N" @8 J
[color=rgb(155, 155, 139) !important]# 2) configure local2 events to go to the /var/log/haproxy.log, u4 P7 K% y$ {! d& \
[color=rgb(155, 155, 139) !important]# file. A line like the following can be added to
+ W' F+ M6 Q* f, B4 o" l/ x& H[color=rgb(155, 155, 139) !important]# /etc/sysconfig/syslog4 y' ~+ X& M5 _4 E8 b. l
[color=rgb(155, 155, 139) !important]#
, [* g8 R! D* E( G+ o[color=rgb(155, 155, 139) !important]# local2.* /var/log/haproxy.log
* a+ s" ~& q' R, n( W[color=rgb(155, 155, 139) !important]#
( C) b; H) x6 [" A" S6 p- M+ ?7 ^[color=rgb(220, 220, 220) !important]log[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]127.0.0.1[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]local2, H1 E3 A. Q. H! H
[color=rgb(189, 183, 107) !important]chroot[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(244, 187, 21) !important]var[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]lib[color=rgb(218, 218, 218) !important]/[color=rgb(255, 128, 0) !important]haproxy
; {+ ?" i4 y2 {" [* ~[color=rgb(189, 183, 107) !important]pidfile[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(244, 187, 21) !important]var[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]run[color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxy[color=rgb(216, 216, 216) !important].[color=rgb(255, 128, 0) !important]pid; v6 U% R/ y  y! q) E8 J
[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]4000- }, l- _/ `+ W: n5 `0 Q1 k- O
[color=rgb(255, 128, 0) !important]user [color=rgb(255, 128, 0) !important]haproxy6 `/ `# N9 o$ T, B
[color=rgb(255, 128, 0) !important]group [color=rgb(255, 128, 0) !important]haproxy
( S6 V: }( w- i8 U) m[color=rgb(255, 128, 0) !important]daemon& ?$ u: s5 R) v
[color=rgb(255, 128, 0) !important]defaults
2 k) K& Z8 b* E6 w[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]tcp* Z- z( I, m' h+ P
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global
8 u8 F' D  W' d. ?5 F9 k5 |[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httplog
1 _$ m- w) ^- [[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]dontlognull
2 x5 l* T; N. u" J" g3 j[color=rgb(220, 220, 220) !important]retries[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3
# y! L4 n0 K* q9 y+ s6 }[color=rgb(255, 128, 0) !important]timeout [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]request[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s" B; i9 w9 I# W2 `& b$ R) r
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]queue[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m
( u: E/ M/ q6 R[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]connect[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s# U: Y  F5 k3 E1 }0 |( k" j
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]client[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m& p; A, G6 Q* \: H
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]server[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1m5 G+ Y( t7 J  l5 s* k
[color=rgb(255, 128, 0) !important]timeout [color=rgb(189, 183, 107) !important]http[color=rgb(218, 218, 218) !important]-[color=rgb(189, 183, 107) !important]keep[color=rgb(218, 218, 218) !important]-[color=rgb(220, 220, 220) !important]alive[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s
- G$ h) j& x' m4 Q7 K& m- E2 _6 |[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]check[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]10s
7 ^) K5 H6 s$ k/ f[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]6003 r& E7 {. D. k% P! Q' b+ J( o
[color=rgb(255, 128, 0) !important]listen [color=rgb(255, 128, 0) !important]stats
$ s, Z* m. g; `% s6 q$ f$ w7 }6 C[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http0 h/ Z7 @& j8 v: [0 D& |8 o
[color=rgb(220, 220, 220) !important]bind[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]0.0.0.0[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]1080" H4 M3 A6 u6 d2 z$ h4 P! C
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable  Y7 t6 ~" E& [
[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]hide[color=rgb(218, 218, 218) !important]-[color=rgb(255, 128, 0) !important]version
, M. j( v5 o. h$ N8 [0 W7 i0 V[color=rgb(255, 128, 0) !important]stats [color=rgb(189, 183, 107) !important]uri[color=rgb(0, 111, 224) !important] [color=rgb(218, 218, 218) !important]/[color=rgb(189, 183, 107) !important]haproxyadmin[color=rgb(216, 216, 216) !important]?[color=rgb(255, 128, 0) !important]stats
" }; P, M# q7 b( Y% i[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]realm [color=rgb(189, 183, 107) !important]Haproxy[color=rgb(216, 216, 216) !important]\[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]Statistics' L9 @6 m! S. D) H) q
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]auth [color=rgb(189, 183, 107) !important]admin[color=rgb(218, 218, 218) !important]:[color=rgb(255, 128, 0) !important]admin' v. s5 A4 N  z% a9 p
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]admin [color=rgb(32, 176, 218) !important]if[color=rgb(0, 111, 224) !important] [color=rgb(244, 187, 21) !important]TRUE! ^% K; [" \( M+ {# Q* ^3 Y
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]mysql
9 w. x- @+ s9 e- R( `  d[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]3306
+ f( p' e! P# o; l[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]tcp
) v" j' }" j8 B. V7 W# u[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global1 J9 [7 K; J2 [4 h  h# X' }7 }6 v" O
[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]mysqlservers* q! r3 q3 ^& F& Z2 W6 @
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]mysqlservers
, ~8 V5 T0 _) g2 |& d[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]leastconn
* @  @" S7 p. d% d5 _. ?4 K7 @[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]dbsrv1[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.111[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]3306[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]port[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3306[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]intval[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]rise[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]fall[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]300; Y* P, |+ @' K
[color=rgb(255, 128, 0) !important]server [color=rgb(220, 220, 220) !important]dbsrv2[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]192.168.1.112[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]3306[color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]check [color=rgb(220, 220, 220) !important]port[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3306[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]intval[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]rise[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]1[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]fall[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]2[color=rgb(0, 111, 224) !important] [color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3003 \9 @) G9 g! |1 h9 {+ P$ j

4 H5 K: D" N3 T$ V. [6 M
5 |9 n) I# G. r$ |' v
4 {& S0 s5 X2 L; a
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )点击这里给我发消息

GMT+8, 2026-4-8 21:31 , Processed in 0.136940 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

快速回复 返回顶部 返回列表