易陆发现互联网技术论坛

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

HaProxy详解

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

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

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

x
本帖最后由 rui 于 2016-8-17 13:32 编辑
, {. \* y+ Q: E# o  c4 q" n4 j: J! Z6 E2 R
一、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,安装haproxy2 l1 F* K- s& }" k
( I8 `) s& V6 m% {
1

7 j/ g$ k  w: }1 s: P4 V: b' y5 t$ J
2
* k% i+ t* t/ n6 x8 E- y# x& |
3
. W$ x7 {& a: g' v( f5 f
4

* T' {* y* h8 L* h& U& ?  v
5

+ o9 ^3 F3 W  s4 Z* P
6

- E- G' T  H1 j8 \# x- z5 A
7

) i4 ]6 x6 W2 y: a( S0 u
8
4 S2 b. @% I* S  B$ R& K& r& W7 L  T
9
3 x+ t& ?; k9 B0 `
10

6 r% A" ?& {9 D3 ^- N8 |' J2 h, B
11
$ Q& k# m3 A! Y
12

# m9 ^7 F: [5 u
13
1 ]$ A; |5 ]- T' t) |! z
14

' j1 I/ |" b* B2 z
15
7 ~- f2 Y7 n; r8 I& f* K  d
16

/ T0 {* i: i9 n$ W; M
17
$ e+ }& M; i, [
18
1 q) q; O# W2 U) D& |/ }9 }; m% f# [
19
2 \/ R! i& Q) Z) W6 S. z* C
20
+ X; n& H5 ]* D: D8 z4 |& L7 Y
21

" _$ n, v0 v* N2 o+ @+ j; N" t& Z
22

0 w* Y( W* B0 E: Y
0 L0 i/ D: L4 f( e/ t' K
[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来安装% R0 K6 {$ ]# X: K( s( I
[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  J9 T1 k2 C4 ^/ k. s3 K; ~
[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])
( u- b3 \$ a3 f" L5 a8 e[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
4 X& b- ^* T0 _[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 o( H( t8 a! b$ C
[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]org* E1 m* M! S1 _2 Q" W! P
[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, \& {8 z. P. T" e" x
[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]+
9 ]& N( O* L% E( c: Z[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
8 k' A6 d5 d3 T# Z. D[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>! C3 G9 [- M: q
[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/! P5 H- W/ ]+ N
[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! W. ?% e2 `# p2 |" z  r
[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 haproxy( T' @+ Y: v' Z$ N" r3 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]haproxy
0 K8 y$ t8 G) ~7 z1 a' f5 ?[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]>配置文件
+ S" S9 G, }& `4 R4 U( C[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]haproxy' H) [* V* W8 N. h) c; W7 t& o
[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]haproxy! t1 w# M9 _$ r
[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
' R4 d9 @: x. |+ j' I, 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]halog
& m" t% i4 m, [' C: E- R5 I5 T; Z  A[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) o2 Y! j8 O  _8 n& \: A4 A5 ]
[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]haproxy8 \( i  Y) K# z1 F# t
[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/
" v7 o; a* j2 _' k
; k$ E9 R  Y/ {% {; a# B. h

! K  I! p& W% y; X+ j' o- L
3 m6 ]8 s$ L; S  Y) H3 k

. S0 c% I. k, Z$ J2,详解配置文件

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

2.1 配置文件格式

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

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

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

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

2.2 时间格式

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

, K5 z9 o# \' _8 G6 K  H
1

/ o. {6 ~5 A2 n- {. l" L
2
: R. h8 e! r9 p/ l' f/ x
3

$ M8 Y0 m9 Z4 b7 v# q; E( _9 b
4
: X5 |, E4 W6 h
5
6 \" H# o% c% j2 g
6
6 `, B$ U/ `6 g( l; D3 I3 W5 T
) b* a" ?% a- y8 y
[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秒;% t1 e% h0 [% j: P; k* Y; ^9 a
[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秒;
1 Z8 V' ?8 b$ 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])
- t  }! e3 q8 h+ L[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])
5 v" |! @# m" I- n4 R8 K6 [[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])- q0 g8 Q0 N- v  Q, `; g
[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])
- q* t) l. r; d3 ]0 P/ C  C) P7 c  v* v$ V: ~

1 n" n/ n7 i$ b9 w- _, f4 y
7 v3 W5 h* y- L/ {: N

) X: j# c0 Z' f3 Z2.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/ ^5 M, M4 ~6 y, @! i; {

* 性能调整相关的参数

– 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:

2 {+ K. \" c3 t4 {( O: h6 Q6 c

* Debug相关的参数

, q: ~* b  \5 Q' F, {3 }9 Q
1

0 Q6 E7 ]9 S3 l( L
2

0 f; g5 Y% c1 V
3
0 Z: u1 a6 ?* N! }; v
2 `: V" h, Q) I# t  a4 D0 n
[color=rgb(218, 218, 218) !important]-[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]debug
6 c; c9 w" J! u, t: J7 y4 y
* z2 {+ U5 k" `" P[color=rgb(218, 218, 218) !important]-[color=rgb(0, 111, 224) !important] [color=rgb(189, 183, 107) !important]quiet
1 x2 L$ x4 X. i/ a9 f# y' D( `& \7 j4 w" `) }

$ j" g) O! G! r7 i! m
3 u: C" \  x3 [  c* z. E- |: d; `: q

* 超时时长

) H! T, ]4 y. a  t0 }
1

! h# v$ u# K& H( k* x9 ^+ N% O9 V( E
2
* T2 s( J; g0 `7 P2 @
3
- l* G) f3 O( t- s* @0 a
4
0 ^- _& `) k1 C( T+ l5 O6 Y
5

! Q1 y2 y! T2 r$ J6 ?
6

# K/ ?' L5 G8 R% \8 `
7

9 C; |' v4 W# H! b3 O' r
8

" Q' g/ i9 G/ K, K
9
: K. x, Q# Q$ n' e0 o$ R% C
10
, [/ g3 J" T$ ~. p/ g# X
11
: t  t% H9 j4 Z; A
8 g( [4 g  `' C; q( g# L" |
[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] :在客户端建立连接但不请求数据时,关闭客户端连接
: ^" m( O. `# k" b[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]queue[color=rgb(0, 111, 224) !important] :等待最大时长
" L9 r; l0 m, C" r[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将客户端请求转发至后端服务器所等待的超时时长; h3 O0 i& z" v) h
[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]client:客户端非活动状态的超时时长
' B& z, W0 m$ V# o% L[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]server:客户端与服务器端建立连接后,等待服务器端的超时时长,
$ ?) c% Z; N9 R( H# o8 z[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] :定义保持连接的超时时长
0 Y2 A8 N7 c. d5 p2 S[color=rgb(255, 128, 0) !important]timeout [color=rgb(220, 220, 220) !important]check:健康状态监测时的超时时间,过短会误判,过长资源消耗
# w. U: Z7 S* ][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最大的连接数: h7 G7 g" r3 G! Y3 }3 O

, A* [  c) |6 j( r+ p0 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] 在使用长连接时,为了避免客户端超时没有关闭长连接,此功能可以使服务器端关闭长连接3 ?. V7 T# c8 F+ Q8 A
[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] 的选项
- m2 _" ]/ w3 {7 _  T8 g
+ L9 ?& r  D" A5 R& I8 h( A( @
5 F4 u* {* N$ U& [% @) H' c+ H
# F0 G. h6 V' C) R  ]% z

* 实现访问控制:


1 |- D+ C' m, a) B( _" W
1

; o  y; u+ J& \5 I  Y$ B7 a
2
; h  m! M( o) d" p* D
4 v6 T- d& I/ M" r% W; a1 n: a
[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层过滤
  A/ d! p, d* T' b3 X- H' @[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层过滤,四层过滤
8 G' a* U  }3 g( z! Z
5 J* X2 l- e8 b

" x& D3 K: |+ x7 ]- V, G' W

0 c6 }8 y* ~$ y' z' n7 ?% y7 e$ K- \. @  ^& e% Y* z- ^
2.4 代理

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

1 Z- m  a& ]7 A5 n* P
1
. ~! e: A$ D  F; `8 ^- w
2
# F; \) W. x6 U" M7 ~
3

8 p3 a9 Z$ R1 x. T# t
4
- W  O7 t  F( R0 ~, D- ]
, `, J7 \7 }# l; Q
–[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]>1 i7 _; D/ C3 T; @# k
–[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 a$ \; P1 X5 I# r' W; L
–[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]>2 ^% H6 T! V  e8 C+ w) C
–[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]>
$ z. E+ o* Q! b1 t2 K! X$ F( W2 o- n& ^
+ ~! m, @3 z5 E+ E1 D

& r* o! N4 M* T7 _6 A( [

“defaults”段用于为所有其它配置段提供默认参数,这配置默认配置参数可由下一个“defaults”所重新设定。
# K: W. }& V3 y9 u- t: J- G! Q“frontend”段用于定义一系列监听的套接字,这些套接字可接受客户端请求并与之建立连接。2 Z$ ?5 @& {7 L$ p
“backend”段用于定义一系列“后端”服务器,代理将会将对应客户端的请求转发至这些服务器。8 F  O) M  H5 p" j% Q
“listen”段通过关联“frontend”和“backend”定义了一个完整的代理,通常只对TCP流量有用。

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

三、配置文件中的关键字参考3.1 balance
# T* u: s1 L1 l  i0 x; h2 J  x. z0 S8 f8 ], M5 ^+ u# Q/ Y2 ]
1

) C& ], [9 f* @3 K  `( A- o
2

+ ]- x  M6 G6 R; o( h
3
7 X$ M+ Q6 e; w; C
* x7 x( v+ A) k1 n
[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]
0 R, c' ?& ~' A! J3 g) u1 u
8 g/ C- |5 a3 |* ~: X& m6 {[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]
& E9 a0 ?$ U  ?( J$ f( T1 \' B
- J  r; q+ Y, s  G7 u  ^0 Q

4 V- B" j; s  T

0 V4 G9 k" {* F- n' U

定义负载均衡算法,可用于“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
) H0 `5 F- M" ]' ksource:IP层,位于同一个NAT服务器背后的多个请求都会定向至同一个upstream server,不利于负载均衡,一般只有不支持使用cookie插入又需要保持会话时使用
7 B% n/ @6 r3 s( Q. pcookie:应用层,有更好的负载均衡效果;

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
; a0 D0 Y  S- t9 Y
! J4 H' w6 M* w( Y& }5 @( p
1
6 l' C, @7 R2 V/ L2 U6 y# W8 I
2
* S4 U% q$ r. g8 v0 I% p8 \
3

$ R, ]7 T& I, h8 B& Z( B, \
2 B0 E0 v5 V# Q
[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]
$ k; M, P7 g% Y9 ~. T
1 [' H* h( V- V# w[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]>- d* ?" e" q3 E8 f! V: {

3 O; L# x$ E- n% a8 ]+ ?

0 v( a, g% U' Y1 ^( z0 [/ K
: J& a5 d% `; p$ j) v) J

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

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

3.3 mode
7 l. U6 ?' U4 I5 R! l& n& r6 d' f
1
; Q3 g/ E" x, h

) z9 j9 c0 K6 M  _: [
[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]}
. m/ r. U% z6 o5 J, f1 W9 ?3 C+ {; H. ]5 d8 ~' v/ ^0 L

8 Y2 _  O$ P" Q) V' _

2 u- o7 E( {" H6 O

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

tcp:实例运行于纯TCP模式,在客户端和服务器端之间将建立一个全双工的连接,且不会对7层报文做任何类型的检查;通常用于SSL、SSH、SMTP等应用;- S" j- B9 `0 }$ @+ P
http:实例运行于HTTP模式,客户端请求在转发至后端服务器之前将被深度分析,所有不与RFC格式兼容的请求都会被拒绝;此为默认模式;! V' p% ^7 c- r2 ~
health:实例工作于health模式,其对入站请求仅响应“OK”信息并关闭连接,且不会记录任何日志信息;此模式将用于响应外部组件的健康状态检查请求;目前来讲,此模式已经废弃,因为tcp或http模式中的monitor关键字可完成类似功能;

4 {& r9 f8 Z6 t7 }/ `; m
3.4 hash-type
7 k* q) t5 j# ^9 v* w$ u% A" H- I9 F
1
$ v5 j* I8 U& D3 x  L" e; H+ ]. B$ |

  \( e! Z# B( h
[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]>% T5 E5 s5 u- J7 S9 C3 \
% t' a/ F* c3 z  D
6 M! e( e: `+ O- ]
7 r" W: L- B7 w5 A# |

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

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

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

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

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

重以获得更好的均衡性。


* d& q9 M: C. T4 L3.5 log
1 B; [% O+ |' q( `6 U2 }0 J, q5 q% B; X7 s* j; ]) l% [5 O5 K# F, j: X  T
1
0 O3 }' C: ~# ^0 F; F  S+ l+ ~
2
, _" n8 V* e4 ^/ e2 U# R2 R
3

( S9 f. N- F9 u% r
3 F, R2 H2 i* t5 e0 b, I, Q
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global" `  Q. `7 o3 W0 x2 A: p( ^" A

4 F, _0 W5 z9 a& a6 H7 U1 O" L[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]
  r! l+ J$ \1 }' \3 O: i( i
; Y- ]+ ?. I7 ~2 K0 f

6 Z. A" ], x3 J, O
7 E# A) K" E4 ?

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

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

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

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

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


- z: e, A1 S; B, Z3.6 maxconn' m' I. y/ l. B6 w; p6 Z4 _
* U6 S  I7 i' j  m
1

4 g; ?: I1 m( \; z: I, a2 ~, V/ S+ b5 r5 T1 P
[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]>8 Q: T# K" s* o

2 ~7 u5 {) C0 J; ^" N' q
, f: M% {1 h4 w8 {8 ~
5 j2 j2 x% w; s) e0 O3 l+ C1 P

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

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

3.7 default_backend
% N- {: f. Y5 c7 X4 X) Q% y2 K
' S1 T) ]. v2 D+ d) {7 d
1

. O  Y0 ~, v' ^) I& e9 I: I+ B8 f5 _( e, P0 ?5 E1 E3 @/ Q
[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]>. \# i  D5 k7 f, I$ M+ H

( X6 I+ z" x5 r9 D3 d- D( ^/ Q8 X& X( N
8 a3 F% H/ y2 s; m
% m, `2 K" U" C5 |

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

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

使用案例:


. U, m$ Q0 B! E  W
1
: ~% R  f' A4 b; K- p& d! u8 ~2 B. A
2

1 i) u) k- ?4 l! Z; O3 q! ?' n* K
3

" m8 c: h6 ^/ ]% c, o: v5 ~$ R. Q. j3 f' c3 z9 r0 K( c
[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
0 t" e" G/ f% a( k$ q8 ][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_img0 g& c( o- v9 ]2 z8 o9 A- |
[color=rgb(255, 128, 0) !important]default_backend [color=rgb(189, 183, 107) !important]dynamic/ ^! Y8 x1 x9 w+ u# w  d

7 Z" C3 ~7 W( J. T+ D; W

- O  }+ ~7 A- x
. B$ L6 l) r7 j' {

, U, v& {5 h& x- k3.8 server
4 H+ A; u+ z; |' z
& t! c/ v% V! k$ T4 Y" g
1

" H8 S. `# \0 f
: U5 |7 q0 m' U* O3 a+ Q
[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]
) y% a  R7 a% ?$ [& f. s: I0 R1 U# M2 z4 H
' S8 A. w5 N/ n
3 j5 z4 n5 {. y0 f+ u7 ]+ G

为后端声明一个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后面不能使用/,且不能使用相对地址,以免造成循环;例如:

3 k: z, f; z6 v0 ~/ |
1
- o: p. c3 c) J- P* Y
1 h) D2 j* L) w3 e2 ^5 ]) }
[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$ g+ s6 P# g' E# t

! @+ u  _7 T/ O6 X( o/ [

- I8 F0 R# v' G
2 v; w  K( o, s: W

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

检查方法:


' J, d' [/ g" k5 C
1
) `* C1 \2 ~/ |4 |0 H, R1 D/ N
2
* V2 l- e8 M7 a6 ?! G$ s; o2 r8 |
3

, ?- J* j2 ]/ y& o% f% q) k* I
4

) ]  m! A5 x9 r
5

  H, D7 Q8 Y& s
6

2 _. J) i2 T* F: v! V. D; o7 ]4 [
7

8 m9 b" ?% l  x, b# {/ Z
8
" W7 J/ r" B' r
/ H/ V. y8 B; E- t7 N
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpchk
# \$ ^" t6 S' g  ?[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpchk5 _- H) R7 o4 a- G' D0 Q. D
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpchk
, S. h8 `8 B5 H[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段,例如:
8 C+ T+ G8 Y5 X3 p. r[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]https_relay+ x  w+ C* q# b8 R* `1 I
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]tcp
6 C' F' ?6 T  T' f[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]com( D$ S$ C( i3 q
[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]80
! @4 W8 L1 k" \9 d; `% @  z) T4 Y  y( `
8 n( [) F- B/ L5 n" `

$ O' q: j* M7 I7 K9 p, G
- e! J  }1 A) t

使用案例:

! p( W' W; e0 `! c5 |
1

/ `' ^; ]: m) c5 w3 ?
2

  t8 B  D# A+ ]/ ?" F0 a3 c, N+ j( Y
[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]1000& ^& L9 }+ Z7 ~
[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]1000
5 [! h- B" u' F: O- L/ G; g1 F! h* a+ J1 A

7 N$ @$ F0 W: g& W: E
; C- ?9 w/ Q/ B% W! `

3 B+ j6 q; Y3 T3 [& {$ O* q3.9 capture request header# a1 Y2 k# }/ f6 p

2 z0 o5 B  N# Z, r+ o0 e; A- d! v; H
1
4 k0 }  L2 M0 C$ ?! c

/ q& F* {6 i1 U: k  a6 Y* ?
[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]>
2 g$ D$ C# f, t9 t& {- u9 y: H8 l0 C! {* e
4 W4 @% h+ h/ C) p9 }

* N+ F9 Q4 V7 w4 s

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

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

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

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

* r) u& u! c( A1 s( z
3.10 capture response header
6 [: U! c- ]8 I1 v
0 u( c' H, |4 u6 [. r9 I2 S& G0 c$ B
1
1 T, k, Y' t9 l. H# z+ G$ c" c

/ T0 S' U3 I" ~, k
[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]>
! m$ b: {: _& E% U8 j7 P$ Y" }  `! y3 e. V9 E) r
# ^- V5 P/ i0 U& X3 Z! y, G
2 ~' a- f( K5 N% M! j# o3 o

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

6 y0 U" y- L. U1 k/ d! z
3.11 stats enable

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


; k1 W  k; y& Y- ?2 y- w( _
1
& d! U  ]1 x3 _" }0 l/ _- U: }
2

2 m+ g4 L0 A" E, l# w8 U
3

* ?. F+ F- R- Y$ M/ G
4

0 ]1 d( a3 ~" K4 W  E
/ w2 q3 _# s8 P  O8 J- C* Z& C
[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]stats7 D! X+ U! s* I% }- x' R7 @; D
[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"  g! n1 q5 V; s+ ?; P3 {* 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
# R9 k8 J9 s0 s" j[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' l- O4 m/ F8 l/ c' B2 H' [. Y# f* n

5 J/ T& _# E6 G
3 q: q$ Q& P" A8 E' L. \
5 I' O9 }5 Q2 G, c( s8 E

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

( j0 T4 y/ Z; K( N8 v: Z' }
1

9 F7 m9 D- s' _7 }
2
  p& w7 E% a! \0 P, ^
3

, N. b8 [% g$ K  w& T- q; h! K4 `
4
8 r) i+ n8 J* c
5

( u2 U- w# U" r9 {+ ^
6

. o2 e# ?) l; l# V: L/ D
7
$ e. V, l9 i+ T- U
8
# a/ I+ J6 y6 @4 s
9
2 |# ^5 Y& K2 k4 M% |' V5 u
. C; ^! R7 y6 r9 r% y& m" W
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]public_www' A- i# Z) P' I* o, S, Q) \1 r- u! j
[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
, y0 |* f+ Z6 L7 {[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable+ z% t5 @3 n! B. @, D% {
[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) f" U' \* h6 ?% x2 |  ]6 i! O
[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].' f  H+ ^, ?- `" 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]haproxyadmin[color=rgb(216, 216, 216) !important]?[color=rgb(255, 128, 0) !important]stats( k- n9 n5 [; B9 \" U$ q' n2 _
[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]Statistics6 W  p3 W3 L! W4 c% v1 j
[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
  c; u$ M. g# q: {/ [9 h3 l[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
9 r/ n; i6 N0 r! [
% J# A8 g) H% Y" f  O) ?

: C- V" T2 N$ ], n
) D( D7 N3 W5 s& L" |4 e9 S- n  B; [

9 C9 ]  i0 W2 A  c0 t5 q" [1 E) t3.12 stats hide-version
& z+ ^3 s$ \# a/ ~$ ?! E9 P( u" d* W% B: |! w
1

5 Q+ g% H  C5 D! B
) e  ~; T% d% p9 U) a8 E' O
[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
' S" y/ G1 L! [3 C% }( x% c6 j7 b* d7 i, s# X% Z

6 S1 l0 R- N$ t2 o* ^6 Q

( o, C6 S1 C7 E1 C3 j  b

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

3.13 stats realm
' O/ x( T+ `, W$ O& D) h7 O" i. ?0 \  w
1
/ ?7 ~: R0 ?! r2 q' y8 ?& D

# R+ B* x& L* [( o% b3 Q) O+ r
[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]>: ~9 x# O, Q3 l* R  X4 g

, e0 P% X( N7 q6 d* g1 Z
+ \1 g3 z5 @* {4 A
3 H) s: U' d/ b

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

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

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

3.14 stats scope
# U7 d/ A' s& Y# h( K
  ^4 J0 U. b' ?* y, f3 `
1

8 I0 o5 l/ e# y" _7 ^$ |
8 D4 I* D1 _: I1 H/ }; p
[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]}
8 M3 n6 l  N& F
: l4 [1 S* F- z7 }7 Y8 j

! i! u& t3 i; D9 j4 [! r

( u8 g7 y/ z! n# i+ p( }

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

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

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

# u4 g4 ^5 i: m5 o. X7 s
1
9 F" I4 h' O2 k2 ]# s
2
1 n- n9 k9 C; L! H2 j1 `
3

% z& E: a1 l+ o- j( l
4
) {( |3 H0 D" @+ \
5 X# X: }" P" N% C8 T
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]private_monitoring2 D5 U, @1 F+ B; u" |# `
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable
' |% s' P3 c/ X: o5 w. f6 q[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  c+ P$ {) R5 N  q2 l
[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
) H! m. w+ u: Q+ g( E. r; r7 `% Q! k5 g" W5 `

0 L! Z3 X0 L9 E9 p

2 W  c5 {/ e& G1 |4 Q
2 E- i7 B4 Z  w$ A2 `3.15 stats auth
/ T* n6 k) c/ d# r- b. S# Q  B6 J0 v6 m6 Y% k0 d
1

+ |' i* J  p/ c; F6 G/ ]  Z+ o$ i& P$ U% X
[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]>* L+ A: f. D7 q9 k0 ^2 U. Z3 |3 R) o+ k# e7 w

0 v6 G7 L( A0 X
: o( D% }) I4 j- F  U
, j; W3 d4 t, R

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

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

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

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

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

3.16 stats admin
& K% |' j- X) K9 {% m5 k" b# G# C2 L1 Z+ g5 q. s; S
1

, C' ^) b8 y/ y9 V  k; g6 F
4 m% g" I, @6 v/ i% ~4 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]>4 `) t# D+ M7 E$ l

$ R3 {0 d0 L2 g' f  G- u) ~( E
( I0 P2 s* U. \! x* I- m0 n

5 I7 p8 G1 T6 |

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

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


, Y6 D) x7 n6 K* n. R; q2 {4 o2 G
1

) M. A# K$ {4 C4 s8 U& m
2

" ]+ B' {/ A7 j% D5 m: T
3
" o- D  j7 C* |( Q
4

& r, g( }3 s( ^1 e! L! E+ y5 e8 ~
5

1 V$ U* f, }# F5 I: j/ ^. C+ b, D' b
6
" F8 U; m- I- W% ^/ `
7
  q% @  X9 W5 h5 c# S; m& q2 @
$ M* _; t' c  M+ b( g
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]stats_localhost
, B& `/ N( O5 K3 n' Q[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable6 `/ [0 i  r7 I. t# M, q
[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
( v9 s9 E& z7 w0 k" k[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]stats_auth' ]9 G5 H* z/ t1 q+ s, ]
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable8 s9 T: b! w6 Q" L6 R) ^
[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]password5 A8 J- C$ L+ p2 |
[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
% b+ H1 S: X& a% Q5 d5 w$ u
7 @% x/ O" H- s3 s- z1 y+ D
; \3 h1 W$ d3 `8 m

; L: T/ a" [3 ?9 I6 T- l5 j9 i6 i# \" O+ k. B5 Z1 e
3.17 option httplog
/ |$ H7 t$ H( v) {% C# w6 f0 J- M" k! i3 |* Y
1
5 B( r$ b: o4 m8 N( R" v! r. k

; ]; t+ }. b- }8 U# ~
[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], c# k- g% Z. n
% ]$ F: j" ~" Q+ f$ a# N4 R
: n# S( b& @9 L. |, P: j
4 d" S2 g& H; K0 k- d4 z3 ^# U! a- Y

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

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

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

3.18 option logasap" a) k" z. y3 `9 F

7 V- M, u. O' S0 _6 u
1
" {% w$ C7 m% Z; z+ t, F5 v
2

. I. A, o: ]9 C/ ]2 |3 k3 w5 Y* ~* z
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]logasap
2 H  k; {" i  K9 P& b[color=rgb(255, 128, 0) !important]no [color=rgb(255, 128, 0) !important]option [color=rgb(189, 183, 107) !important]logasap
( t8 E! @1 j" G. T: V' }9 x' ?0 b! v/ g! U$ }& g; `

  B/ j1 Y6 |: h, z% s$ A3 S
2 u8 k" a/ t" t" B, F, T

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

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


5 p, {/ ^4 |# }# t/ s
1

5 C6 C* ~  ~/ ]
2

- I8 J4 a& u2 U6 }9 g
3

" J' G/ n& F  x2 P% G2 Z
4
8 ~* D8 n5 L8 _8 }/ N5 G
5

: `$ T9 Y- q8 q, q+ L6 w" e  Z" P. U0 z
[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
% P9 a2 K# S' \. R7 ?4 X7 ~" A$ `[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http, z# Y0 [0 p1 j5 K8 o) ~
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httplog
( t! x) j" {* F[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]logasap
- x+ i' y) B- V6 T[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" f" Q9 j% T2 y+ R% b; ^

1 O. a: ^" S" @, Z/ Y9 H

+ M" |4 u$ D9 l% J  o$ _# p& j
( k2 x  D' X' M. U$ q+ ^0 u% h

: @, A. n* N8 J# J" ^+ U7 A3.19 option forwardfor( v1 ~) Y1 `1 Y; w) s* b

+ v7 H: U; o- M8 g) w$ p" `
1
/ q: t6 O9 A* G# t9 E1 }$ p

8 `7 ]5 p- d( h5 h* U& N
[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]4 |- T/ i% L/ k+ y3 b  ^
, l8 b4 E+ g* k; U& Y

/ F; K$ j* o9 K% ?/ x" F$ c
0 l8 H4 _# z" M0 k

允许在发往服务器的请求首部中插入“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。

下面是一个例子。


5 n/ s* P. d4 }7 h5 C- f) D6 C7 J2 z- \
1
' b/ a! f+ C/ R
2
5 U; a7 U: C* R5 O0 d
3

9 G8 L+ U4 Q. E, q
6 r4 {7 A2 T6 a: n+ v% O3 Z& s
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]www
8 {+ t3 F! f. f! K+ _! i- P[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http
2 q. Q  t( n) R0 \0 ]) j* U- Z[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
& H) N( h5 u0 P, H; L% n* g
+ J5 G- ~+ u9 B' N+ o4 F
( \0 W, p" T, g2 g9 S4 X: ^
) l! O. s& k- m( N& G9 g
- p8 `% F$ B. d+ ?7 K; K$ ]
3.20 errorfile
8 q/ x& I* y, A. c' h5 `3 \) r& Y! ?' B' d/ V2 s& R
1

% p" \3 O. H8 v4 ]" L' M" R
! e9 I0 b% R' \
[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]>5 I" C9 Y' i% g, z+ h# T( [
+ t/ G% W4 a$ C& ?" C0 D
# T  W, b& {. ~

; \* m5 G2 |' T! O7 z

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

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

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

例如:


% x; Q5 C7 c9 s
1

3 F* T6 G  d2 b2 T' h! H! |, C
2

. S! H( d7 c( v( d( ^; f
3
7 i* c- S6 W8 c$ h# R& E$ ?

) Y& e& r) m7 E" b
[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.http
* R( `& d" _( k& a# r/ ^[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
8 t# Q2 c) m* Q4 i/ h2 a: W[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! I: ^( E  C  k7 _8 }* i. z! M

3 h1 ]2 i, E1 B3 I' ]* R: r

' a0 I5 N9 Z. I' m/ x% y
  P8 O, j" o) k0 u
! p$ |" I. A% N0 X, S% O6 j# F5 x5 J5 E
3.21 errorloc 和 errorloc302
0 S1 K# i$ j$ ^4 q; {3 a
3 ~4 }7 ^! a; P% Z  s
1
, t: @2 ]# {) @/ R, n5 B1 S% R* t
2
) _5 [. l) Z" p, [
2 B3 m, o% a' G5 B( c# J$ J
[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]>
: a+ b( z& G  U6 n3 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]>, `6 ?; ?% z: Y/ D1 f- D" y' d" C5 D8 }

8 n! h, K) D7 \9 b) r: i4 {9 S0 u

( N5 z7 d- U' v9 B0 J4 o% I

$ y6 p- A' z- x  k8 ~5 A

请求错误时,返回一个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
7 s& R4 m; U  |! I
3 m2 B" Q( h8 z% O5 y
1

" Q* B9 w, n  ?- @
  X% a0 t& t. ^  u( Y$ v
[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]>* P& T! R0 S% h: @

; |4 A/ q9 v6 N2 ]! `
$ U4 d- v/ e: |$ |" `

9 M6 ]8 F0 |; ^4 M5 u5 ]; Z' }% `

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

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

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

例如:

( e1 m" f2 m$ m9 @
1

! J" n8 a0 J0 Y' S" x4 s
2
9 I# j! ]0 ^  ^. K! B
3

: [* W( b1 T8 f" l
4

% c. V0 ?" v7 m/ v0 D1 @1 R( u
5

7 P/ c( P* {  S: }+ ^$ `& Z! T! i' R/ V3 b# p
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]webserver
1 I4 Z' C, D, j& ?9 w$ g) s[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
; P) M+ }" D% m& q[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
; u2 w7 e* \& ^. o0 K* Q9 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$ M  n/ ^7 W  {( ~/ e2 E
[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
, S. J( s2 n1 H' e6 B( p3 U4 c$ C& t' |  j

5 h' b: j7 J( G2 ^7 \
1 m, _" y; y: L  O1 k* N; y
) w2 S4 _2 R7 @
四、ACL

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

/ {9 \' O6 g* U5 G/ ], c
1

( M5 N0 ^9 ?# Y. |# s% O4 I1 M. H7 `5 x! g' o
[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].6 Y+ |1 G. d2 _8 L# r. d- `
& m% l1 C" e' B: r

( Z+ a) C. w7 X% e1 s
6 `7 F% x7 ]" o

<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


; U( |4 t) s# `. g. E
1
" \' g1 y5 _) h! X5 a
6 _5 {2 e! x+ m- F2 c
[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]>
5 w0 W$ h  `: V, l
& I4 i- F7 E. C# ]% v/ M8 v9 P
) b. R/ [8 p  o2 V

. y! u: M( U; ~& H/ \, o

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


. E9 p# M7 c! F- l) w
1
9 d% f" E% f+ r: I6 J7 e5 k7 r
2
, X, K6 G4 m* d) `$ V" G
3

4 r  b7 J2 j  L9 A
4

( c+ B2 {1 A; C4 L1 ~% ], V% ]" q! C' I9 i
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]dynamic$ n3 m5 q4 i$ o+ r  [& P0 N! p  Z! K
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http2 ~8 n8 m; E  o9 U
[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; N3 q6 `3 p6 i8 Z6 \
[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
; m# [, d( A5 o9 |, \5 f/ P! M: N+ u, b; x
) y6 w# b! u" U0 a/ }
' \5 ?! |/ y7 y! s" _+ u

5.1.2 fe_sess_rate


% ]+ i4 X" l" O; P% a# W
1

) t! M0 Y5 @' D$ L+ W) n; L( D$ `0 E+ w* i' \
[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]>/ I' `+ @; E, |& K' b( B

; [' V3 O  ^4 L( C& M+ K& X

) X/ }) G# u' m

4 O) e1 e5 \1 R. e

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

/ x2 _* m% k& J) I' I
1

7 r% E1 R( p' I. a& X
2
1 B6 m! m+ S0 o. s- B
3

/ i& o8 q5 T# A2 ~: o  I
4
& W, Q5 q# _. z9 |
5

6 m: W4 P* U( K
6

- m7 j0 w  p- `% S7 a6 Z; [
7

: h# P8 t8 T1 a8 x% `& m" _
8
8 a& W9 r! t$ h8 }; f8 ?! I
7 X: }4 d. x; _& X
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]mail; q" t0 j; R' v$ e1 K5 o
[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
5 E! W3 }5 F9 t: B- J9 r# W[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]tcp
8 `* c) k9 C+ J+ T3 Y( s[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]500
) @$ X9 P5 Z* d# z- H9 n[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]50
" H1 t. o  g8 I5 c+ Y2 C2 ^; L[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]50ms
! q% z) z% e  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(218, 218, 218) !important]![color=rgb(0, 111, 224) !important] [color=rgb(255, 128, 0) !important]too_fast
3 q9 ~$ `3 ~5 O+ e' j[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
+ l- X0 w3 o& ?- A" j8 r, l2 M" W6 ]3 j3 ?3 b

3 N" V4 D8 T5 _% v5 l: @) L

+ K9 k6 M3 m4 z$ t6 |7 X

5.1.3 hdr <string>


' r" k% ], |: G/ K+ Y! m4 T1 |
1
6 g3 [: `- j- K7 w" z. Z& H, Q
5 P( r1 }4 p4 P' n7 i; w
[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]>* ^5 K; R) M7 b( D) F# d/ [
  d% N9 t& i4 \

0 ^+ m: {+ f7 z6 W, e, H
0 Q0 m; e9 F" t( J; W

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


6 |/ E4 ~# d! p- q: l
1

/ H  |& M/ a& a: H5 d( A, o/ S
[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
' P' l6 z- [( t5 H, A* }
+ `7 {$ d6 o9 O* H$ y3 D
; P; j# g; w/ A+ @

2 Y6 N+ N- K3 a9 q7 u

5.1.4 method <string>


$ Y! i/ I" }/ I+ ^
1

) F7 |; M! I% `  A3 ~8 `$ a
9 a" V6 ~+ N3 B
[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]>
" n* e2 J9 Q& _" U6 F2 r) ~
) p$ _+ z9 Q1 Y

0 C! N1 W  i% G' `7 p" r( ^

) Y* s2 u; Y& z) A( r  z

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

5.1.5 path_beg <string>

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


$ x* L+ G6 e3 r! _3 F
1

* f* `! ?; S) v4 V7 V
/ L( C5 _' R8 w7 A# p* w" R
[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. M, M# \& S2 ~$ t2 h
0 W8 W' h2 J4 I9 J! V1 _

) a" A  g0 S9 q! S- o, s
% B0 l% q3 F3 B# P! a, j

5.1.6 path_end <string>

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


+ @6 X2 m( W4 C+ _. j+ k; w
1

3 A+ J# R- U4 B+ P( ^5 D$ _
8 _+ T+ f- _/ _  d
[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
$ Z% n0 s& V8 ^* q" }1 h7 z5 l0 N6 g6 P4 ~8 J4 m5 H! |  v
1 d# c0 |* h( q: v

1 {  b" ]" ?& c# H& x

5.1.7 hdr_beg <string>

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

+ a: c& {$ G, ~  d1 a
1
% I! K; F$ D/ C( F

4 H8 I" }5 d" j* V- C, r0 n
[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].
3 o9 w' T" z+ B8 s" Z! |0 l$ v
* K0 p( Y- h, @6 \2 o

: n) X+ S+ }9 r* {; V+ {) _/ Q3 f
: r. Q' a3 P6 E& R. p$ [

5.1.8 hdr_end <string>

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

五、配置案例

前端调度器IP:192.168.1.210+ H- h/ ^  ?- h1 {1 A
后端应用服务器IP: 192.168.1.111 和 192.168.1.112

定义独立日志文件
+ J+ G- L# G8 \3 G. O' `* n) v' Y' K) `. o5 T, N* K. V  N/ p
1
, k; k( \- i0 N6 i5 }
2
; h; l# ^4 K7 I
3

, M0 `4 ?# @6 \& O; C% N
4

  |  Q' J; B# g% s7 E5 [6 E6 Y1 R
5
* T; L: q" n& r3 c! q
6

$ `; ^& Y# ~; h$ z8 y
7

6 v) p+ K" H+ v( _* F! h& G7 G( x
8
& M4 x/ d& K/ I* U+ m2 i2 ?
9

% C) y7 {7 ^  H, j: s7 R% ^4 h
10

6 G, ~, t; V( O8 t
11

, W& p, m) l5 N9 z
12

, w) s) d! H# F& ]9 b" [1 K  k" k9 |( H7 x
[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 #为其添加日志功能5 @' ~9 o$ x9 n/ P4 \! D  z3 _! y' _
[color=rgb(155, 155, 139) !important]# Provides UDP syslog reception
9 N) U  K9 S# h/ F4 c' C: y[color=rgb(216, 216, 216) !important]$[color=rgb(255, 128, 0) !important]ModLoad [color=rgb(220, 220, 220) !important]imudp* r. s2 M' }; U- g) s+ g2 {9 j& r
[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,启动端口后将作为服务器工作$ E2 n1 s* D( W* F) B8 u, j8 G
[color=rgb(155, 155, 139) !important]# Provides TCP syslog reception
: k6 O  |% f1 ][color=rgb(216, 216, 216) !important]$[color=rgb(255, 128, 0) !important]ModLoad [color=rgb(220, 220, 220) !important]imtcp
! A+ r; p" C; Q6 T3 t! U) k[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监听端口
$ |: m1 t$ D$ w5 C[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]log8 Z0 L% p" K; r/ g0 t/ e
' I- J9 Y  J  K2 L/ k
[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
! F0 r/ n' r9 ~& w[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
8 M/ o8 b" U0 B7 h5 i% m[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端中添加此行7 w+ t1 m: M7 r" C
3 U6 C- P) z9 U. \+ e* a. @

& ^& i( H) \8 h3 |8 i

7 e* l; L/ c0 [* O
/ @4 J# I0 Q$ a" ~% t4 v  H一个最简单的http服务的配置' Y2 ~! r# V9 P* J- D
: G; K8 f5 R: ~
1

- R( R2 r+ R8 U, I* h' `( A
2

& G5 \  O( w: X2 n6 _1 l
3
5 C) m3 J. t* l1 L+ p
4
( y" _& E& c( G" f7 {" W4 Y4 a
5
! q3 }4 k/ }# x5 X( B- x
6

& A5 g% R: W# x# ~9 z2 C1 k
7
3 d' X5 W+ ^: }% D6 _! X6 n/ @
8

7 r- G+ X' r8 z8 n4 V* h! M
9

0 d5 [$ Y; K# ?% k
10
& c1 U$ p5 G+ ^6 h7 T  e# B- `
11

0 m7 S$ g! i$ Q  N: T& }; @* V
12
8 g0 ?# Y+ k' V3 [3 N- ?% k& B
13
9 A5 n! Q9 Y: P0 l4 j+ W* ~2 q1 q
14
* @2 z5 J( j; _4 b: ]5 @( M
15
0 u' t; s" H/ K# v% {* ]3 c5 N2 f0 O3 \
16
7 o; n+ i) n  q- @9 i
17

) o. o( p, m. {
18
2 a9 _8 R8 a+ `: P4 k, |
19

- `+ {- g( E/ `0 x
20

  u6 i& O" F  s' r; B' A
21
0 ~0 I' l0 O: Z, z$ N
22

3 \2 E1 f; Z: }
23
$ I% H( f5 S& [/ ]% R- y
24

1 d- \5 ]/ D2 O
25

/ h4 A2 X2 T1 ^3 n1 e5 b
26
8 f3 a9 r% u( T* |0 T0 |
27
% s$ X2 R( `9 A/ D. x' L9 Z5 B
28
4 o" z) o$ T1 ?3 Y. K" w, f% }
29

* E$ D& X: a% y
30
/ s, G9 E- |( D; Z  Q
31
; Q0 O/ a8 Q& t& X
32

1 y& K- I+ p! }; e
33
& l# |# Z. {* o! ?# H9 i" `) k
34

4 ]4 f7 ?4 e5 F
* `7 B+ r* V! ~, I
[color=rgb(86, 156, 214) !important]global0 o3 @1 m4 Z. s1 @% W6 x
[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. }# r( `: J% N
[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]haproxy3 o6 P+ K2 w! |, c/ n
[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
: ~3 r/ e! D% q) s[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]4000
9 H1 u) j) I5 D[color=rgb(255, 128, 0) !important]user [color=rgb(255, 128, 0) !important]haproxy8 |# |9 \2 W; l5 h8 W9 S
[color=rgb(255, 128, 0) !important]group [color=rgb(255, 128, 0) !important]haproxy
: l1 O$ T2 u! |+ X[color=rgb(255, 128, 0) !important]daemon
$ [2 e6 I. Q) Q1 C+ g8 s& e[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
) [( E% U- O' l, r; R; ?[color=rgb(255, 128, 0) !important]defaults
" p( o/ q" \2 f[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http( x$ c, \1 D+ |7 p% f6 ]% y
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global# C- i& k. k1 T8 n# i" g/ x
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httplog' }7 m7 Q) r: @& l5 d
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]dontlognull0 W* o: j) p2 c( d( d$ H
[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% V( ]( ^  v* K; s$ N4 x
[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
2 d0 q7 P: \; p% D- K[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]redispatch
" o2 ?0 }# v3 X$ ], M[color=rgb(220, 220, 220) !important]retries[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3
% K" d; F$ [6 @1 x' T+ _; h5 g[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
7 e) P, @* d; u[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
# m7 p& u% _+ `1 b; }[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
+ L' q. H1 ^1 ]' |5 d/ X[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]1m5 Q* n" E$ b8 F7 i: a! {
[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: ]; P# V# C' _! ^  {3 E* B
[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% a7 h+ c. A4 X+ w" o7 ~# 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
9 e% ^4 e/ w* t3 n[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3000, f8 Q7 b& \2 a0 }' t- S( B& a- 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为名称. b4 u  b, g$ E0 g- A2 H( k/ N1 S, ^
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor
* h8 B: b4 n' H4 \[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]801 m( u" g5 O9 v% K" H8 t& ^
[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]app% Z; W  D4 n' B; m& K% E: V
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]app0 P; e$ C( i$ \4 ~+ v
[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 算法2 W3 Q7 h: R5 r* @
[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
  {: C  c) d8 G, |6 G4 ?[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]check% x! ^5 S5 y1 }: t

$ M( O, S( i/ o6 t# n. d

; e- A; z( n. A& o$ O
% H. g& k- Q. U1 G
5 ~1 m: f) M( M( H8 f* V) z9 V
haproxy统计页面的输出机制
, T" \  Q; m$ `0 D. q+ O- w, P. Y7 R: e: q8 J* R8 Q
1

' ?$ ^, a- y0 G4 r% B: `8 i+ x
2

2 {$ i! Y, p' ?  P: j
3

  _4 q9 U$ e! t) S% Y$ @
4
! w4 D+ F3 {5 m% W, `  s4 z0 y
5
/ U2 m. H* p- Y, {
6
; H$ f0 ~4 @$ n) a
7
, @- ^$ P8 P' {$ u$ r2 C
8
/ Z7 K4 D- F) E( V0 S0 Q
9

# k, P! P$ ~: _( j: B( T: _, q
10
. {: ~% g( A" a  s
11
- _! f% o6 g( F, D% e
12

0 i3 |, \/ Z( P) F( b7 r
13

' E0 v. o2 v& v8 ?9 S& B, ]- x
14
' S3 s) F# ~) ?% N' _" l
15

/ H: O8 }5 R/ e( T4 c( i
16
* P) B" E- s- S) d" p. j
17
8 f3 v5 f6 ^% z( t( F
18
/ `. Q$ b- c$ j, q- P: g
19
4 V7 K  O6 Q# [+ b3 S, p. ^- l; S
20
2 R! |& z3 o- E4 k$ D* q0 J

; g- k0 ?3 D( Z- E% r* [1 U
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]webser
- ]* s* r% L; g4 @4 S% @[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
) {% s( ^# E6 A) J# l) Q+ F$ ^; z4 [[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor; R, |0 E6 m) p/ t
[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80
0 i- p6 ]" n, L) q* ?1 M' R6 A[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]app
  O* E( l' Z8 h: L7 Y[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]app
, Q1 J0 ^# g& s0 o+ q& i[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]nocache5 v  {7 V1 R$ r' O
[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]roundrobin+ x. e2 d8 r+ h8 o- C$ U: ?9 Q
[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# x$ @$ ~- X' Q% k
[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]2
9 z  O7 T9 Z7 i0 I[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]backup
& l; n9 l: H) @* h7 k8 C[color=rgb(255, 128, 0) !important]listen [color=rgb(255, 128, 0) !important]statistics
3 C/ s$ k0 v) c5 {, `[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]# 自定义监听端口
/ }& n: Z; G+ Q! V[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]# 启用基于程序编译时默认设置的统计报告( K7 }" Y8 t. M. I) k- ]* J) o% E
[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]# 统计页面用户名和密码设置
! n) U/ g9 o4 a, I% 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]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$ j0 t) |& Z1 [
[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的版本信息
' h/ M. Q& a* a  W$ B) a[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]# 统计页面自动刷新时间8 O1 R6 y+ U( v8 I( I
[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]#如果认证通过就做管理功能,可以管理后端的服务器
0 ~/ c) v4 P' {[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
/ x- M( e' m0 X0 |2 ^5 F
  T7 b! q7 X! v* E9 ~2 D8 u
8 r/ u1 [$ t0 W" \( v

; F& V2 W7 H8 d, X2 `1 \* m- K( \( e* Y* r2 w
动静分离示例:2 J/ A$ _3 f1 O
5 r: y7 [& ~' t: u; k/ o# v
1
! T+ h, E5 |3 z( S! R7 P$ W  E9 O9 o
2

& k, T2 S+ w- O" ~8 n5 p
3

$ D3 h( E8 H# E$ }7 ?8 f
4
+ g; P. i2 k9 X: k3 J: ]
5

9 F; p4 j- f) N8 T- _* D! ?  v! B
6
" K1 J: d7 g. k: h
7

  F; Z" l7 J" F+ y
8
7 b% W, o4 c( Y& j" _  R
9
1 Z4 q" y) O+ }$ r) E
10

. H4 i' A- ]6 s' @$ c5 d
11
) [, B; H( G0 t1 h6 I/ ]6 B$ k
12
, B1 x9 U( S" g/ b; `
13
- i5 a% h$ P9 u* C4 x( O- T
14
+ ]# D, M; R; r" U: M' z
15

- H* `$ A# |: H: Q! J
2 N) S' X- A2 i
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]webservs
# }( K% G& W: C! z[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80
7 L- B1 ^' f; m" j[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' x! d1 C" ]" M# J$ ]" p
[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]html9 i; x4 [4 L6 q6 z0 n% R
[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
! N4 `: U- _4 g$ E9 g* O[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].
1 `5 x1 X6 Y, n1 ~[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
1 L! {2 ?2 C1 |4 q2 c0 l[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
7 c( y3 P6 c* I/ i, Y[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]dynamic4 @* }: j4 }7 A  v" |! @- L( u
[color=rgb(255, 128, 0) !important]backend [color=rgb(86, 156, 214) !important]static
( @& n; q/ m& q[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]roundrobin
6 q9 n9 u/ ]3 [, r6 a5 n0 G* }[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
6 Q* G; L( W, r0 O[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]dynamic- ^- O& l/ |  S! v. H! T, r/ N) V
[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]roundrobin% d! O( p7 b( y5 @
[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
1 }, n) U- ?& p) y. J' `( \
; j: P. b: l. Y3 t5 S+ ^2 u

, k, Q; V2 W' A6 T3 M2 a; z

0 [/ y9 K1 w* b; v- T( _
* ~- B2 }2 G# |* B) C! P5 Rhttp服务器配置完整示例
1 M4 o) E  d# e2 Y
, w. v# b" D' S
1

1 H( X( \" N& l- ?; E; H2 u
2
, }1 ~$ A+ L% @3 C
3

/ M4 s  R" E2 H1 T$ Z
4
+ K4 i) x9 o8 \7 m+ T
5

* E! O4 N7 L/ L2 ~5 _
6

- S) N; c0 {9 M6 ^3 A3 v
7
) Q, F! |/ F* E, Y% c! ~6 U
8

2 k: \* V9 ^+ I4 I8 i
9
2 V4 q8 T- p" f% i5 z& Z# d, o8 z
10

! C/ A: ~5 b/ `, T: p9 N
11
4 N5 q0 w) Y5 m
12

$ q  d: {0 C1 o5 _/ K
13

4 T4 l' `( `1 j4 J: p0 R8 x* @
14
: m- A- t& g7 |3 B) h# s
15
& {# g( @2 w% j9 ~: E/ z1 ?; B, K
16
  ~/ H" ]- ?& s1 L# Y9 p
17

- X6 Y! ~1 C$ P
18

5 Z5 P2 v; p5 K4 @1 L' R, k
19

! X; A% G7 K% n2 K# _* C% |% S
20

  Q7 a. u* J& A) S
21
7 i) K1 x( b6 d! O+ d. V8 t; d
22
! t/ ?+ g2 q; B9 {( L2 ]% W
23

6 h# I' [# U( f' c
24
" y8 \! K  l3 ?4 q, [' i1 k
25

; l0 h- W( y) g: ~4 W  g
26

. U2 g$ D8 O' I+ A# M- B* }
27

% x! ?3 n! z0 J7 d* ^
28
' J; w1 ~. Z! }/ F" N. V- W& {6 g
29

* N( j# ?. Y2 N* S, G$ B
30

& m  }  @0 d' p
31
# Y# q2 ~+ P& a5 L& i/ \
32
' W" T2 s$ Z' h- S
33
8 n7 @- q: R! d
34
, l2 a6 y8 Z4 \: x
35

7 t- M  V7 V# T
36

" B& V5 g9 {! W0 m1 v4 h, ~, x
37

8 y# `5 E, w4 W8 b5 d+ h0 H6 x! E
38

& _+ T" ]+ t. f5 R' j% e
39

" J3 `- T2 H: n+ c% W( D
40

2 m/ C, [( z) D; X8 y& k
41

- E9 P' Z1 h5 Z6 |' E- G
42

" r6 z0 _2 T! J/ e/ w& f! F2 q$ B3 i
43

2 T; @( h  Y, P  P) I
44
8 k7 W# x* p# O- k. ^* N
45

% ^/ L; |1 f$ d/ g9 [! g% N" c
46
8 G# C3 Q: M& Z  H
47

, ]) M8 u6 _% c. w* Y$ k
48

* K% E% J& X7 }) c! ~4 ]! A& E0 F
49

4 r# o8 [% ^- J5 n# q5 I/ f
50
0 p8 }  p* r  k2 P
51
4 K2 B9 m9 C; Q, \& ~9 M. ?# t' J% G
52

% {& B- `* W& f: O1 j& Z3 _
53
6 H( Z% s: P  |3 |. w( z
54

2 }, @4 w1 ?' b) d) b. b. w
55

) D7 ~& z; W" Y7 ]1 ]
56
) L( S" b( K, A2 z. n$ K- A
57
3 m; |4 o# J7 ]
58

8 w0 x9 z' _3 l+ Q' V
59
* c! L0 ?7 b8 Z% {0 A
60
( l7 \- W* {+ G  C* f! K
61

) G2 ?+ e. v1 }* f9 C/ Q) N" J8 j
62

) q' I) P' O) `; {2 c% t
63

9 _4 m+ }8 W1 j% u% c! N" r
64
( B5 o5 o- w8 o0 f) H
65

& N- M) Y' T6 @; l/ p
66
8 ?6 V: c: _+ D* @
67

& Q1 w' R. H& J7 S$ U- x
68
9 y# g- M' q- G' z, f& O
69
7 i9 |9 L- K% n1 n
70

3 r* m+ `, P5 |2 R/ E& w$ U, n/ q, y  Q
[color=rgb(155, 155, 139) !important]#---------------------------------------------------------------------: f  m. j/ Q. N, n+ H' E
[color=rgb(155, 155, 139) !important]# Global settings
: a& H( Y1 t; F. Y! e) y: @8 p[color=rgb(155, 155, 139) !important]#---------------------------------------------------------------------* y. |+ n# g5 [/ S
[color=rgb(86, 156, 214) !important]global. r3 x& m4 c1 n; B, w. s# t3 e/ X
[color=rgb(155, 155, 139) !important]# to have these messages end up in /var/log/haproxy.log you will
% |2 T5 \4 [8 l, U& S! O; x. v[color=rgb(155, 155, 139) !important]# need to:5 \) E! x1 o' R' y- ]# E
[color=rgb(155, 155, 139) !important]#) l1 h& y2 L- R( Q3 I. O/ e  O6 f
[color=rgb(155, 155, 139) !important]# 1) configure syslog to accept network log events. This is done
7 I/ t5 i/ {7 h[color=rgb(155, 155, 139) !important]# by adding the '-r' option to the SYSLOGD_OPTIONS in
. n5 o3 I/ N4 B+ K+ c5 I[color=rgb(155, 155, 139) !important]# /etc/sysconfig/syslog
& l) q/ x% \5 Q# {3 W# K[color=rgb(155, 155, 139) !important]#
4 L7 {8 V  l3 ^* m5 |# ?: r[color=rgb(155, 155, 139) !important]# 2) configure local2 events to go to the /var/log/haproxy.log
" T# ~  x& d# ~) k  f% b[color=rgb(155, 155, 139) !important]# file. A line like the following can be added to
. E+ ^4 w8 Z8 c' p! i4 |[color=rgb(155, 155, 139) !important]# /etc/sysconfig/syslog) e) `. G  Y- Z% T+ V; Q9 X
[color=rgb(155, 155, 139) !important]#
+ a1 s& w2 F! k/ U+ l0 Y[color=rgb(155, 155, 139) !important]# local2.* /var/log/haproxy.log; |  u3 h, Y* }5 n/ H: ]
[color=rgb(155, 155, 139) !important]#0 Q2 f1 f" h5 I# T( O
[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
! ~6 E; t4 C2 A. T[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]haproxy3 E- N4 x% B* x5 n& x/ |& `) B/ q
[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
7 ]+ {4 T) f7 R) R# o4 t* z9 Y6 N* T[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]40009 p9 s+ e9 j7 L5 ?+ @, @5 R  q4 J
[color=rgb(255, 128, 0) !important]user [color=rgb(255, 128, 0) !important]haproxy
' ]; j# d$ x* g5 D[color=rgb(255, 128, 0) !important]group [color=rgb(255, 128, 0) !important]haproxy1 x" c" V% ^7 Z0 v# k; M
[color=rgb(255, 128, 0) !important]daemon9 c# ?+ I5 @& }/ l4 c" e6 C+ n) e
[color=rgb(255, 128, 0) !important]defaults
/ U8 x9 V$ r- ?! W, G  }[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http- ^1 D# p$ B! k3 `) N: a6 R* {
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global
, \- j- Q3 V! Q[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httplog  M) Z9 u% j: r" P
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]dontlognull; m8 A4 m: E* s, s- O# k
[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. X+ L( x0 T3 X: `
[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
- b, g9 |. `4 {& M( f! N[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]redispatch6 E$ N3 w3 q$ i
[color=rgb(220, 220, 220) !important]retries[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3
# ?+ a: y' N2 n( H8 G# O[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
( f: n- L( c# G: o+ T# r9 W[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
( s9 Z/ y* _( G' Z[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
& S: M2 u' q8 |4 s  l5 g3 g" N[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]1m5 ~+ c, C0 F, \. m' |
[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" R4 R  S9 P  z" g5 y3 k  _; s
[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 B2 X" e6 m; \5 e, f% I# J) U  y
[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
- z: p% k: h+ q" Z, u9 i$ H- D4 L' f[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]30000
7 A0 i/ w  O9 n' g6 @1 z[color=rgb(255, 128, 0) !important]listen [color=rgb(255, 128, 0) !important]stats
- X0 j& _- C3 V* n/ O! @8 _[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http
6 |5 k$ h6 _) x  i8 l) c[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! Q; ~; o& t# U: [+ w+ w- `
[color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable' z3 X0 V/ J# @: p' H
[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
1 N7 i0 `/ k' O% h2 X2 f  n/ `/ T  J[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  {3 o2 J# |( G. e) W7 c/ h; R0 e
[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  e7 L, f) C& I9 A! K* H
[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
+ I! t9 Y' v9 U3 E4 l" Z+ m8 T[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
% ^% [1 Y/ k$ W[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]in3 H3 @( y, [1 |8 k; Q' o1 I
[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]80
4 c0 _2 X+ g, z& |[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http
0 s  K% L+ q8 O+ D  ]: ][color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global! g; ]7 R! N2 n5 _- `
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpclose. E% O2 Q0 b7 k+ X$ K
[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]#不等待响应结束就记录日志,表示提前记录日志,一般日志会记录响应时长,此不记录响应时长+ A, D7 N  a: }- ~8 ~
[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]#不记录空信息
$ c: q) V5 L  w/ ]8 ]; {0 i* R[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个字符
& [. F+ m# R) W' S[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跳转引用,就是上一级
4 H7 l5 N7 R7 a2 |9 q[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]servers) X9 X( Z0 |, U
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]healthcheck
1 f: I  v3 A; X7 Y0 P9 ~6 \[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]#定义外部检测机制5 l1 l6 ?1 B0 `  J! ^9 \6 l
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http* G/ n# b. C0 u
[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httpclose
) {* U# e( d% o; m% _( F  ^1 l- O[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]forwardfor! C2 L( Q1 l: G; n9 q
[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]servers2 l$ z3 c( U6 {9 G% N
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]servers
+ h9 o. v3 `. x  T6 G8 t[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]roundrobin
8 ]- i! p- _0 k6 n, }/ I" k; |. X  ~[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
. V3 f. |: z9 b  X  I+ P) A- _[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  a+ s4 \% E& {
5 s% q- L8 Z7 x/ N' W
- \4 m1 c. m8 p( ?+ ~5 ?" t

% Y$ D; x' o/ l7 x1 \0 f+ }* |5 [; i8 U" L9 J+ Z
负载均衡MySQL服务的配置示例
8 Q* d+ M- ]* B3 ?4 G- u' e. d! e6 G$ E& Z2 @1 n
1
0 y' O: O; R! T) b+ s
2
' s2 {6 w9 @6 y$ p; r( `
3
" q7 R! X% A& g# x( B' C5 `
4

* d  q1 P! T) l
5
  W& I; E6 V; w6 Y1 L7 z. W& B2 d
6

0 ?, K; q* G1 v7 P1 U0 V3 @
7

& I, g! I7 O. |) w% N& p8 e
8
6 R& w8 Z' F9 _; ^
9

' P! G1 R* K: v
10

$ m) u  t2 D: Y  X8 {4 d: p
11

; o9 _' N$ m4 G: r2 R0 D5 [! E! M
12

  l2 G, A# G6 C( i$ s6 y6 ?
13
. A1 Q; z# d- p
14
0 H; T( @: {7 P( C- F, K
15
8 _+ W" V% z8 e
16
- u' h2 k1 z# D. K& |& B
17
: f6 L& D% D3 A9 e: ^
18
$ S& m7 V$ m% ~( M3 x9 e
19
/ p3 [# \0 R2 c0 x% Y( U
20

8 |  r! i# l% |  h+ L0 _0 A
21
$ H$ O7 v. t' J1 ^# g3 |. I
22
+ y$ I- Y. l' _& ^; H
23
% B3 T2 Q* I# c& ], b. Y' i! L
24

2 F' k; L' N2 ^4 k: g  G; Y6 W
25
; T7 a, ^3 q6 t1 G
26
& u6 }) f/ R, D2 Q! O) {8 B* [" E
27
2 X9 @2 d- ^% @
28
) k+ |3 H+ E) v# O  i
29

3 `4 M& }# e3 ^6 r; t7 h
30

8 R/ b- H) H/ X8 o# r4 t; q
31
! }! t6 K% V) d
32

! u; A- \, ^4 M. S" t
33
# ]! n1 {3 v5 j. v  m
34

. f; q) O9 B* I! X' [& n9 c
35
3 L' p3 @7 }* `& E
36
. j7 M  E: R- ^4 O% ^* E( J% I2 @
37

2 K* s' A, j. A. N% J" C
38
- t, U# ~/ w8 m3 S5 l* ~
39

3 f2 v% L) s! T$ i0 z8 D2 Y
40
) N# |7 o2 l% c1 O9 s: {. X
41
3 c  B8 N5 p; B/ W6 |
42

+ z) k6 e. B7 r# ?
43
4 M. M# a- g* J, J9 S5 C2 \
44

& z& u8 p5 [2 b& f
45
4 G$ q) ~# [7 `  \/ `+ W
46

# Y% N0 [0 o- v3 j
47
) y( p9 Y% Z- @5 j
48
; G- G# f5 G1 b' ]) n
49
$ [- @6 o3 ^( o% Y  `4 g4 y
50
1 ^' G6 R& j3 |  L4 D9 j- f% W
51
8 a0 e- l* e. ]
52

9 J& Z$ q$ V9 ^% v  C
53
. [% N5 O3 o1 o- |2 a* w
54
. l; W+ i* Q# }5 ~# l; v; R
55

" v8 {7 W, j5 }
56
+ W$ X9 R& g2 ^* n# F1 @) X! A

8 i+ Z3 b3 o5 A1 `5 i2 E; ~5 T
[color=rgb(155, 155, 139) !important]#---------------------------------------------------------------------
& s6 `! D& v- V! a/ s' |$ o/ \[color=rgb(155, 155, 139) !important]# Global settings9 n" r, Z, d( _2 b8 ~1 _
[color=rgb(155, 155, 139) !important]#---------------------------------------------------------------------
2 r! A; e# k6 t1 n7 y2 ^" @[color=rgb(86, 156, 214) !important]global' i3 L; y6 t% T; ^) w: L0 W
[color=rgb(155, 155, 139) !important]# to have these messages end up in /var/log/haproxy.log you will
2 d) x' o* s0 z1 z% ~; |5 ~[color=rgb(155, 155, 139) !important]# need to:* @( ?1 X# r( K6 H; R
[color=rgb(155, 155, 139) !important]#
: @( o/ P! W; j: o& d: l2 t[color=rgb(155, 155, 139) !important]# 1) configure syslog to accept network log events. This is done
9 v; `1 @) w" X" u3 P[color=rgb(155, 155, 139) !important]# by adding the '-r' option to the SYSLOGD_OPTIONS in8 j- i/ p! ?/ I& j+ U
[color=rgb(155, 155, 139) !important]# /etc/sysconfig/syslog
( A: Z. C+ V7 N2 [, p7 ]7 V/ S. n+ n[color=rgb(155, 155, 139) !important]#
3 l+ _8 [* o. C1 ^% r[color=rgb(155, 155, 139) !important]# 2) configure local2 events to go to the /var/log/haproxy.log7 h* w4 N6 k: Z" D
[color=rgb(155, 155, 139) !important]# file. A line like the following can be added to
* f+ M' H& `* A[color=rgb(155, 155, 139) !important]# /etc/sysconfig/syslog, I6 a. S# X% \0 T. b: ~4 x2 }
[color=rgb(155, 155, 139) !important]#
. J% o  ~3 r/ Z! J[color=rgb(155, 155, 139) !important]# local2.* /var/log/haproxy.log3 P- o* K, b' U
[color=rgb(155, 155, 139) !important]#" j* d- m) y* d0 N. D0 t
[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
' S; P! o8 E+ f! G) x! |; Q[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
+ D8 Q1 o9 `, b/ q[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]pid9 V( l, x7 N: U$ O1 A: \- H1 E
[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]4000
+ F& H2 K2 i) T. O+ c[color=rgb(255, 128, 0) !important]user [color=rgb(255, 128, 0) !important]haproxy4 R$ q- I* C. C2 `
[color=rgb(255, 128, 0) !important]group [color=rgb(255, 128, 0) !important]haproxy! `1 V$ m& d( D$ I2 z2 x  _
[color=rgb(255, 128, 0) !important]daemon0 G3 c8 e1 Q0 J; I; L
[color=rgb(255, 128, 0) !important]defaults& R3 q5 a9 b, U8 C5 ~
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]tcp# K# \6 H6 f3 x& |6 U; P4 W3 x) v7 j
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global
+ f# J: {4 B; r[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]httplog
- v; r) z+ E9 U. }8 F- G+ U. O) O+ W2 [* ?[color=rgb(255, 128, 0) !important]option [color=rgb(255, 128, 0) !important]dontlognull
3 L8 @0 `( r8 d: W# W[color=rgb(220, 220, 220) !important]retries[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]3
  J! g; n$ e0 n+ F[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- ^) {3 i$ k- U2 Z
[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 T! x. }/ s( H; ~2 s[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]10s0 H7 p; g8 P7 r
[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
* J' n9 T3 {4 i' {' w3 m) A[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
5 ]9 o/ ]( M2 y4 F3 Q0 [[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
$ h0 b' l; ~2 {  z4 k[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' A" l) b5 ]* u$ P
[color=rgb(220, 220, 220) !important]maxconn[color=rgb(0, 111, 224) !important] [color=rgb(231, 163, 122) !important]600
' a4 F, R) ^) g# V[color=rgb(255, 128, 0) !important]listen [color=rgb(255, 128, 0) !important]stats
. w6 ?/ i1 V9 d[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]http
& P0 n2 D) X$ f* @* }[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
% ?9 b/ N7 H7 F' C0 ][color=rgb(255, 128, 0) !important]stats [color=rgb(255, 128, 0) !important]enable* T3 K" o5 h* O8 j; o' E- `3 L3 u
[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
9 ~4 ]# ^8 O, D) b2 k6 y9 I[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+ R& P6 U" e1 `+ h% C
[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
& U# }0 W0 w# x' m/ [, i7 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(255, 128, 0) !important]admin
# T; Z3 M0 a( R5 d- C# ^7 i7 {[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: V0 G1 C5 A. K: g! w/ Y5 {+ T; G6 p
[color=rgb(255, 128, 0) !important]frontend [color=rgb(255, 128, 0) !important]mysql: e- q) b4 o! Y* _
[color=rgb(255, 128, 0) !important]bind *[color=rgb(218, 218, 218) !important]:[color=rgb(231, 163, 122) !important]3306  X5 c5 G0 D) }. C
[color=rgb(255, 128, 0) !important]mode [color=rgb(255, 128, 0) !important]tcp$ m5 v$ G$ q# s- m! X4 q7 c
[color=rgb(255, 128, 0) !important]log [color=rgb(86, 156, 214) !important]global
0 Z0 \$ @) A# U5 ?0 f[color=rgb(255, 128, 0) !important]default_backend [color=rgb(255, 128, 0) !important]mysqlservers8 @* B6 q+ }3 I# L! j* h
[color=rgb(255, 128, 0) !important]backend [color=rgb(255, 128, 0) !important]mysqlservers, A. f. ~) o; D
[color=rgb(255, 128, 0) !important]balance [color=rgb(255, 128, 0) !important]leastconn+ |8 I' ~- F3 Z
[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
8 q6 T; t6 M' [1 Y: ]% J[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]300
' u" }7 n  \5 V7 `
/ M- e  T5 v& H- `
0 C- K$ k' ?, s

; x( w  S& z6 _% @/ |* M9 g
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

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

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

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

GMT+8, 2026-4-8 21:33 , Processed in 0.138895 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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