blob: 98a36789d9349bed9dbbda9ed7baba410398c17b [file] [log] [blame]
willy tarreau0174f312005-12-18 01:02:42 +01001 -------------------
Willy Tarreau94b45912006-05-31 06:40:15 +02002 HAProxy
willy tarreau0174f312005-12-18 01:02:42 +01003 Reference Manual
4 -------------------
Willy Tarreau2272dc12006-09-03 10:19:38 +02005 version 1.3.2
willy tarreauc5f73ed2005-12-18 01:26:38 +01006 willy tarreau
Willy Tarreau2272dc12006-09-03 10:19:38 +02007 2006/09/03
willy tarreaueedaa9f2005-12-17 14:08:03 +01008
9============
10| Abstract |
11============
12
Willy Tarreau94b45912006-05-31 06:40:15 +020013HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high
willy tarreaueedaa9f2005-12-17 14:08:03 +010014availability environments. Indeed, it can :
15 - route HTTP requests depending on statically assigned cookies ;
16 - spread the load among several servers while assuring server persistence
17 through the use of HTTP cookies ;
18 - switch to backup servers in the event a main one fails ;
19 - accept connections to special ports dedicated to service monitoring ;
20 - stop accepting connections without breaking existing ones ;
21 - add/modify/delete HTTP headers both ways ;
22 - block requests matching a particular pattern ;
willy tarreau532bb552006-05-13 18:40:37 +020023 - hold clients to the right application server depending on application
24 cookies
willy tarreau481132e2006-05-21 21:43:10 +020025 - report detailed status as HTML pages to authenticated users from an URI
26 intercepted from the application.
willy tarreaueedaa9f2005-12-17 14:08:03 +010027
28It needs very little resource. Its event-driven architecture allows it to easily
29handle thousands of simultaneous connections on hundreds of instances without
30risking the system's stability.
31
32====================
33| Start parameters |
34====================
35
36There are only a few command line options :
37
38 -f <configuration file>
39 -n <high limit for the total number of simultaneous connections>
willy tarreau532bb552006-05-13 18:40:37 +020040 = 'maxconn' in 'global' section
41 -N <high limit for the per-listener number of simultaneous connections>
42 = 'maxconn' in 'listen' or 'default' sections
willy tarreaueedaa9f2005-12-17 14:08:03 +010043 -d starts in foregreound with debugging mode enabled
44 -D starts in daemon mode
willy tarreau982249e2005-12-18 00:57:06 +010045 -q disable messages on output
46 -V displays messages on output even when -q or 'quiet' are specified.
47 -c only checks config file and exits with code 0 if no error was found, or
48 exits with code 1 if a syntax error was found.
willy tarreaufe2c5c12005-12-17 14:14:34 +010049 -p <pidfile> asks the process to write down each of its children's
50 pids to this file in daemon mode.
willy tarreau34f45302006-04-15 21:37:14 +020051 -sf specifies a list of pids to send a FINISH signal to after startup.
52 -st specifies a list of pids to send a TERMINATE signal to after startup.
willy tarreaueedaa9f2005-12-17 14:08:03 +010053 -s shows statistics (only if compiled in)
54 -l shows even more statistics (implies '-s')
willy tarreau64a3cc32005-12-18 01:13:11 +010055 -de disables use of epoll()
56 -dp disables use of poll()
willy tarreau34f45302006-04-15 21:37:14 +020057 -db disables background mode (stays in foreground, useful for debugging)
58 -m <megs> enforces a memory usage limit to a maximum of <megs> megabytes.
willy tarreaueedaa9f2005-12-17 14:08:03 +010059
willy tarreau532bb552006-05-13 18:40:37 +020060The maximal number of connections per proxy instance is used as the default
61parameter for each instance for which the 'maxconn' paramter is not set in the
62'listen' section.
willy tarreaueedaa9f2005-12-17 14:08:03 +010063
64The maximal number of total connections limits the number of connections used by
65the whole process if the 'maxconn' parameter is not set in the 'global' section.
66
67The debugging mode has the same effect as the 'debug' option in the 'global'
68section. When the proxy runs in this mode, it dumps every connections,
69disconnections, timestamps, and HTTP headers to stdout. This should NEVER
70be used in an init script since it will prevent the system from starting up.
71
willy tarreau34f45302006-04-15 21:37:14 +020072For debugging, the '-db' option is very useful as it temporarily disables
73daemon mode and multi-process mode. The service can then be stopped by simply
74pressing Ctrl-C, without having to edit the config nor run full debug.
75
willy tarreaueedaa9f2005-12-17 14:08:03 +010076Statistics are only available if compiled in with the 'STATTIME' option. It's
willy tarreau481132e2006-05-21 21:43:10 +020077only used during code optimization phases, and will soon disappear.
willy tarreaueedaa9f2005-12-17 14:08:03 +010078
willy tarreau532bb552006-05-13 18:40:37 +020079The '-st' and '-sf' options are used for hot reconfiguration (see below).
willy tarreau34f45302006-04-15 21:37:14 +020080
willy tarreaueedaa9f2005-12-17 14:08:03 +010081======================
82| Configuration file |
83======================
84
85Structure
86=========
87
88The configuration file parser ignores empty lines, spaces, tabs. Anything
89between a sharp ('#') not following a backslash ('\'), and the end of a line
90constitutes a comment and is ignored too.
91
92The configuration file is segmented in sections. A section begins whenever
93one of these 3 keywords are encountered :
94
95 - 'global'
96 - 'listen'
97 - 'defaults'
98
99Every parameter refer to the section beginning at the last one of these 3
100keywords.
101
102
1031) Global parameters
104====================
105
106Global parameters affect the whole process behaviour. They are all set in the
107'global' section. There may be several 'global' sections if needed, but their
108parameters will only be merged. Allowed parameters in 'global' section include
109the following ones :
110
111 - log <address> <facility> [max_level]
112 - maxconn <number>
113 - uid <user id>
114 - gid <group id>
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200115 - user <user name>
116 - group <group name>
willy tarreaueedaa9f2005-12-17 14:08:03 +0100117 - chroot <directory>
118 - nbproc <number>
119 - daemon
120 - debug
willy tarreau64a3cc32005-12-18 01:13:11 +0100121 - noepoll
122 - nopoll
willy tarreaueedaa9f2005-12-17 14:08:03 +0100123 - quiet
willy tarreaufe2c5c12005-12-17 14:14:34 +0100124 - pidfile <file>
willy tarreauc5f73ed2005-12-18 01:26:38 +0100125 - ulimit-n <number>
willy tarreau598da412005-12-18 01:07:29 +0100126 - stats
willy tarreaueedaa9f2005-12-17 14:08:03 +0100127
willy tarreauc5f73ed2005-12-18 01:26:38 +0100128
willy tarreaueedaa9f2005-12-17 14:08:03 +01001291.1) Event logging
130------------------
131Most events are logged : start, stop, servers going up and down, connections and
132errors. Each event generates a syslog message which can be sent to up to 2
133servers. The syntax is :
134
135 log <ip_address> <facility> [max_level]
136
137Connections are logged at level "info". Services initialization and servers
138going up are logged at level "notice", termination signals are logged at
139"warning", and definitive service termination, as well as loss of servers are
140logged at level "alert". The optional parameter <max_level> specifies above
141what level messages should be sent. Level can take one of these 8 values :
142
143 emerg, alert, crit, err, warning, notice, info, debug
144
145For backwards compatibility with versions 1.1.16 and earlier, the default level
146value is "debug" if not specified.
147
148Permitted facilities are :
149 kern, user, mail, daemon, auth, syslog, lpr, news,
150 uucp, cron, auth2, ftp, ntp, audit, alert, cron2,
151 local0, local1, local2, local3, local4, local5, local6, local7
152
153According to RFC3164, messages are truncated to 1024 bytes before being emitted.
154
155Example :
156---------
157 global
willy tarreauc5f73ed2005-12-18 01:26:38 +0100158 log 192.168.2.200 local3
159 log 127.0.0.1 local4 notice
160
willy tarreaueedaa9f2005-12-17 14:08:03 +0100161
1621.2) limiting the number of connections
163---------------------------------------
164It is possible and recommended to limit the global number of per-process
willy tarreauc5f73ed2005-12-18 01:26:38 +0100165connections using the 'maxconn' global keyword. Since one connection includes
166both a client and a server, it means that the max number of TCP sessions will
167be about the double of this number. It's important to understand this when
168trying to find best values for 'ulimit -n' before starting the proxy. To
169anticipate the number of sockets needed, all these parameters must be counted :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100170
171 - 1 socket per incoming connection
172 - 1 socket per outgoing connection
173 - 1 socket per address/port/proxy tuple.
174 - 1 socket per server being health-checked
175 - 1 socket for all logs
176
177In simple configurations where each proxy only listens one one address/port,
willy tarreauc5f73ed2005-12-18 01:26:38 +0100178set the limit of file descriptors (ulimit -n) to
179(2 * maxconn + nbproxies + nbservers + 1). Starting with versions 1.1.32/1.2.6,
180it is now possible to set the limit in the configuration using the 'ulimit-n'
181global keyword, provided the proxy is started as root. This puts an end to the
182recurrent problem of ensuring that the system limits are adapted to the proxy
183values. Note that these limits are per-process.
184
185Example :
186---------
187 global
188 maxconn 32000
189 ulimit-n 65536
190
willy tarreaueedaa9f2005-12-17 14:08:03 +0100191
1921.3) Drop of priviledges
193------------------------
194In order to reduce the risk and consequences of attacks, in the event where a
195yet non-identified vulnerability would be successfully exploited, it's possible
196to lower the process priviledges and even isolate it in a riskless directory.
197
198In the 'global' section, the 'uid' parameter sets a numerical user identifier
199which the process will switch to after binding its listening sockets. The value
200'0', which normally represents the super-user, here indicates that the UID must
201not change during startup. It's the default behaviour. The 'gid' parameter does
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200202the same for the group identifier. If setting an uid is not possible because of
203deployment constraints, it is possible to set a user name with the 'user'
204keyword followed by a valid user name. The same is true for the gid. It is
205possible to specify a group name after the 'group' keyword.
206
207It is particularly advised against use of generic accounts such as 'nobody'
208because it has the same consequences as using 'root' if other services use
209them.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100210
211The 'chroot' parameter makes the process isolate itself in an empty directory
212just before switching its UID. This type of isolation (chroot) can sometimes
213be worked around on certain OS (Linux, Solaris), provided that the attacker
214has gained 'root' priviledges and has the ability to use or create a directory.
215For this reason, it's capital to use a dedicated directory and not to share one
216between several services of different nature. To make isolation more resistant,
217it's recommended to use an empty directory without any right, and to change the
218UID of the process so that it cannot do anything there.
219
220Note: in the event where such a vulnerability would be exploited, it's most
221likely that first attempts would kill the process due to 'Segmentation Fault',
222'Bus Error' or 'Illegal Instruction' signals. Eventhough it's true that
223isolating the server reduces the risks of intrusion, it's sometimes useful to
224find why a process dies, via the analysis of a 'core' file, although very rare
225(the last bug of this sort was fixed in 1.1.9). For security reasons, most
226systems disable the generation of core file when a process changes its UID. So
227the two workarounds are either to start the process from a restricted user
228account, which will not be able to chroot itself, or start it as root and not
229change the UID. In both cases the core will be either in the start or the chroot
230directories. Do not forget to allow core dumps prior to start the process :
231
232# ulimit -c unlimited
233
234Example :
235---------
236
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200237 # with uid/gid
willy tarreaueedaa9f2005-12-17 14:08:03 +0100238 global
willy tarreauc5f73ed2005-12-18 01:26:38 +0100239 uid 30000
240 gid 30000
241 chroot /var/chroot/haproxy
242
Willy Tarreau95c20ac2007-03-25 15:39:23 +0200243 # with user/group
244 global
245 user haproxy
246 group public
247 chroot /var/chroot/haproxy
248
willy tarreaueedaa9f2005-12-17 14:08:03 +0100249
2501.4) Startup modes
251------------------
willy tarreau34f45302006-04-15 21:37:14 +0200252The service can start in several different modes :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100253 - foreground / background
254 - quiet / normal / debug
255
256The default mode is normal, foreground, which means that the program doesn't
257return once started. NEVER EVER use this mode in a system startup script, or
258the system won't boot. It needs to be started in background, so that it
259returns immediately after forking. That's accomplished by the 'daemon' option
260in the 'global' section, which is the equivalent of the '-D' command line
261argument.
262
willy tarreau34f45302006-04-15 21:37:14 +0200263The '-db' command line argument overrides the 'daemon' and 'nbproc' global
264options to make the process run in normal, foreground mode.
265
willy tarreaueedaa9f2005-12-17 14:08:03 +0100266Moreover, certain alert messages are still sent to the standard output even
267in 'daemon' mode. To make them disappear, simply add the 'quiet' option in the
268'global' section. This option has no command-line equivalent.
269
270Last, the 'debug' mode, enabled with the 'debug' option in the 'global' section,
271and which is equivalent of the '-d' option, allows deep TCP/HTTP analysis, with
272timestamped display of each connection, disconnection, and HTTP headers for both
273ways. This mode is incompatible with 'daemon' and 'quiet' modes for obvious
274reasons.
275
willy tarreauc5f73ed2005-12-18 01:26:38 +0100276
willy tarreaueedaa9f2005-12-17 14:08:03 +01002771.5) Increasing the overall processing power
278--------------------------------------------
279On multi-processor systems, it may seem to be a shame to use only one processor,
willy tarreau982249e2005-12-18 00:57:06 +0100280eventhough the load needed to saturate a recent processor is far above common
willy tarreaueedaa9f2005-12-17 14:08:03 +0100281usage. Anyway, for very specific needs, the proxy can start several processes
282between which the operating system will spread the incoming connections. The
283number of processes is controlled by the 'nbproc' parameter in the 'global'
willy tarreau4302f492005-12-18 01:00:37 +0100284section. It defaults to 1, and obviously works only in 'daemon' mode. One
285typical usage of this parameter has been to workaround the default per-process
286file-descriptor limit that Solaris imposes to user processes.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100287
288Example :
289---------
290
291 global
willy tarreauc5f73ed2005-12-18 01:26:38 +0100292 daemon
293 quiet
294 nbproc 2
willy tarreaueedaa9f2005-12-17 14:08:03 +0100295
296
willy tarreaufe2c5c12005-12-17 14:14:34 +01002971.6) Helping process management
298-------------------------------
299Haproxy now supports the notion of pidfile. If the '-p' command line argument,
300or the 'pidfile' global option is followed with a file name, this file will be
301removed, then filled with all children's pids, one per line (only in daemon
302mode). This file is NOT within the chroot, which allows to work with a readonly
303 chroot. It will be owned by the user starting the process, and will have
304permissions 0644.
305
306Example :
307---------
308
309 global
310 daemon
311 quiet
willy tarreauc5f73ed2005-12-18 01:26:38 +0100312 nbproc 2
willy tarreaufe2c5c12005-12-17 14:14:34 +0100313 pidfile /var/run/haproxy-private.pid
314
315 # to stop only those processes among others :
316 # kill $(</var/run/haproxy-private.pid)
317
willy tarreau34f45302006-04-15 21:37:14 +0200318 # to reload a new configuration with minimal service impact and without
319 # breaking existing sessions :
320 # haproxy -f haproxy.cfg -p $(</var/run/haproxy-private.pid) -st $(</var/run/haproxy-private.pid)
willy tarreaufe2c5c12005-12-17 14:14:34 +0100321
willy tarreau64a3cc32005-12-18 01:13:11 +01003221.7) Polling mechanisms
323-----------------------
324Starting from version 1.2.5, haproxy supports the poll() and epoll() polling
325mechanisms. On systems where select() is limited by FD_SETSIZE (like Solaris),
326poll() can be an interesting alternative. Performance tests show that Solaris'
327poll() performance does not decay as fast as the numbers of sockets increase,
328making it a safe solution for high loads. However, Solaris already uses poll()
329to emulate select(), so as long as the number of sockets has no reason to go
330higher than FD_SETSIZE, poll() should not provide any better performance. On
331Linux systems with the epoll() patch (or any 2.6 version), haproxy will use
332epoll() which is extremely fast and non dependant on the number of sockets.
333Tests have shown constant performance from 1 to 20000 simultaneous sessions.
334
335Haproxy will use epoll() when available, and will fall back to poll(), then to
336select(). However, if for any reason you need to disable epoll() or poll() (eg.
337because of a bug or just to compare performance), two new global options have
338been created for this matter : 'noepoll' and 'nopoll'.
339
340Example :
341---------
342
343 global
344 # use only select()
345 noepoll
346 nopoll
347
348Note :
349------
350For the sake of configuration file portability, these options are accepted but
351ignored if the poll() or epoll() mechanisms have not been enabled at compile
352time.
353
354To make debugging easier, the '-de' runtime argument disables epoll support and
355the '-dp' argument disables poll support. They are respectively equivalent to
356'noepoll' and 'nopoll'.
357
358
willy tarreaueedaa9f2005-12-17 14:08:03 +01003592) Declaration of a listening service
360=====================================
361
362Service sections start with the 'listen' keyword :
363
364 listen <instance_name> [ <IP_address>:<port_range>[,...] ]
365
366- <instance_name> is the name of the instance. This name will be reported in
367 logs, so it is good to have it reflect the proxied service. No unicity test
368 is done on this name, and it's not mandatory for it to be unique, but highly
369 recommended.
370
371- <IP_address> is the IP address the proxy binds to. Empty address, '*' and
372 '0.0.0.0' all mean that the proxy listens to all valid addresses on the
373 system.
374
375- <port_range> is either a unique port, or a port range for which the proxy will
376 accept connections for the IP address specified above. This range can be :
377 - a numerical port (ex: '80')
378 - a dash-delimited ports range explicitly stating the lower and upper bounds
379 (ex: '2000-2100') which are included in the range.
380
381 Particular care must be taken against port ranges, because every <addr:port>
382 couple consumes one socket (=a file descriptor), so it's easy to eat lots of
383 descriptors with a simple range. The <addr:port> couple must be used only once
384 among all instances running on a same system. Please note that attaching to
385 ports lower than 1024 need particular priviledges to start the program, which
386 are independant of the 'uid' parameter.
387
388- the <IP_address>:<port_range> couple may be repeated indefinitely to require
389 the proxy to listen to other addresses and/or ports. To achieve this, simply
390 separate them with a coma.
391
392Examples :
393---------
394 listen http_proxy :80
395 listen x11_proxy 127.0.0.1:6000-6009
396 listen smtp_proxy 127.0.0.1:25,127.0.0.1:587
397 listen ldap_proxy :389,:663
398
399In the event that all addresses do not fit line width, it's preferable to
400detach secondary addresses on other lines with the 'bind' keyword. If this
401keyword is used, it's not even necessary to specify the first address on the
402'listen' line, which sometimes makes multiple configuration handling easier :
403
404 bind [ <IP_address>:<port_range>[,...] ]
405
406Examples :
407----------
408 listen http_proxy
409 bind :80,:443
willy tarreauc5f73ed2005-12-18 01:26:38 +0100410 bind 10.0.0.1:10080,10.0.0.1:10443
411
willy tarreaueedaa9f2005-12-17 14:08:03 +0100412
4132.1) Inhibiting a service
414-------------------------
415A service may be disabled for maintenance reasons, without needing to comment
416out the whole section, simply by specifying the 'disabled' keyword in the
417section to be disabled :
418
419 listen smtp_proxy 0.0.0.0:25
willy tarreauc5f73ed2005-12-18 01:26:38 +0100420 disabled
willy tarreaueedaa9f2005-12-17 14:08:03 +0100421
422Note: the 'enabled' keyword allows to enable a service which has been disabled
423 previously by a default configuration.
424
willy tarreauc5f73ed2005-12-18 01:26:38 +0100425
willy tarreaueedaa9f2005-12-17 14:08:03 +01004262.2) Modes of operation
427-----------------------
428A service can work in 3 different distinct modes :
429 - TCP
430 - HTTP
willy tarreau532bb552006-05-13 18:40:37 +0200431 - health
willy tarreaueedaa9f2005-12-17 14:08:03 +0100432
433TCP mode
434--------
435In this mode, the service relays TCP connections as soon as they're established,
436towards one or several servers. No processing is done on the stream. It's only
437an association of source(addr:port) -> destination(addr:port). To use this mode,
438you must specify 'mode tcp' in the 'listen' section. This is the default mode.
439
440Example :
441---------
442 listen smtp_proxy 0.0.0.0:25
willy tarreauc5f73ed2005-12-18 01:26:38 +0100443 mode tcp
willy tarreaueedaa9f2005-12-17 14:08:03 +0100444
445HTTP mode
446---------
447In this mode, the service relays TCP connections towards one or several servers,
448when it has enough informations to decide, which normally means that all HTTP
449headers have been read. Some of them may be scanned for a cookie or a pattern
450matching a regex. To use this mode, specify 'mode http' in the 'listen' section.
451
452Example :
453---------
454 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100455 mode http
willy tarreaueedaa9f2005-12-17 14:08:03 +0100456
457Health-checking mode
458--------------------
459This mode provides a way for external components to check the proxy's health.
460It is meant to be used with intelligent load-balancers which can use send/expect
461scripts to check for all of their servers' availability. This one simply accepts
willy tarreau197e8ec2005-12-17 14:10:59 +0100462the connection, returns the word 'OK' and closes it. If the 'option httpchk' is
463set, then the reply will be 'HTTP/1.0 200 OK' with no data, so that it can be
464tested from a tool which supports HTTP health-checks. To enable it, simply
willy tarreaueedaa9f2005-12-17 14:08:03 +0100465specify 'health' as the working mode :
466
467Example :
468---------
willy tarreau197e8ec2005-12-17 14:10:59 +0100469 # simple response : 'OK'
willy tarreaueedaa9f2005-12-17 14:08:03 +0100470 listen health_check 0.0.0.0:60000
willy tarreauc5f73ed2005-12-18 01:26:38 +0100471 mode health
willy tarreaueedaa9f2005-12-17 14:08:03 +0100472
willy tarreau197e8ec2005-12-17 14:10:59 +0100473 # HTTP response : 'HTTP/1.0 200 OK'
474 listen http_health_check 0.0.0.0:60001
willy tarreauc5f73ed2005-12-18 01:26:38 +0100475 mode health
476 option httpchk
477
willy tarreau532bb552006-05-13 18:40:37 +02004782.2.1 Monitoring
479----------------
willy tarreauc5f73ed2005-12-18 01:26:38 +0100480Versions 1.1.32 and 1.2.6 provide a new solution to check the proxy's
481availability without perturbating the service. The 'monitor-net' keyword was
482created to specify a network of equipments which CANNOT use the service for
483anything but health-checks. This is particularly suited to TCP proxies, because
484it prevents the proxy from relaying the monitor's connection to the remote
485server.
486
487When used with TCP, the connection is accepted then closed and nothing is
488logged. This is enough for a front-end load-balancer to detect the service as
489available.
willy tarreau197e8ec2005-12-17 14:10:59 +0100490
willy tarreauc5f73ed2005-12-18 01:26:38 +0100491When used with HTTP, the connection is accepted, nothing is logged, the
492following response is sent, then the session is closed : "HTTP/1.0 200 OK".
493This is normally enough for any front-end HTTP load-balancer to detect the
494service as available too, both with TCP and HTTP checks.
495
496Proxies using the "monitor-net" keyword can remove the "option dontlognull", as
497it will make them log empty connections from hosts outside the monitoring
498network.
499
500Example :
501---------
502
503 listen tse-proxy
504 bind :3389,:1494,:5900 # TSE, ICA and VNC at once.
505 mode tcp
506 balance roundrobin
507 server tse-farm 192.168.1.10
508 monitor-net 192.168.1.252/31 # L4 load-balancers on .252 and .253
509
willy tarreaueedaa9f2005-12-17 14:08:03 +0100510
Willy Tarreau1c47f852006-07-09 08:22:27 +0200511When the system executing the checks is located behind a proxy, the monitor-net
512keyword cannot be used because haproxy will always see the proxy's address. To
513overcome this limitation, version 1.2.15 brought the 'monitor-uri' keyword. It
514defines an URI which will not be forwarded nor logged, but for which haproxy
515will immediately send an "HTTP/1.0 200 OK" response. This makes it possible to
516check the validity of the reverse-proxy->haproxy chain with one request. It can
517be used in HTTPS checks in front of an stunnel -> haproxy combination for
518instance. Obviously, this keyword is only valid in HTTP mode, otherwise there
519is no notion of URI. Note that the method and HTTP versions are simply ignored.
520
521Example :
522---------
523
524 listen stunnel_backend :8080
525 mode http
526 balance roundrobin
527 server web1 192.168.1.10:80 check
528 server web2 192.168.1.11:80 check
529 monitor-uri /haproxy_test
530
531
willy tarreaueedaa9f2005-12-17 14:08:03 +01005322.3) Limiting the number of simultaneous connections
533----------------------------------------------------
534The 'maxconn' parameter allows a proxy to refuse connections above a certain
535amount of simultaneous ones. When the limit is reached, it simply stops
536listening, but the system may still be accepting them because of the back log
willy tarreau982249e2005-12-18 00:57:06 +0100537queue. These connections will be processed later when other ones have freed
willy tarreaueedaa9f2005-12-17 14:08:03 +0100538some slots. This provides a serialization effect which helps very fragile
willy tarreau34f45302006-04-15 21:37:14 +0200539servers resist to high loads. See further for system limitations.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100540
541Example :
542---------
543 listen tiny_server 0.0.0.0:80
544 maxconn 10
545
546
5472.4) Soft stop
548--------------
549It is possible to stop services without breaking existing connections by the
willy tarreau22739ef2006-01-20 20:43:32 +0100550sending of the SIGUSR1 signal to the process. All services are then put into
willy tarreaueedaa9f2005-12-17 14:08:03 +0100551soft-stop state, which means that they will refuse to accept new connections,
552except for those which have a non-zero value in the 'grace' parameter, in which
553case they will still accept connections for the specified amount of time, in
willy tarreau22739ef2006-01-20 20:43:32 +0100554milliseconds. This makes it possible to tell a load-balancer that the service
555is failing, while still doing the job during the time it needs to detect it.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100556
557Note: active connections are never killed. In the worst case, the user will have
558to wait for all of them to close or to time-out, or simply kill the process
willy tarreau22739ef2006-01-20 20:43:32 +0100559normally (SIGTERM). The default 'grace' value is '0'.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100560
561Example :
562---------
563 # enter soft stop after 'killall -USR1 haproxy'
564 # the service will still run 10 seconds after the signal
565 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100566 mode http
567 grace 10000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100568
569 # this port is dedicated to a load-balancer, and must fail immediately
570 listen health_check 0.0.0.0:60000
willy tarreauc5f73ed2005-12-18 01:26:38 +0100571 mode health
572 grace 0
willy tarreaueedaa9f2005-12-17 14:08:03 +0100573
574
willy tarreau39df2dc2006-01-29 21:56:05 +0100575As of version 1.2.8, a new soft-reconfiguration mechanism has been introduced.
willy tarreau22739ef2006-01-20 20:43:32 +0100576It is now possible to "pause" all the proxies by sending a SIGTTOU signal to
577the processes. This will disable the listening socket without breaking existing
578connections. After that, sending a SIGTTIN signal to those processes enables
579the listening sockets again. This is very useful to try to load a new
580configuration or even a new version of haproxy without breaking existing
581connections. If the load succeeds, then simply send a SIGUSR1 which will make
582the previous proxies exit immediately once their sessions are closed ; and if
583the load fails, then simply send a SIGTTIN to restore the service immediately.
584Please note that the 'grace' parameter is ignored for SIGTTOU, as well as for
585SIGUSR1 when the process was in the pause mode. Please also note that it would
586be useful to save the pidfile before starting a new instance.
587
willy tarreau34f45302006-04-15 21:37:14 +0200588This mechanism fully exploited since 1.2.11 with the '-st' and '-sf' options
willy tarreau532bb552006-05-13 18:40:37 +0200589(see below).
590
5912.4.1) Hot reconfiguration
592--------------------------
593The '-st' and '-sf' command line options are used to inform previously running
594processes that a configuration is being reloaded. They will receive the SIGTTOU
595signal to ask them to temporarily stop listening to the ports so that the new
596process can grab them. If anything wrong happens, the new process will send
597them a SIGTTIN to tell them to re-listen to the ports and continue their normal
598work. Otherwise, it will either ask them to finish (-sf) their work then softly
599exit, or immediately terminate (-st), breaking existing sessions. A typical use
600of this allows a configuration reload without service interruption :
601
602 # haproxy -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)
603
willy tarreau22739ef2006-01-20 20:43:32 +0100604
willy tarreaueedaa9f2005-12-17 14:08:03 +01006052.5) Connections expiration time
606--------------------------------
607It is possible (and recommended) to configure several time-outs on TCP
608connections. Three independant timers are adjustable with values specified
609in milliseconds. A session will be terminated if either one of these timers
610expire.
611
612 - the time we accept to wait for data from the client, or for the client to
613 accept data : 'clitimeout' :
614
willy tarreauc5f73ed2005-12-18 01:26:38 +0100615 # client time-out set to 2mn30.
616 clitimeout 150000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100617
618 - the time we accept to wait for data from the server, or for the server to
619 accept data : 'srvtimeout' :
620
willy tarreauc5f73ed2005-12-18 01:26:38 +0100621 # server time-out set to 30s.
622 srvtimeout 30000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100623
624 - the time we accept to wait for a connection to establish on a server :
625 'contimeout' :
626
627 # we give up if the connection does not complete within 4 seconds
willy tarreauc5f73ed2005-12-18 01:26:38 +0100628 contimeout 4000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100629
630Notes :
631-------
632 - 'contimeout' and 'srvtimeout' have no sense on 'health' mode servers ;
633 - under high loads, or with a saturated or defective network, it's possible
634 that some packets get lost. Since the first TCP retransmit only happens
635 after 3 seconds, a time-out equal to, or lower than 3 seconds cannot
636 compensate for a packet loss. A 4 seconds time-out seems a reasonable
637 minimum which will considerably reduce connection failures.
638
willy tarreauc5f73ed2005-12-18 01:26:38 +0100639
willy tarreaueedaa9f2005-12-17 14:08:03 +01006402.6) Attempts to reconnect
641--------------------------
642After a connection failure to a server, it is possible to retry, potentially
643on another server. This is useful if health-checks are too rare and you don't
644want the clients to see the failures. The number of attempts to reconnect is
645set by the 'retries' paramter.
646
647Example :
648---------
willy tarreauc5f73ed2005-12-18 01:26:38 +0100649 # we can retry 3 times max after a failure
650 retries 3
willy tarreaueedaa9f2005-12-17 14:08:03 +0100651
willy tarreau34f45302006-04-15 21:37:14 +0200652Please note that the reconnection attempt may lead to getting the connection
653sent to a new server if the original one died between connection attempts.
654
willy tarreaueedaa9f2005-12-17 14:08:03 +0100655
6562.7) Address of the dispatch server (deprecated)
657------------------------------------------------
658The server which will be sent all new connections is defined by the 'dispatch'
659parameter, in the form <address>:<port>. It generally is dedicated to unknown
660connections and will assign them a cookie, in case of HTTP persistence mode,
661or simply is a single server in case of generic TCP proxy. This old mode is only
662provided for backwards compatibility, but doesn't allow to check remote servers
663state, and has a rather limited usage. All new setups should switch to 'balance'
664mode. The principle of the dispatcher is to be able to perform the load
665balancing itself, but work only on new clients so that the server doesn't need
666to be a big machine.
667
668Example :
669---------
willy tarreauc5f73ed2005-12-18 01:26:38 +0100670 # all new connections go there
671 dispatch 192.168.1.2:80
willy tarreaueedaa9f2005-12-17 14:08:03 +0100672
673Note :
674------
675This parameter has no sense for 'health' servers, and is incompatible with
676'balance' mode.
677
678
6792.8) Outgoing source address
680----------------------------
681It is often necessary to bind to a particular address when connecting to some
682remote hosts. This is done via the 'source' parameter which is a per-proxy
683parameter. A newer version may allow to fix different sources to reach different
684servers. The syntax is 'source <address>[:<port>]', where <address> is a valid
685local address (or '0.0.0.0' or '*' or empty to let the system choose), and
686<port> is an optional parameter allowing the user to force the source port for
687very specific needs. If the port is not specified or is '0', the system will
688choose a free port. Note that as of version 1.1.18, the servers health checks
689are also performed from the same source.
690
691Examples :
692----------
693 listen http_proxy *:80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100694 # all connections take 192.168.1.200 as source address
695 source 192.168.1.200:0
willy tarreaueedaa9f2005-12-17 14:08:03 +0100696
697 listen rlogin_proxy *:513
willy tarreauc5f73ed2005-12-18 01:26:38 +0100698 # use address 192.168.1.200 and the reserved port 900 (needs to be root)
699 source 192.168.1.200:900
willy tarreaueedaa9f2005-12-17 14:08:03 +0100700
701
7022.9) Setting the cookie name
703----------------------------
704In HTTP mode, it is possible to look for a particular cookie which will contain
705a server identifier which should handle the connection. The cookie name is set
706via the 'cookie' parameter.
707
708Example :
709---------
710 listen http_proxy :80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100711 mode http
712 cookie SERVERID
willy tarreaueedaa9f2005-12-17 14:08:03 +0100713
714It is possible to change the cookie behaviour to get a smarter persistence,
715depending on applications. It is notably possible to delete or modify a cookie
716emitted by a server, insert a cookie identifying the server in an HTTP response
717and even add a header to tell upstream caches not to cache this response.
718
719Examples :
720----------
721
722To remove the cookie for direct accesses (ie when the server matches the one
723which was specified in the client cookie) :
724
willy tarreauc5f73ed2005-12-18 01:26:38 +0100725 cookie SERVERID indirect
willy tarreaueedaa9f2005-12-17 14:08:03 +0100726
727To replace the cookie value with the one assigned to the server if any (no
728cookie will be created if the server does not provide one, nor if the
729configuration does not provide one). This lets the application put the cookie
730exactly on certain pages (eg: successful authentication) :
731
willy tarreauc5f73ed2005-12-18 01:26:38 +0100732 cookie SERVERID rewrite
willy tarreaueedaa9f2005-12-17 14:08:03 +0100733
734To create a new cookie and assign the server identifier to it (in this case, all
735servers should be associated with a valid cookie, since no cookie will simply
736delete the cookie from the client's browser) :
737
willy tarreauc5f73ed2005-12-18 01:26:38 +0100738 cookie SERVERID insert
willy tarreaueedaa9f2005-12-17 14:08:03 +0100739
willy tarreau0174f312005-12-18 01:02:42 +0100740To reuse an existing application cookie and prefix it with the server's
741identifier, and remove it in the request, use the 'prefix' option. This allows
742to insert a haproxy in front of an application without risking to break clients
743which does not support more than one cookie :
744
willy tarreauc5f73ed2005-12-18 01:26:38 +0100745 cookie JSESSIONID prefix
willy tarreau0174f312005-12-18 01:02:42 +0100746
willy tarreaueedaa9f2005-12-17 14:08:03 +0100747To insert a cookie and ensure that no upstream cache will store it, add the
748'nocache' option :
749
willy tarreauc5f73ed2005-12-18 01:26:38 +0100750 cookie SERVERID insert nocache
willy tarreaueedaa9f2005-12-17 14:08:03 +0100751
752To insert a cookie only after a POST request, add 'postonly' after 'insert'.
753This has the advantage that there's no risk of caching, and that all pages
754seen before the POST one can still be cached :
755
willy tarreauc5f73ed2005-12-18 01:26:38 +0100756 cookie SERVERID insert postonly
willy tarreaueedaa9f2005-12-17 14:08:03 +0100757
758Notes :
759-----------
760- it is possible to combine 'insert' with 'indirect' or 'rewrite' to adapt to
761 applications which already generate the cookie with an invalid content.
762
763- in the case where 'insert' and 'indirect' are both specified, the cookie is
willy tarreau0174f312005-12-18 01:02:42 +0100764 never transmitted to the server, since it wouldn't understand it. This is the
765 most application-transparent mode.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100766
767- it is particularly recommended to use 'nocache' in 'insert' mode if any
768 upstream HTTP/1.0 cache is susceptible to cache the result, because this may
769 lead to many clients going to the same server, or even worse, some clients
770 having their server changed while retrieving a page from the cache.
771
willy tarreau0174f312005-12-18 01:02:42 +0100772- the 'prefix' mode normally does not need 'indirect', 'nocache', nor
773 'postonly', because just as in the 'rewrite' mode, it relies on the
774 application to know when a cookie can be emitted. However, since it has to
775 fix the cookie name in every subsequent requests, you must ensure that the
776 proxy will be used without any "HTTP keep-alive". Use option "httpclose" if
777 unsure.
778
willy tarreaueedaa9f2005-12-17 14:08:03 +0100779- when the application is well known and controlled, the best method is to
780 only add the persistence cookie on a POST form because it's up to the
willy tarreau0174f312005-12-18 01:02:42 +0100781 application to select which page it wants the upstream servers to cache. In
782 this case, you would use 'insert postonly indirect'.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100783
willy tarreauc5f73ed2005-12-18 01:26:38 +0100784
willy tarreaueedaa9f2005-12-17 14:08:03 +01007852.10) Associating a cookie value with a server
786----------------------------------------------
787In HTTP mode, it's possible to associate a cookie value to each server. This
788was initially used in combination with 'dispatch' mode to handle direct accesses
789but it is now the standard way of doing the load balancing. The syntax is :
790
791 server <identifier> <address>:<port> cookie <value>
792
793- <identifier> is any name which can be used to identify the server in the logs.
794- <address>:<port> specifies where the server is bound.
795- <value> is the value to put in or to read from the cookie.
796
797Example : the 'SERVERID' cookie can be either 'server01' or 'server02'
798---------
799 listen http_proxy :80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100800 mode http
801 cookie SERVERID
802 dispatch 192.168.1.100:80
803 server web1 192.168.1.1:80 cookie server01
804 server web2 192.168.1.2:80 cookie server02
willy tarreaueedaa9f2005-12-17 14:08:03 +0100805
806Warning : the syntax has changed since version 1.0 !
807---------
808
willy tarreauc5f73ed2005-12-18 01:26:38 +0100809
willy tarreau598da412005-12-18 01:07:29 +01008102.11) Application Cookies
811-------------------------
812Since 1.2.4 it is possible to catch the cookie that comes from an
813application server in order to apply "application session stickyness".
814The server's response is searched for 'appsession' cookie, the first
815'len' bytes are used for matching and it is stored for a period of
816'timeout'.
817The syntax is:
818
willy tarreau532bb552006-05-13 18:40:37 +0200819 appsession <session_cookie> len <match_length> timeout <holdtime>
willy tarreau598da412005-12-18 01:07:29 +0100820
willy tarreau532bb552006-05-13 18:40:37 +0200821- <session_cookie> is the cookie, the server uses for it's session-handling
822- <match_length> how many bytes/characters should be used for matching equal
willy tarreau598da412005-12-18 01:07:29 +0100823 sessions
willy tarreau532bb552006-05-13 18:40:37 +0200824- <holdtime> after this inactivaty time, in ms, the cookie will be deleted
willy tarreau598da412005-12-18 01:07:29 +0100825 from the sessionstore
826
827The appsession is only per 'listen' section possible.
828
829Example :
830---------
willy tarreau532bb552006-05-13 18:40:37 +0200831 listen http_lb1 192.168.3.4:80
832 mode http
833 capture request header Cookie len 200
834 # Havind a ServerID cookie on the client allows him to reach
835 # the right server even after expiration of the appsession.
836 cookie ServerID insert nocache indirect
837 # Will memorize 52 bytes of the cookie 'JSESSIONID' and keep them
838 # for 3 hours. It will match it in the cookie and the URL field.
839 appsession JSESSIONID len 52 timeout 10800000
840 server first1 10.3.9.2:10805 check inter 3000 cookie first
841 server secon1 10.3.9.3:10805 check inter 3000 cookie secon
842 server first1 10.3.9.4:10805 check inter 3000 cookie first
843 server secon2 10.3.9.5:10805 check inter 3000 cookie secon
844 option httpchk GET /test.jsp
willy tarreau598da412005-12-18 01:07:29 +0100845
willy tarreauc5f73ed2005-12-18 01:26:38 +0100846
willy tarreaueedaa9f2005-12-17 14:08:03 +01008473) Autonomous load balancer
848===========================
849
850The proxy can perform the load-balancing itself, both in TCP and in HTTP modes.
851This is the most interesting mode which obsoletes the old 'dispatch' mode
852described above. It has advantages such as server health monitoring, multiple
853port binding and port mapping. To use this mode, the 'balance' keyword is used,
willy tarreau34f45302006-04-15 21:37:14 +0200854followed by the selected algorithm. Up to version 1.2.11, only 'roundrobin' was
855available, which is also the default value if unspecified. Starting with
856version 1.2.12, a new 'source' keyword appeared. In this mode, there will be no
857dispatch address, but the proxy needs at least one server.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100858
859Example : same as the last one, with internal load balancer
860---------
861
862 listen http_proxy :80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100863 mode http
864 cookie SERVERID
865 balance roundrobin
866 server web1 192.168.1.1:80 cookie server01
867 server web2 192.168.1.2:80 cookie server02
willy tarreaueedaa9f2005-12-17 14:08:03 +0100868
869
870Since version 1.1.22, it is possible to automatically determine on which port
871the server will get the connection, depending on the port the client connected
872to. Indeed, there now are 4 possible combinations for the server's <port> field:
873
874 - unspecified or '0' :
875 the connection will be sent to the same port as the one on which the proxy
876 received the client connection itself.
877
878 - numerical value (the only one supported in versions earlier than 1.1.22) :
879 the connection will always be sent to the specified port.
880
881 - '+' followed by a numerical value :
882 the connection will be sent to the same port as the one on which the proxy
883 received the connection, plus this value.
884
885 - '-' followed by a numerical value :
886 the connection will be sent to the same port as the one on which the proxy
887 received the connection, minus this value.
888
889Examples :
890----------
891
892# same as previous example
893
894 listen http_proxy :80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100895 mode http
896 cookie SERVERID
897 balance roundrobin
898 server web1 192.168.1.1 cookie server01
899 server web2 192.168.1.2 cookie server02
willy tarreaueedaa9f2005-12-17 14:08:03 +0100900
901# simultaneous relaying of ports 80, 81 and 8080-8089
902
903 listen http_proxy :80,:81,:8080-8089
willy tarreauc5f73ed2005-12-18 01:26:38 +0100904 mode http
905 cookie SERVERID
906 balance roundrobin
907 server web1 192.168.1.1 cookie server01
908 server web2 192.168.1.2 cookie server02
willy tarreaueedaa9f2005-12-17 14:08:03 +0100909
910# relaying of TCP ports 25, 389 and 663 to ports 1025, 1389 and 1663
911
912 listen http_proxy :25,:389,:663
willy tarreauc5f73ed2005-12-18 01:26:38 +0100913 mode tcp
914 balance roundrobin
915 server srv1 192.168.1.1:+1000
916 server srv2 192.168.1.2:+1000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100917
willy tarreau34f45302006-04-15 21:37:14 +0200918As previously stated, version 1.2.12 brought the 'source' keyword. When this
919keyword is used, the client's IP address is hashed and evenly distributed among
920the available servers so that a same source IP will always go to the same
921server as long as there are no change in the number of available servers. This
922can be used for instance to bind HTTP and HTTPS to the same server. It can also
923be used to improve stickyness when one part of the client population does not
924accept cookies. In this case, only those ones will be perturbated should a
925server fail.
926
927NOTE: It is important to consider the fact that many clients surf the net
928 through proxy farms which assign different IP addresses for each
929 request. Others use dialup connections with a different IP at each
930 connection. Thus, the 'source' parameter should be used with extreme
931 care.
932
933Examples :
934----------
935
936# make a same IP go to the same server whatever the service
937
938 listen http_proxy
939 bind :80,:443
940 mode http
941 balance source
942 server web1 192.168.1.1
943 server web2 192.168.1.2
944
945# try to improve client-server binding by using both source IP and cookie :
946
947 listen http_proxy :80
948 mode http
949 cookie SERVERID
950 balance source
951 server web1 192.168.1.1 cookie server01
952 server web2 192.168.1.2 cookie server02
953
willy tarreaueedaa9f2005-12-17 14:08:03 +0100954
willy tarreau197e8ec2005-12-17 14:10:59 +01009553.1) Server monitoring
956----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +0100957It is possible to check the servers status by trying to establish TCP
958connections or even sending HTTP requests to them. A server which fails to
959reply to health checks as expected will not be used by the load balancing
960algorithms. To enable monitoring, add the 'check' keyword on a server line.
961It is possible to specify the interval between tests (in milliseconds) with
962the 'inter' parameter, the number of failures supported before declaring that
963the server has fallen down with the 'fall' parameter, and the number of valid
964checks needed for the server to fully get up with the 'rise' parameter. Since
965version 1.1.22, it is also possible to send checks to a different port
966(mandatory when none is specified) with the 'port' parameter. The default
967values are the following ones :
968
969 - inter : 2000
970 - rise : 2
971 - fall : 3
972 - port : default server port
Willy Tarreau2ea3abb2007-03-25 16:45:16 +0200973 - addr : specific address for the test (default = address server)
974
willy tarreaueedaa9f2005-12-17 14:08:03 +0100975The default mode consists in establishing TCP connections only. But in certain
976types of application failures, it is often that the server continues to accept
977connections because the system does it itself while the application is running
978an endless loop, or is completely stuck. So in version 1.1.16 were introduced
979HTTP health checks which only performed simple lightweight requests and analysed
980the response. Now, as of version 1.1.23, it is possible to change the HTTP
981method, the URI, and the HTTP version string (which even allows to send headers
982with a dirty trick). To enable HTTP health-checks, use 'option httpchk'.
983
984By default, requests use the 'OPTIONS' method because it's very light and easy
985to filter from logs, and does it on '/'. Only HTTP responses 2xx and 3xx are
986considered valid ones, and only if they come before the time to send a new
987request is reached ('inter' parameter). If some servers block this type of
988request, 3 other forms help to forge a request :
989
990 - option httpchk -> OPTIONS / HTTP/1.0
991 - option httpchk URI -> OPTIONS <URI> HTTP/1.0
992 - option httpchk METH URI -> <METH> <URI> HTTP/1.0
993 - option httpchk METH URI VER -> <METH> <URI> <VER>
994
Willy Tarreauf3c69202006-07-09 16:42:34 +0200995Some people are using HAProxy to relay various TCP-based protocols such as
996HTTPS, SMTP or LDAP, with the most common one being HTTPS. One problem commonly
997encountered in data centers is the need to forward the traffic to far remote
998servers while providing server fail-over. Often, TCP-only checks are not enough
999because intermediate firewalls, load balancers or proxies might acknowledge the
1000connection before it reaches the real server. The only solution to this problem
1001is to send application-level health checks. Since the demand for HTTPS checks
1002is high, it has been implemented in 1.2.15 based on SSLv3 Client Hello packets.
1003To enable it, use 'option ssl-hello-chk'. It will send SSL CLIENT HELLO packets
1004to the servers, announcing support for most common cipher suites. If the server
1005responds what looks like a SERVER HELLO or an ALERT (refuses the ciphers) then
1006the response is considered as valid. Note that Apache does not generate a log
1007when it receives only an HELLO message, which makes this type of message
1008perfectly suit this need.
1009
willy tarreaueedaa9f2005-12-17 14:08:03 +01001010See examples below.
1011
1012Since version 1.1.17, it is possible to specify backup servers. These servers
1013are only sollicited when no other server is available. This may only be useful
1014to serve a maintenance page, or define one active and one backup server (seldom
1015used in TCP mode). To make a server a backup one, simply add the 'backup' option
1016on its line. These servers also support cookies, so if a cookie is specified for
1017a backup server, clients assigned to this server will stick to it even when the
1018other ones come back. Conversely, if no cookie is assigned to such a server,
1019the clients will get their cookies removed (empty cookie = removal), and will
1020be balanced against other servers once they come back. Please note that there
Willy TARREAU3481c462006-03-01 22:37:57 +01001021is no load-balancing among backup servers by default. If there are several
1022backup servers, the second one will only be used when the first one dies, and
1023so on. To force load-balancing between backup servers, specify the 'allbackups'
1024option.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001025
Willy Tarreau2ea3abb2007-03-25 16:45:16 +02001026Since version 1.1.22, it is possible to send health checks to a different port
1027than the service. It is mainly needed in setups where the server does not have
1028any predefined port, for instance when the port is deduced from the listening
1029port. For this, use the 'port' parameter followed by the port number which must
1030respond to health checks. It is also possible to send health checks to a
1031different address than the service. It makes it easier to use a dedicated check
1032daemon on the servers, for instance, check return contents and stop several
1033farms at once in the event of an error anywhere.
1034
willy tarreaueedaa9f2005-12-17 14:08:03 +01001035Since version 1.1.17, it is also possible to visually check the status of all
1036servers at once. For this, you just have to send a SIGHUP signal to the proxy.
1037The servers status will be dumped into the logs at the 'notice' level, as well
1038as on <stderr> if not closed. For this reason, it's always a good idea to have
1039one local log server at the 'notice' level.
1040
willy tarreau982249e2005-12-18 00:57:06 +01001041Since version 1.1.28 and 1.2.1, if an instance loses all its servers, an
willy tarreau0174f312005-12-18 01:02:42 +01001042emergency message will be sent in the logs to inform the administator that an
willy tarreau982249e2005-12-18 00:57:06 +01001043immediate action must be taken.
1044
willy tarreau0174f312005-12-18 01:02:42 +01001045Since version 1.1.30 and 1.2.3, several servers can share the same cookie
1046value. This is particularly useful in backup mode, to select alternate paths
1047for a given server for example, to provide soft-stop, or to direct the clients
1048to a temporary page during an application restart. The principle is that when
1049a server is dead, the proxy will first look for another server which shares the
1050same cookie value for every client which presents the cookie. If there is no
1051standard server for this cookie, it will then look for a backup server which
1052shares the same name. Please consult the architecture guide for more information.
willy tarreau982249e2005-12-18 00:57:06 +01001053
willy tarreaueedaa9f2005-12-17 14:08:03 +01001054Examples :
1055----------
1056# same setup as in paragraph 3) with TCP monitoring
1057 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001058 mode http
1059 cookie SERVERID
1060 balance roundrobin
1061 server web1 192.168.1.1:80 cookie server01 check
1062 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
willy tarreaueedaa9f2005-12-17 14:08:03 +01001063
1064# same with HTTP monitoring via 'OPTIONS / HTTP/1.0'
1065 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001066 mode http
1067 cookie SERVERID
1068 balance roundrobin
1069 option httpchk
1070 server web1 192.168.1.1:80 cookie server01 check
1071 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
willy tarreaueedaa9f2005-12-17 14:08:03 +01001072
1073# same with HTTP monitoring via 'OPTIONS /index.html HTTP/1.0'
1074 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001075 mode http
1076 cookie SERVERID
1077 balance roundrobin
1078 option httpchk /index.html
1079 server web1 192.168.1.1:80 cookie server01 check
1080 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
willy tarreaueedaa9f2005-12-17 14:08:03 +01001081
1082# same with HTTP monitoring via 'HEAD /index.jsp? HTTP/1.1\r\nHost: www'
1083 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001084 mode http
1085 cookie SERVERID
1086 balance roundrobin
1087 option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www
1088 server web1 192.168.1.1:80 cookie server01 check
1089 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
willy tarreaueedaa9f2005-12-17 14:08:03 +01001090
willy tarreau0174f312005-12-18 01:02:42 +01001091# Load-balancing with 'prefixed cookie' persistence, and soft-stop using an
1092# alternate port 81 on the server for health-checks.
1093 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001094 mode http
1095 cookie JSESSIONID prefix
1096 balance roundrobin
1097 option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www
1098 server web1-norm 192.168.1.1:80 cookie s1 check port 81
1099 server web2-norm 192.168.1.2:80 cookie s2 check port 81
1100 server web1-stop 192.168.1.1:80 cookie s1 check port 80 backup
1101 server web2-stop 192.168.1.2:80 cookie s2 check port 80 backup
willy tarreau0174f312005-12-18 01:02:42 +01001102
willy tarreaueedaa9f2005-12-17 14:08:03 +01001103# automatic insertion of a cookie in the server's response, and automatic
1104# deletion of the cookie in the client request, while asking upstream caches
1105# not to cache replies.
1106 listen web_appl 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001107 mode http
1108 cookie SERVERID insert nocache indirect
1109 balance roundrobin
1110 server web1 192.168.1.1:80 cookie server01 check
1111 server web2 192.168.1.2:80 cookie server02 check
willy tarreaueedaa9f2005-12-17 14:08:03 +01001112
1113# same with off-site application backup and local error pages server
1114 listen web_appl 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001115 mode http
1116 cookie SERVERID insert nocache indirect
1117 balance roundrobin
1118 server web1 192.168.1.1:80 cookie server01 check
1119 server web2 192.168.1.2:80 cookie server02 check
1120 server web-backup 192.168.2.1:80 cookie server03 check backup
1121 server web-excuse 192.168.3.1:80 check backup
willy tarreaueedaa9f2005-12-17 14:08:03 +01001122
willy tarreauc5f73ed2005-12-18 01:26:38 +01001123# SMTP+TLS relaying with health-checks and backup servers
willy tarreaueedaa9f2005-12-17 14:08:03 +01001124
1125 listen http_proxy :25,:587
willy tarreauc5f73ed2005-12-18 01:26:38 +01001126 mode tcp
1127 balance roundrobin
1128 server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2
1129 server srv2 192.168.1.2 backup
willy tarreaueedaa9f2005-12-17 14:08:03 +01001130
Willy Tarreauf3c69202006-07-09 16:42:34 +02001131# HTTPS relaying with health-checks and backup servers
1132
1133 listen http_proxy :443
1134 mode tcp
1135 option ssl-hello-chk
1136 balance roundrobin
1137 server srv1 192.168.1.1 check inter 30000 rise 1 fall 2
1138 server srv2 192.168.1.2 backup
1139
Willy TARREAU3481c462006-03-01 22:37:57 +01001140# Load-balancing using a backup pool (requires haproxy 1.2.9)
1141 listen http_proxy 0.0.0.0:80
1142 mode http
1143 balance roundrobin
1144 option httpchk
1145 server inst1 192.168.1.1:80 cookie s1 check
1146 server inst2 192.168.1.2:80 cookie s2 check
1147 server inst3 192.168.1.3:80 cookie s3 check
1148 server back1 192.168.1.10:80 check backup
1149 server back2 192.168.1.11:80 check backup
1150 option allbackups # all backups will be used
1151
willy tarreaueedaa9f2005-12-17 14:08:03 +01001152
11533.2) Redistribute connections in case of failure
1154------------------------------------------------
1155In HTTP mode, if a server designated by a cookie does not respond, the clients
1156may definitely stick to it because they cannot flush the cookie, so they will
1157not be able to access the service anymore. Specifying 'redispatch' will allow
1158the proxy to break their persistence and redistribute them to working servers.
1159
1160Example :
1161---------
1162 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001163 mode http
1164 cookie SERVERID
1165 dispatch 192.168.1.100:80
1166 server web1 192.168.1.1:80 cookie server01
1167 server web2 192.168.1.2:80 cookie server02
1168 redispatch # send back to dispatch in case of connection failure
willy tarreaueedaa9f2005-12-17 14:08:03 +01001169
1170Up to, and including version 1.1.16, this parameter only applied to connection
1171failures. Since version 1.1.17, it also applies to servers which have been
1172detected as failed by the health check mechanism. Indeed, a server may be broken
1173but still accepting connections, which would not solve every case. But it is
1174possible to conserve the old behaviour, that is, make a client insist on trying
1175to connect to a server even if it is said to be down, by setting the 'persist'
1176option :
1177
1178 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001179 mode http
1180 option persist
1181 cookie SERVERID
1182 dispatch 192.168.1.100:80
1183 server web1 192.168.1.1:80 cookie server01
1184 server web2 192.168.1.2:80 cookie server02
1185 redispatch # send back to dispatch in case of connection failure
willy tarreaueedaa9f2005-12-17 14:08:03 +01001186
1187
willy tarreau34f45302006-04-15 21:37:14 +020011883.3) Assigning different weights to servers
1189-------------------------------------------
1190Sometimes you will need to bring new servers to increase your server farm's
1191capacity, but the new server will be either smaller (emergency use of anything
1192that fits) or bigger (when investing in new hardware). For this reason, it
1193might be wise to be able to send more clients to biggest servers. Till version
11941.2.11, it was necessary to replicate the same server multiple times in the
1195configuration. Starting with 1.2.12, the 'weight' option is available. HAProxy
1196then computes the most homogenous possible map of servers based on their
willy tarreau532bb552006-05-13 18:40:37 +02001197weights so that the load gets distributed as smoothly as possible among them.
1198The weight, between 1 and 256, should reflect one server's capacity relative to
1199others. Weight 1 represents the lowest frequency and 256 the highest. This way,
1200if a server fails, the remaining capacities are still respected.
willy tarreau34f45302006-04-15 21:37:14 +02001201
1202Example :
1203---------
1204# fair distribution among two opterons and one old pentium3
1205
1206 listen web_appl 0.0.0.0:80
1207 mode http
1208 cookie SERVERID insert nocache indirect
1209 balance roundrobin
1210 server pentium3-800 192.168.1.1:80 cookie server01 weight 8 check
1211 server opteron-2.0G 192.168.1.2:80 cookie server02 weight 20 check
1212 server opteron-2.4G 192.168.1.3:80 cookie server03 weight 24 check
1213 server web-backup1 192.168.2.1:80 cookie server04 check backup
1214 server web-excuse 192.168.3.1:80 check backup
1215
1216Notes :
1217-------
1218 - if unspecified, the default weight is 1
1219
1220 - the weight does not impact health checks, so it is cleaner to use weights
1221 than replicating the same server several times
1222
1223 - weights also work on backup servers if the 'allbackups' option is used
1224
1225 - the weights also apply to the source address load balancing
1226 ('balance source').
1227
1228 - whatever the weights, the first server will always be assigned first. This
1229 is helpful for troubleshooting.
1230
1231 - for the purists, the map calculation algorithm gives precedence to first
1232 server, so the map is the most uniform when servers are declared in
1233 ascending order relative to their weights.
1234
willy tarreau532bb552006-05-13 18:40:37 +02001235The load distribution will follow exactly this sequence :
1236
1237 Request| 1 1 1 1
1238 number | 1 2 3 4 5 6 7 8 9 0 1 2 3
1239 --------+---------------------------
1240 p3-800 | X . . . . . . X . . . . .
1241 opt-20 | . X . X . X . . . X . X .
1242 opt-24 | . . X . X . X . X . X . X
1243
1244
12453.4) Limiting the number of concurrent sessions on each server
1246--------------------------------------------------------------
1247Some pre-forked servers such as Apache suffer from too many concurrent
1248sessions, because it's very expensive to run hundreds or thousands of
1249processes on one system. One solution is to increase the number of servers
1250and load-balance between them, but it is a problem when the only goal is
1251to resist to short surges.
1252
1253To solve this problem, a new feature was implemented in HAProxy 1.2.13.
1254It's a per-server 'maxconn', associated with a per-server and a per-proxy
1255queue. This transforms haproxy into a request buffer between the thousands of
1256clients and the few servers. On many circumstances, lowering the maxconn value
1257will increase the server's performance and decrease the overall response times
1258because the servers will be less congested.
1259
1260When a request tries to reach any server, the first non-saturated server is
1261used, respective to the load balancing algorithm. If all servers are saturated,
1262then the request gets queued into the instance's global queue. It will be
1263dequeued once a server will have freed a session and all previously queued
1264requests have been processed.
1265
1266If a request references a particular server (eg: source hashing, or persistence
1267cookie), and if this server is full, then the request will be queued into the
1268server's dedicated queue. This queue has higher priority than the global queue,
1269so it's easier for already registered users to enter the site than for new
1270users.
1271
1272For this, the logs have been enhanced to show the number of sessions per
1273server, the request's position in the queue and the time spent in the queue.
1274This helps doing capacity planning. See the 'logs' section below for more info.
1275
1276Example :
1277---------
1278 # be nice with P3 which only has 256 MB of RAM.
1279 listen web_appl 0.0.0.0:80
1280 maxconn 10000
1281 mode http
1282 cookie SERVERID insert nocache indirect
1283 balance roundrobin
1284 server pentium3-800 192.168.1.1:80 cookie s1 weight 8 maxconn 100 check
1285 server opteron-2.0G 192.168.1.2:80 cookie s2 weight 20 maxconn 300 check
1286 server opteron-2.4G 192.168.1.3:80 cookie s3 weight 24 maxconn 300 check
1287 server web-backup1 192.168.2.1:80 cookie s4 check maxconn 200 backup
1288 server web-excuse 192.168.3.1:80 check backup
1289
willy tarreauf76e6ca2006-05-21 21:09:55 +02001290
1291This was so much efficient at reducing the server's response time that some
1292users wanted to use low values to improve their server's performance. However,
1293they were not able anymore to handle very large loads because it was not
1294possible anymore to saturate the servers. For this reason, version 1.2.14 has
1295brought dynamic limitation with the addition of the parameter 'minconn'. When
1296this parameter is set along with maxconn, it will enable dynamic limitation
1297based on the instance's load. The maximum number of concurrent sessions on a
1298server will be proportionnal to the number of sessions on the instance relative
1299to its maxconn. A minimum of <minconn> will be allowed whatever the load. This
1300will ensure that servers will perform at their best level under normal loads,
1301while still handling surges when needed. The dynamic limit is computed like
1302this :
1303
1304 srv.dyn_limit = max(srv.minconn, srv.maxconn * inst.sess / inst.maxconn)
1305
1306Example :
1307---------
1308 # be nice with P3 which only has 256 MB of RAM.
1309 listen web_appl 0.0.0.0:80
1310 maxconn 10000
1311 mode http
1312 cookie SERVERID insert nocache indirect
1313 balance roundrobin
1314 server pentium3-800 192.168.1.1:80 cookie s1 weight 8 minconn 10 maxconn 100 check
1315 server opteron-2.0G 192.168.1.2:80 cookie s2 weight 20 minconn 30 maxconn 300 check
1316 server opteron-2.4G 192.168.1.3:80 cookie s3 weight 24 minconn 30 maxconn 300 check
1317 server web-backup1 192.168.2.1:80 cookie s4 check maxconn 200 backup
1318 server web-excuse 192.168.3.1:80 check backup
1319
1320In the example above, the server 'pentium3-800' will receive at most 100
1321simultaneous sessions when the proxy instance will reach 10000 sessions, and
1322will receive only 10 simultaneous sessions when the proxy will be under 1000
1323sessions.
1324
willy tarreau532bb552006-05-13 18:40:37 +02001325Notes :
1326-------
1327 - The requests will not stay indefinitely in the queue, they follow the
1328 'contimeout' parameter, and if a request cannot be dequeued within this
1329 timeout because the server is saturated or because the queue is filled,
1330 the session will expire with a 503 error.
1331
willy tarreauf76e6ca2006-05-21 21:09:55 +02001332 - if only <minconn> is specified, it has the same effect as <maxconn>
1333
willy tarreau532bb552006-05-13 18:40:37 +02001334 - setting too low values for maxconn might improve performance but might also
1335 allow slow users to block access to the server for other users.
1336
willy tarreau34f45302006-04-15 21:37:14 +02001337
willy tarreaue0bdd622006-05-21 20:51:54 +020013383.5) Dropping aborted requests
1339------------------------------
1340In presence of very high loads, the servers will take some time to respond. The
1341per-proxy's connection queue will inflate, and the response time will increase
1342respective to the size of the queue times the average per-session response
1343time. When clients will wait for more than a few seconds, they will often hit
1344the 'STOP' button on their browser, leaving a useless request in the queue, and
1345slowing down other users.
1346
1347As there is no way to distinguish between a full STOP and a simple
1348shutdown(SHUT_WR) on the client side, HTTP agents should be conservative and
1349consider that the client might only have closed its output channel while
1350waiting for the response. However, this introduces risks of congestion when
1351lots of users do the same, and is completely useless nowadays because probably
1352no client at all will close the session while waiting for the response. Some
1353HTTP agents support this (Squid, Apache, HAProxy), and others do not (TUX, most
1354hardware-based load balancers). So the probability for a closed input channel
1355to represent a user hitting the 'STOP' button is close to 100%, and it is very
1356tempting to be able to abort the session early without polluting the servers.
1357
1358For this reason, a new option "abortonclose" was introduced in version 1.2.14.
1359By default (without the option) the behaviour is HTTP-compliant. But when the
1360option is specified, a session with an incoming channel closed will be aborted
1361if it's still possible, which means that it's either waiting for a connect() to
1362establish or it is queued waiting for a connection slot. This considerably
1363reduces the queue size and the load on saturated servers when users are tempted
1364to click on STOP, which in turn reduces the response time for other users.
1365
1366Example :
1367---------
1368 listen web_appl 0.0.0.0:80
1369 maxconn 10000
1370 mode http
1371 cookie SERVERID insert nocache indirect
1372 balance roundrobin
1373 server web1 192.168.1.1:80 cookie s1 weight 10 maxconn 100 check
1374 server web2 192.168.1.2:80 cookie s2 weight 10 maxconn 100 check
1375 server web3 192.168.1.3:80 cookie s3 weight 10 maxconn 100 check
1376 server bck1 192.168.2.1:80 cookie s4 check maxconn 200 backup
1377 option abortonclose
1378
1379
willy tarreaueedaa9f2005-12-17 14:08:03 +010013804) Additionnal features
1381=======================
1382
willy tarreau481132e2006-05-21 21:43:10 +02001383Other features are available. They are transparent mode, event logging, header
1384rewriting/filtering, and the status as an HTML page.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001385
willy tarreauc5f73ed2005-12-18 01:26:38 +01001386
willy tarreau0174f312005-12-18 01:02:42 +010013874.1) Network features
willy tarreaueedaa9f2005-12-17 14:08:03 +01001388---------------------
willy tarreau0174f312005-12-18 01:02:42 +010013894.1.1) Transparent mode
1390-----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +01001391In HTTP mode, the 'transparent' keyword allows to intercept sessions which are
1392routed through the system hosting the proxy. This mode was implemented as a
1393replacement for the 'dispatch' mode, since connections without cookie will be
1394sent to the original address while known cookies will be sent to the servers.
1395This mode implies that the system can redirect sessions to a local port.
1396
1397Example :
1398---------
1399 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001400 mode http
1401 transparent
1402 cookie SERVERID
1403 server server01 192.168.1.1:80
1404 server server02 192.168.1.2:80
willy tarreaueedaa9f2005-12-17 14:08:03 +01001405
1406 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
1407 --dport 80 -j REDIRECT --to-ports 65000
1408
1409Note :
1410------
1411If the port is left unspecified on the server, the port the client connected to
1412will be used. This allows to relay a full port range without using transparent
1413mode nor thousands of file descriptors, provided that the system can redirect
1414sessions to local ports.
1415
1416Example :
1417---------
1418 # redirect all ports to local port 65000, then forward to the server on the
1419 # original port.
1420 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001421 mode tcp
1422 server server01 192.168.1.1 check port 60000
1423 server server02 192.168.1.2 check port 60000
willy tarreaueedaa9f2005-12-17 14:08:03 +01001424
1425 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
1426 -j REDIRECT --to-ports 65000
1427
willy tarreau0174f312005-12-18 01:02:42 +010014284.1.2) Per-server source address binding
1429----------------------------------------
1430As of versions 1.1.30 and 1.2.3, it is possible to specify a particular source
1431to reach each server. This is useful when reaching backup servers from a
1432different LAN, or to use an alternate path to reach the same server. It is also
1433usable to provide source load-balancing for outgoing connections. Obviously,
1434the same source address is used to send health-checks.
1435
1436Example :
1437---------
1438 # use a particular source to reach both servers
1439 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001440 mode http
1441 balance roundrobin
1442 server server01 192.168.1.1:80 source 192.168.2.13
1443 server server02 192.168.1.2:80 source 192.168.2.13
willy tarreau0174f312005-12-18 01:02:42 +01001444
1445Example :
1446---------
1447 # use a particular source to reach each servers
1448 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001449 mode http
1450 balance roundrobin
1451 server server01 192.168.1.1:80 source 192.168.1.1
1452 server server02 192.168.2.1:80 source 192.168.2.1
willy tarreau0174f312005-12-18 01:02:42 +01001453
1454Example :
1455---------
1456 # provide source load-balancing to reach the same proxy through 2 WAN links
1457 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001458 mode http
1459 balance roundrobin
1460 server remote-proxy-way1 192.168.1.1:3128 source 192.168.2.1
1461 server remote-proxy-way2 192.168.1.1:3128 source 192.168.3.1
willy tarreau0174f312005-12-18 01:02:42 +01001462
1463Example :
1464---------
1465 # force a TCP connection to bind to a specific port
1466 listen http_proxy 0.0.0.0:2000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001467 mode tcp
1468 balance roundrobin
1469 server srv1 192.168.1.1:80 source 192.168.2.1:20
1470 server srv2 192.168.1.2:80 source 192.168.2.1:20
willy tarreau0174f312005-12-18 01:02:42 +01001471
willy tarreaub952e1d2005-12-18 01:31:20 +010014724.1.3) TCP keep-alive
1473---------------------
1474With version 1.2.7, it becomes possible to enable TCP keep-alives on both the
1475client and server sides. This makes it possible to prevent long sessions from
1476expiring on external layer 4 components such as firewalls and load-balancers.
1477It also allows the system to terminate dead sessions when no timeout has been
1478set (not recommanded). The proxy cannot set the keep-alive probes intervals nor
1479maximal count, consult your operating system manual for this. There are 3
1480options to enable TCP keep-alive :
1481
1482 option tcpka # enables keep-alive both on client and server side
1483 option clitcpka # enables keep-alive only on client side
1484 option srvtcpka # enables keep-alive only on server side
1485
willy tarreaueedaa9f2005-12-17 14:08:03 +01001486
14874.2) Event logging
1488------------------
willy tarreauc5f73ed2005-12-18 01:26:38 +01001489
1490HAProxy's strength certainly lies in its precise logs. It probably provides the
1491finest level of information available for such a product, which is very
1492important for troubleshooting complex environments. Standard log information
1493include client ports, TCP/HTTP state timers, precise session state at
1494termination and precise termination cause, information about decisions to
1495direct trafic to a server, and of course the ability to capture arbitrary
1496headers.
1497
1498In order to improve administrators reactivity, it offers a great transparency
1499about encountered problems, both internal and external, and it is possible to
1500send logs to different sources at the same time with different level filters :
1501
1502 - global process-level logs (system errors, start/stop, etc..)
1503 - per-listener system and internal errors (lack of resource, bugs, ...)
1504 - per-listener external troubles (servers up/down, max connections)
1505 - per-listener activity (client connections), either at the establishment or
1506 at the termination.
1507
1508The ability to distribute different levels of logs to different log servers
1509allow several production teams to interact and to fix their problems as soon
1510as possible. For example, the system team might monitor system-wide errors,
1511while the application team might be monitoring the up/down for their servers in
1512real time, and the security team might analyze the activity logs with one hour
1513delay.
1514
willy tarreauc1cae632005-12-17 14:12:23 +010015154.2.1) Log levels
1516-----------------
willy tarreau197e8ec2005-12-17 14:10:59 +01001517TCP and HTTP connections can be logged with informations such as date, time,
1518source IP address, destination address, connection duration, response times,
1519HTTP request, the HTTP return code, number of bytes transmitted, the conditions
1520in which the session ended, and even exchanged cookies values, to track a
1521particular user's problems for example. All messages are sent to up to two
1522syslog servers. Consult section 1.1 for more info about log facilities. The
1523syntax follows :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001524
willy tarreau197e8ec2005-12-17 14:10:59 +01001525 log <address_1> <facility_1> [max_level_1]
1526 log <address_2> <facility_2> [max_level_2]
1527or
willy tarreaueedaa9f2005-12-17 14:08:03 +01001528 log global
1529
willy tarreau197e8ec2005-12-17 14:10:59 +01001530Note :
1531------
1532The particular syntax 'log global' means that the same log configuration as the
1533'global' section will be used.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001534
willy tarreau197e8ec2005-12-17 14:10:59 +01001535Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001536---------
1537 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001538 mode http
1539 log 192.168.2.200 local3
1540 log 192.168.2.201 local4
willy tarreaueedaa9f2005-12-17 14:08:03 +01001541
willy tarreauc1cae632005-12-17 14:12:23 +010015424.2.2) Log format
1543-----------------
1544By default, connections are logged at the TCP level, as soon as the session
1545establishes between the client and the proxy. By enabling the 'tcplog' option,
1546the proxy will wait until the session ends to generate an enhanced log
1547containing more information such as session duration and its state during the
willy tarreau532bb552006-05-13 18:40:37 +02001548disconnection. The number of remaining session after disconnection is also
1549indicated (for the server, the listener, and the process).
willy tarreauc1cae632005-12-17 14:12:23 +01001550
willy tarreauc5f73ed2005-12-18 01:26:38 +01001551Example of TCP logging :
1552------------------------
willy tarreau982249e2005-12-18 00:57:06 +01001553 listen relais-tcp 0.0.0.0:8000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001554 mode tcp
1555 option tcplog
1556 log 192.168.2.200 local3
willy tarreau982249e2005-12-18 00:57:06 +01001557
willy tarreau532bb552006-05-13 18:40:37 +02001558>>> haproxy[18989]: 127.0.0.1:34550 [15/Oct/2003:15:24:28] relais-tcp Srv1 0/0/5007 0 -- 1/1/1 0/0
willy tarreauc5f73ed2005-12-18 01:26:38 +01001559
willy tarreau532bb552006-05-13 18:40:37 +02001560 Field Format Example
willy tarreauc5f73ed2005-12-18 01:26:38 +01001561
willy tarreau532bb552006-05-13 18:40:37 +02001562 1 process_name '[' pid ']:' haproxy[18989]:
1563 2 client_ip ':' client_port 127.0.0.1:34550
1564 3 '[' date ']' [15/Oct/2003:15:24:28]
1565 4 listener_name relais-tcp
1566 5 server_name Srv1
1567 6 queue_time '/' connect_time '/' total_time 0/0/5007
1568 7 bytes_read 0
1569 8 termination_state --
1570 9 srv_conn '/' listener_conn '/' process_conn 1/1/1
1571 10 position in srv_queue / listener_queue 0/0
1572
willy tarreau982249e2005-12-18 00:57:06 +01001573
willy tarreauc1cae632005-12-17 14:12:23 +01001574Another option, 'httplog', provides more detailed information about HTTP
1575contents, such as the request and some cookies. In the event where an external
1576component would establish frequent connections to check the service, logs may be
1577full of useless lines. So it is possible not to log any session which didn't
1578transfer any data, by the setting of the 'dontlognull' option. This only has
1579effect on sessions which are established then closed.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001580
willy tarreauc5f73ed2005-12-18 01:26:38 +01001581Example of HTTP logging :
1582-------------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +01001583 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001584 mode http
1585 option httplog
1586 option dontlognull
1587 log 192.168.2.200 local3
1588
willy tarreau532bb552006-05-13 18:40:37 +02001589>>> haproxy[674]: 127.0.0.1:33319 [15/Oct/2003:08:31:57] relais-http Srv1 9/0/7/147/723 200 243 - - ---- 2/3/3 0/0 "HEAD / HTTP/1.0"
willy tarreaueedaa9f2005-12-17 14:08:03 +01001590
willy tarreauc5f73ed2005-12-18 01:26:38 +01001591More complete example
willy tarreau532bb552006-05-13 18:40:37 +02001592 haproxy[18989]: 10.0.0.1:34552 [15/Oct/2003:15:26:31] relais-http Srv1 3183/-1/-1/-1/11215 503 0 - - SC-- 137/202/205 0/0 {w.ods.org|Mozilla} {} "HEAD / HTTP/1.0"
willy tarreauc5f73ed2005-12-18 01:26:38 +01001593
willy tarreau532bb552006-05-13 18:40:37 +02001594 Field Format Example
willy tarreauc5f73ed2005-12-18 01:26:38 +01001595
willy tarreau532bb552006-05-13 18:40:37 +02001596 1 process_name '[' pid ']:' haproxy[18989]:
1597 2 client_ip ':' client_port 10.0.0.1:34552
1598 3 '[' date ']' [15/Oct/2003:15:26:31]
1599 4 listener_name relais-http
1600 5 server_name Srv1
1601 6 Tq '/' Tw '/' Tc '/' Tr '/' Tt 3183/-1/-1/-1/11215
1602 7 HTTP_return_code 503
1603 8 bytes_read 0
1604 9 captured_request_cookie -
1605 10 captured_response_cookie -
1606 11 termination_state SC--
1607 12 srv_conn '/' listener_conn '/' process_conn 137/202/205
1608 13 position in srv_queue / listener_queue 0/0
1609 14 '{' captured_request_headers '}' {w.ods.org|Mozilla}
1610 15 '{' captured_response_headers '}' {}
1611 16 '"' HTTP_request '"' "HEAD / HTTP/1.0"
willy tarreauc5f73ed2005-12-18 01:26:38 +01001612
1613Note for log parsers: the URI is ALWAYS the end of the line starting with the
1614 first double quote '"'.
willy tarreau982249e2005-12-18 00:57:06 +01001615
1616The problem when logging at end of connection is that you have no clue about
1617what is happening during very long sessions. To workaround this problem, a
1618new option 'logasap' has been introduced in 1.1.28/1.2.1. When specified, the
1619proxy will log as soon as possible, just before data transfer begins. This means
1620that in case of TCP, it will still log the connection status to the server, and
1621in case of HTTP, it will log just after processing the server headers. In this
1622case, the number of bytes reported is the number of header bytes sent to the
1623client.
1624
1625In order to avoid confusion with normal logs, the total time field and the
1626number of bytes are prefixed with a '+' sign which mean that real numbers are
1627certainly bigger.
1628
1629Example :
1630---------
1631
1632 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001633 mode http
1634 option httplog
1635 option dontlognull
1636 option logasap
1637 log 192.168.2.200 local3
willy tarreau982249e2005-12-18 00:57:06 +01001638
willy tarreau532bb552006-05-13 18:40:37 +02001639>>> haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/10/7/14/+30 200 +243 - - ---- 1/1/3 1/0 "GET /image.iso HTTP/1.0"
willy tarreau982249e2005-12-18 00:57:06 +01001640
willy tarreauc1cae632005-12-17 14:12:23 +010016414.2.3) Timing events
1642--------------------
1643Timers provide a great help in trouble shooting network problems. All values
1644are reported in milliseconds (ms). In HTTP mode, four control points are
willy tarreau532bb552006-05-13 18:40:37 +02001645reported under the form 'Tq/Tw/Tc/Tr/Tt' :
willy tarreauc1cae632005-12-17 14:12:23 +01001646
1647 - Tq: total time to get the client request.
1648 It's the time elapsed between the moment the client connection was accepted
1649 and the moment the proxy received the last HTTP header. The value '-1'
1650 indicates that the end of headers (empty line) has never been seen.
1651
willy tarreau532bb552006-05-13 18:40:37 +02001652 - Tw: total time spent in the queues waiting for a connection slot. It
1653 accounts for listener's queue as well as the server's queue, and depends
1654 on the queue size, and the time needed for the server to complete previous
1655 sessions. The value '-1' means that the request was killed before reaching
1656 the queue.
1657
willy tarreauc1cae632005-12-17 14:12:23 +01001658 - Tc: total time to establish the TCP connection to the server.
1659 It's the time elapsed between the moment the proxy sent the connection
1660 request, and the moment it was acknowledged, or between the TCP SYN packet
1661 and the matching SYN/ACK in return. The value '-1' means that the
1662 connection never established.
1663
1664 - Tr: server response time. It's the time elapsed between the moment the
1665 TCP connection was established to the server and the moment it send its
1666 complete response header. It purely shows its request processing time,
1667 without the network overhead due to the data transmission. The value '-1'
1668 means that the last the response header (empty line) was never seen.
1669
1670 - Tt: total session duration time, between the moment the proxy accepted it
willy tarreau982249e2005-12-18 00:57:06 +01001671 and the moment both ends were closed. The exception is when the 'logasap'
willy tarreau532bb552006-05-13 18:40:37 +02001672 option is specified. In this case, it only equals (Tq+Tw+Tc+Tr), and is
willy tarreau982249e2005-12-18 00:57:06 +01001673 prefixed with a '+' sign. From this field, we can deduce Td, the data
1674 transmission time, by substracting other timers when valid :
willy tarreauc1cae632005-12-17 14:12:23 +01001675
willy tarreau532bb552006-05-13 18:40:37 +02001676 Td = Tt - (Tq + Tw + Tc + Tr)
willy tarreauc1cae632005-12-17 14:12:23 +01001677
1678 Timers with '-1' values have to be excluded from this equation.
1679
willy tarreau532bb552006-05-13 18:40:37 +02001680In TCP mode ('option tcplog'), only Tw, Tc and Tt are reported.
willy tarreauc1cae632005-12-17 14:12:23 +01001681
1682These timers provide precious indications on trouble causes. Since the TCP
1683protocol defines retransmit delays of 3, 6, 12... seconds, we know for sure
1684that timers close to multiples of 3s are nearly always related to packets lost
1685due to network problems (wires or negociation). Moreover, if <Tt> is close to
1686a timeout value specified in the configuration, it often means that a session
1687has been aborted on time-out.
1688
1689Most common cases :
1690
1691 - If Tq is close to 3000, a packet has probably been lost between the client
1692 and the proxy.
1693 - If Tc is close to 3000, a packet has probably been lost between the server
1694 and the proxy during the server connection phase. This one should always be
1695 very low (less than a few tens).
1696 - If Tr is nearly always lower than 3000 except some rare values which seem to
1697 be the average majored by 3000, there are probably some packets lost between
1698 the proxy and the server.
1699 - If Tt is often slightly higher than a time-out, it's often because the
1700 client and the server use HTTP keep-alive and the session is maintained
1701 after the response ends. Se further for how to disable HTTP keep-alive.
1702
1703Other cases ('xx' means any value to be ignored) :
willy tarreau532bb552006-05-13 18:40:37 +02001704 -1/xx/xx/xx/Tt: the client was not able to send its complete request in time,
1705 or that it aborted it too early.
1706 Tq/-1/xx/xx/Tt: it was not possible to process the request, maybe because
1707 servers were out of order.
1708 Tq/Tw/-1/xx/Tt: the connection could not establish on the server. Either it
1709 refused it or it timed out after Tt-(Tq+Tw) ms.
1710 Tq/Tw/Tc/-1/Tt: the server has accepted the connection but did not return a
1711 complete response in time, or it closed its connexion
1712 unexpectedly, after Tt-(Tq+Tw+Tc) ms.
willy tarreauc1cae632005-12-17 14:12:23 +01001713
17144.2.4) Session state at disconnection
1715-------------------------------------
willy tarreauc5f73ed2005-12-18 01:26:38 +01001716TCP and HTTP logs provide a session completion indicator in the
1717<termination_state> field, just before the number of active
1718connections. It is 2-characters long in TCP, and 4-characters long in
1719HTTP, each of which has a special meaning :
1720
willy tarreau197e8ec2005-12-17 14:10:59 +01001721 - On the first character, a code reporting the first event which caused the
1722 session to terminate :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001723
willy tarreauc5f73ed2005-12-18 01:26:38 +01001724 C : the TCP session was unexpectedly aborted by the client.
1725
1726 S : the TCP session was unexpectedly aborted by the server, or the
1727 server explicitly refused it.
1728
1729 P : the session was prematurely aborted by the proxy, because of a
1730 connection limit enforcement, because a DENY filter was matched,
1731 or because of a security check which detected and blocked a
1732 dangerous error in server response which might have caused
1733 information leak (eg: cacheable cookie).
1734
1735 R : a resource on the proxy has been exhausted (memory, sockets, source
1736 ports, ...). Usually, this appears during the connection phase, and
1737 system logs should contain a copy of the precise error.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001738
willy tarreauc5f73ed2005-12-18 01:26:38 +01001739 I : an internal error was identified by the proxy during a self-check.
1740 This should NEVER happen, and you are encouraged to report any log
1741 containing this, because this is a bug.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001742
willy tarreauc5f73ed2005-12-18 01:26:38 +01001743 c : the client-side time-out expired first.
1744
1745 s : the server-side time-out expired first.
1746
1747 - : normal session completion.
1748
1749 - on the second character, the TCP/HTTP session state when it was closed :
1750
1751 R : waiting for complete REQUEST from the client (HTTP only). Nothing
1752 was sent to any server.
1753
willy tarreau532bb552006-05-13 18:40:37 +02001754 Q : waiting in the QUEUE for a connection slot. This can only happen on
1755 servers which have a 'maxconn' parameter set. No connection attempt
1756 was made to any server.
1757
willy tarreauc5f73ed2005-12-18 01:26:38 +01001758 C : waiting for CONNECTION to establish on the server. The server might
1759 at most have noticed a connection attempt.
1760
1761 H : waiting for, receiving and processing server HEADERS (HTTP only).
1762
1763 D : the session was in the DATA phase.
1764
1765 L : the proxy was still transmitting LAST data to the client while the
1766 server had already finished.
1767
Willy Tarreau2272dc12006-09-03 10:19:38 +02001768 T : the request was tarpitted. It has been held open on with the client
Willy Tarreau08fa2e32006-09-03 10:47:37 +02001769 during the whole contimeout duration or untill the client closed.
Willy Tarreau2272dc12006-09-03 10:19:38 +02001770
willy tarreauc5f73ed2005-12-18 01:26:38 +01001771 - : normal session completion after end of data transfer.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001772
willy tarreau197e8ec2005-12-17 14:10:59 +01001773 - the third character tells whether the persistence cookie was provided by
willy tarreauc1cae632005-12-17 14:12:23 +01001774 the client (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001775
willy tarreauc5f73ed2005-12-18 01:26:38 +01001776 N : the client provided NO cookie. This is usually the case on new
1777 connections.
1778
1779 I : the client provided an INVALID cookie matching no known
1780 server. This might be caused by a recent configuration change,
1781 mixed cookies between HTTP/HTTPS sites, or an attack.
1782
1783 D : the client provided a cookie designating a server which was DOWN,
1784 so either the 'persist' option was used and the client was sent to
1785 this server, or it was not set and the client was redispatched to
1786 another server.
1787
1788 V : the client provided a valid cookie, and was sent to the associated
1789 server.
1790
1791 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001792
willy tarreau197e8ec2005-12-17 14:10:59 +01001793 - the last character reports what operations were performed on the persistence
willy tarreauc1cae632005-12-17 14:12:23 +01001794 cookie returned by the server (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001795
willy tarreauc5f73ed2005-12-18 01:26:38 +01001796 N : NO cookie was provided by the server, and none was inserted either.
1797
1798 I : no cookie was provided by the server, and the proxy INSERTED one.
1799
willy tarreau197e8ec2005-12-17 14:10:59 +01001800 P : a cookie was PROVIDED by the server and transmitted as-is.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001801
willy tarreauc5f73ed2005-12-18 01:26:38 +01001802 R : the cookie provided by the server was REWRITTEN by the proxy.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001803
willy tarreauc5f73ed2005-12-18 01:26:38 +01001804 D : the cookie provided by the server was DELETED by the proxy.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001805
willy tarreauc5f73ed2005-12-18 01:26:38 +01001806 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001807
willy tarreauc5f73ed2005-12-18 01:26:38 +01001808The combination of the two first flags give a lot of information about what was
1809happening when the session terminated. It can be helpful to detect server
1810saturation, network troubles, local system resource starvation, attacks, etc...
willy tarreaueedaa9f2005-12-17 14:08:03 +01001811
willy tarreauc5f73ed2005-12-18 01:26:38 +01001812The most common termination flags combinations are indicated here.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001813
willy tarreauc5f73ed2005-12-18 01:26:38 +01001814 Flags Reason
1815 CR The client aborted before sending a full request. Most probably the
1816 request was done by hand using a telnet client, and aborted early.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001817
willy tarreauc5f73ed2005-12-18 01:26:38 +01001818 cR The client timed out before sending a full request. This is sometimes
1819 caused by too large TCP MSS values on the client side for PPPoE
1820 networks which cannot transport full-sized packets, or by clients
1821 sending requests by hand and not typing fast enough.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001822
willy tarreauc5f73ed2005-12-18 01:26:38 +01001823 SC The server explicitly refused the connection (the proxy received a
1824 TCP RST or an ICMP in return). Under some circumstances, it can
1825 also be the network stack telling the proxy that the server is
1826 unreachable (eg: no route, or no ARP response on local network).
willy tarreau982249e2005-12-18 00:57:06 +01001827
willy tarreauc5f73ed2005-12-18 01:26:38 +01001828 sC The connection to the server did not complete during contimeout.
willy tarreau982249e2005-12-18 00:57:06 +01001829
willy tarreauc5f73ed2005-12-18 01:26:38 +01001830 PC The proxy refused to establish a connection to the server because the
1831 maxconn limit has been reached. The listener's maxconn parameter may
1832 be increased in the proxy configuration, as well as the global
1833 maxconn parameter.
willy tarreauc1cae632005-12-17 14:12:23 +01001834
willy tarreauc5f73ed2005-12-18 01:26:38 +01001835 RC A local resource has been exhausted (memory, sockets, source ports)
1836 preventing the connection to the server from establishing. The error
1837 logs will tell precisely what was missing. Anyway, this can only be
1838 solved by system tuning.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001839
willy tarreauc5f73ed2005-12-18 01:26:38 +01001840 cH The client timed out during a POST request. This is sometimes caused
1841 by too large TCP MSS values for PPPoE networks which cannot transport
1842 full-sized packets.
willy tarreauc1cae632005-12-17 14:12:23 +01001843
willy tarreau078c79a2006-05-13 12:23:58 +02001844 CH The client aborted while waiting for the server to start responding.
1845 It might be the server taking too long to respond or the client
1846 clicking the 'Stop' button too fast.
1847
1848 CQ The client aborted while its session was queued, waiting for a server
1849 with enough empty slots to accept it. It might be that either all the
1850 servers were saturated or the assigned server taking too long to
1851 respond.
1852
Willy Tarreau08fa2e32006-09-03 10:47:37 +02001853 CT The client aborted while its session was tarpitted.
1854
willy tarreau078c79a2006-05-13 12:23:58 +02001855 sQ The session spent too much time in queue and has been expired.
1856
willy tarreauc5f73ed2005-12-18 01:26:38 +01001857 SH The server aborted before sending its full headers, or it crashed.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001858
willy tarreauc5f73ed2005-12-18 01:26:38 +01001859 sH The server failed to reply during the srvtimeout delay, which
1860 indicates too long transactions, probably caused by back-end
1861 saturation. The only solutions are to fix the problem on the
1862 application or to increase the 'srvtimeout' parameter to support
1863 longer delays (at the risk of the client giving up anyway).
1864
1865 PR The proxy blocked the client's request, either because of an invalid
1866 HTTP syntax, in which case it returned an HTTP 400 error to the
1867 client, or because a deny filter matched, in which case it returned
1868 an HTTP 403 error.
1869
1870 PH The proxy blocked the server's response, because it was invalid,
1871 incomplete, dangerous (cache control), or matched a security filter.
1872 In any case, an HTTP 502 error is sent to the client.
1873
Willy Tarreau2272dc12006-09-03 10:19:38 +02001874 PT The proxy blocked the client's request and has tarpitted its
1875 connection before returning it a 500 server error. Nothing was sent
1876 to the server.
1877
willy tarreauc5f73ed2005-12-18 01:26:38 +01001878 cD The client did not read any data for as long as the clitimeout delay.
1879 This is often caused by network failures on the client side.
1880
1881 CD The client unexpectedly aborted during data transfer. This is either
1882 caused by a browser crash, or by a keep-alive session between the
1883 server and the client terminated first by the client.
1884
1885 sD The server did nothing during the srvtimeout delay. This is often
1886 caused by too short timeouts on L4 equipements before the server
1887 (firewalls, load-balancers, ...).
1888
18894.2.5) Non-printable characters
willy tarreau4302f492005-12-18 01:00:37 +01001890-------------------------------
1891As of version 1.1.29, non-printable characters are not sent as-is into log
1892files, but are converted to their two-digits hexadecimal representation,
1893prefixed by the character '#'. The only characters that can now be logged
1894without being escaped are between 32 and 126 (inclusive). Obviously, the
1895escape character '#' is also encoded to avoid any ambiguity. It is the same for
1896the character '"', as well as '{', '|' and '}' when logging headers.
1897
willy tarreauc5f73ed2005-12-18 01:26:38 +010018984.2.6) Capturing HTTP headers and cookies
1899-----------------------------------------
1900Version 1.1.23 brought cookie capture, and 1.1.29 the header capture. All this
1901is performed using the 'capture' keyword.
1902
1903Cookie capture makes it easy to track a complete user session. The syntax is :
1904
1905 capture cookie <cookie_prefix> len <capture_length>
1906
1907This will enable cookie capture from both requests and responses. This way,
1908it's easy to detect when a user switches to a new session for example, because
1909the server will reassign it a new cookie.
1910
1911The FIRST cookie whose name starts with <cookie_prefix> will be captured, and
1912logged as 'NAME=value', without exceeding <capture_length> characters (64 max).
1913When the cookie name is fixed and known, it's preferable to suffix '=' to it to
1914ensure that no other cookie will be logged.
1915
1916Examples :
1917----------
1918 # capture the first cookie whose name starts with "ASPSESSION"
1919 capture cookie ASPSESSION len 32
1920
1921 # capture the first cookie whose name is exactly "vgnvisitor"
1922 capture cookie vgnvisitor= len 32
1923
1924In the logs, the field preceeding the completion indicator contains the cookie
1925value as sent by the server, preceeded by the cookie value as sent by the
1926client. Each of these field is replaced with '-' when no cookie was seen or
1927when the option is disabled.
1928
1929Header captures have a different goal. They are useful to track unique request
1930identifiers set by a previous proxy, virtual host names, user-agents, POST
1931content-length, referrers, etc. In the response, one can search for information
1932about the response length, how the server asked the cache to behave, or an
1933object location during a redirection. As for cookie captures, it is both
1934possible to include request headers and response headers at the same time. The
1935syntax is :
willy tarreau4302f492005-12-18 01:00:37 +01001936
1937 capture request header <name> len <max length>
1938 capture response header <name> len <max length>
1939
1940Note: Header names are not case-sensitive.
1941
1942Examples:
1943---------
1944 # keep the name of the virtual server
1945 capture request header Host len 20
1946 # keep the amount of data uploaded during a POST
1947 capture request header Content-Length len 10
1948
1949 # note the expected cache behaviour on the response
1950 capture response header Cache-Control len 8
1951 # note the URL location during a redirection
1952 capture response header Location len 20
1953
1954Non-existant headers are logged as empty strings, and if one header appears more
1955than once, only its last occurence will be kept. Request headers are grouped
1956within braces '{' and '}' in the same order as they were declared, and delimited
1957with a vertical bar '|' without any space. Response headers follow the same
1958representation, but are displayed after a space following the request headers
1959block. These blocks are displayed just before the HTTP request in the logs.
willy tarreauc5f73ed2005-12-18 01:26:38 +01001960
willy tarreau4302f492005-12-18 01:00:37 +01001961Example :
1962
willy tarreauc5f73ed2005-12-18 01:26:38 +01001963 Config:
1964
1965 capture request header Host len 20
1966 capture request header Content-Length len 10
1967 capture request header Referer len 20
1968 capture response header Server len 20
1969 capture response header Content-Length len 10
1970 capture response header Cache-Control len 8
1971 capture response header Via len 20
1972 capture response header Location len 20
1973
1974 Log :
1975
willy tarreau532bb552006-05-13 18:40:37 +02001976 Aug 9 20:26:09 localhost haproxy[2022]: 127.0.0.1:34014 [09/Aug/2004:20:26:09] relais-http netcache 0/0/0/162/+162 200 +350 - - ---- 0/0/0 0/0 {fr.adserver.yahoo.co||http://fr.f416.mail.} {|864|private||} "GET http://fr.adserver.yahoo.com/"
1977 Aug 9 20:30:46 localhost haproxy[2022]: 127.0.0.1:34020 [09/Aug/2004:20:30:46] relais-http netcache 0/0/0/182/+182 200 +279 - - ---- 0/0/0 0/0 {w.ods.org||} {Formilux/0.1.8|3495|||} "GET http://w.ods.org/sytadin.html HTTP/1.1"
1978 Aug 9 20:30:46 localhost haproxy[2022]: 127.0.0.1:34028 [09/Aug/2004:20:30:46] relais-http netcache 0/0/2/126/+128 200 +223 - - ---- 0/0/0 0/0 {www.infotrafic.com||http://w.ods.org/syt} {Apache/2.0.40 (Red H|9068|||} "GET http://www.infotrafic.com/images/live/cartesidf/grandes/idf_ne.png HTTP/1.1"
willy tarreauc5f73ed2005-12-18 01:26:38 +01001979
1980
19814.2.7) Examples of logs
1982-----------------------
willy tarreau532bb552006-05-13 18:40:37 +02001983- haproxy[674]: 127.0.0.1:33319 [15/Oct/2003:08:31:57] relais-http Srv1 6559/0/7/147/6723 200 243 - - ---- 1/3/5 0/0 "HEAD / HTTP/1.0"
willy tarreauc5f73ed2005-12-18 01:26:38 +01001984 => long request (6.5s) entered by hand through 'telnet'. The server replied
1985 in 147 ms, and the session ended normally ('----')
1986
willy tarreau532bb552006-05-13 18:40:37 +02001987- haproxy[674]: 127.0.0.1:33319 [15/Oct/2003:08:31:57] relais-http Srv1 6559/1230/7/147/6870 200 243 - - ---- 99/239/324 0/9 "HEAD / HTTP/1.0"
1988 => Idem, but the request was queued in the global queue behind 9 other
1989 requests, and waited there for 1230 ms.
1990
1991- haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/0/7/14/+30 200 +243 - - ---- 1/3/3 0/0 "GET /image.iso HTTP/1.0"
willy tarreauc5f73ed2005-12-18 01:26:38 +01001992 => request for a long data transfer. The 'logasap' option was specified, so
1993 the log was produced just before transfering data. The server replied in
1994 14 ms, 243 bytes of headers were sent to the client, and total time from
1995 accept to first data byte is 30 ms.
1996
willy tarreau532bb552006-05-13 18:40:37 +02001997- haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/0/7/14/30 502 243 - - PH-- 0/2/3 0/0 "GET /cgi-bin/bug.cgi? HTTP/1.0"
willy tarreauc5f73ed2005-12-18 01:26:38 +01001998 => the proxy blocked a server response either because of an 'rspdeny' or
1999 'rspideny' filter, or because it blocked sensible information which risked
2000 being cached. In this case, the response is replaced with a '502 bad
2001 gateway'.
2002
willy tarreau532bb552006-05-13 18:40:37 +02002003- haproxy[18113]: 127.0.0.1:34548 [15/Oct/2003:15:18:55] relais-http <NOSRV> -1/-1/-1/-1/8490 -1 0 - - CR-- 0/2/2 0/0 ""
willy tarreauc5f73ed2005-12-18 01:26:38 +01002004 => the client never completed its request and aborted itself ('C---') after
2005 8.5s, while the proxy was waiting for the request headers ('-R--').
2006 Nothing was sent to the server.
2007
willy tarreau532bb552006-05-13 18:40:37 +02002008- haproxy[18113]: 127.0.0.1:34549 [15/Oct/2003:15:19:06] relais-http <NOSRV> -1/-1/-1/-1/50001 408 0 - - cR-- 2/2 0/0 ""
willy tarreauc5f73ed2005-12-18 01:26:38 +01002009 => The client never completed its request, which was aborted by the time-out
2010 ('c---') after 50s, while the proxy was waiting for the request headers ('-R--').
2011 Nothing was sent to the server, but the proxy could send a 408 return code
2012 to the client.
willy tarreau4302f492005-12-18 01:00:37 +01002013
willy tarreau532bb552006-05-13 18:40:37 +02002014- haproxy[18989]: 127.0.0.1:34550 [15/Oct/2003:15:24:28] relais-tcp Srv1 0/0/5007 0 cD 0/0/0 0/0
willy tarreauc5f73ed2005-12-18 01:26:38 +01002015 => This is a 'tcplog' entry. Client-side time-out ('c----') occured after 5s.
willy tarreau4302f492005-12-18 01:00:37 +01002016
willy tarreau532bb552006-05-13 18:40:37 +02002017- haproxy[18989]: 10.0.0.1:34552 [15/Oct/2003:15:26:31] relais-http Srv1 3183/-1/-1/-1/11215 503 0 - - SC-- 115/202/205 0/0 "HEAD / HTTP/1.0"
willy tarreauc5f73ed2005-12-18 01:26:38 +01002018 => The request took 3s to complete (probably a network problem), and the
2019 connection to the server failed ('SC--') after 4 attemps of 2 seconds
2020 (config says 'retries 3'), then a 503 error code was sent to the client.
willy tarreau532bb552006-05-13 18:40:37 +02002021 There were 115 connections on this server, 202 connections on this proxy,
2022 and 205 on the global process. It is possible that the server refused the
2023 connection because of too many already established.
willy tarreau4302f492005-12-18 01:00:37 +01002024
willy tarreau4302f492005-12-18 01:00:37 +01002025
willy tarreau197e8ec2005-12-17 14:10:59 +010020264.3) HTTP header manipulation
2027-----------------------------
2028In HTTP mode, it is possible to rewrite, add or delete some of the request and
2029response headers based on regular expressions. It is also possible to block a
2030request or a response if a particular header matches a regular expression,
2031which is enough to stops most elementary protocol attacks, and to protect
2032against information leak from the internal network. But there is a limitation
2033to this : since haproxy's HTTP engine knows nothing about keep-alive, only
2034headers passed during the first request of a TCP session will be seen. All
2035subsequent headers will be considered data only and not analyzed. Furthermore,
2036haproxy doesn't touch data contents, it stops at the end of headers.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002037
willy tarreau197e8ec2005-12-17 14:10:59 +01002038The syntax is :
2039 reqadd <string> to add a header to the request
2040 reqrep <search> <replace> to modify the request
2041 reqirep <search> <replace> same, but ignoring the case
2042 reqdel <search> to delete a header in the request
2043 reqidel <search> same, but ignoring the case
2044 reqallow <search> definitely allow a request if a header matches <search>
2045 reqiallow <search> same, but ignoring the case
2046 reqdeny <search> denies a request if a header matches <search>
2047 reqideny <search> same, but ignoring the case
2048 reqpass <search> ignore a header matching <search>
2049 reqipass <search> same, but ignoring the case
Willy Tarreau2272dc12006-09-03 10:19:38 +02002050 reqtarpit <search> tarpit a request matching <search>
2051 reqitarpit <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01002052
willy tarreau197e8ec2005-12-17 14:10:59 +01002053 rspadd <string> to add a header to the response
2054 rsprep <search> <replace> to modify the response
2055 rspirep <search> <replace> same, but ignoring the case
2056 rspdel <search> to delete the response
2057 rspidel <search> same, but ignoring the case
willy tarreau982249e2005-12-18 00:57:06 +01002058 rspdeny <search> replaces a response with a HTTP 502 if a header matches <search>
2059 rspideny <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01002060
2061
willy tarreau197e8ec2005-12-17 14:10:59 +01002062<search> is a POSIX regular expression (regex) which supports grouping through
2063parenthesis (without the backslash). Spaces and other delimiters must be
2064prefixed with a backslash ('\') to avoid confusion with a field delimiter.
2065Other characters may be prefixed with a backslash to change their meaning :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002066
willy tarreau197e8ec2005-12-17 14:10:59 +01002067 \t for a tab
2068 \r for a carriage return (CR)
2069 \n for a new line (LF)
2070 \ to mark a space and differentiate it from a delimiter
2071 \# to mark a sharp and differentiate it from a comment
2072 \\ to use a backslash in a regex
2073 \\\\ to use a backslash in the text (*2 for regex, *2 for haproxy)
2074 \xXX to write the ASCII hex code XX as in the C language
willy tarreaueedaa9f2005-12-17 14:08:03 +01002075
2076
Willy Tarreau2272dc12006-09-03 10:19:38 +02002077<replace> contains the string to be used to replace the largest portion of text
willy tarreau197e8ec2005-12-17 14:10:59 +01002078matching the regex. It can make use of the special characters above, and can
2079reference a substring delimited by parenthesis in the regex, by the group
Willy Tarreau2272dc12006-09-03 10:19:38 +02002080numerical order from 0 to 9 (0 being the entire line). In this case, you would
2081write a backslash ('\') immediately followed by one digit indicating the group
2082position.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002083
willy tarreau197e8ec2005-12-17 14:10:59 +01002084<string> represents the string which will systematically be added after the last
2085header line. It can also use special characters above.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002086
willy tarreau197e8ec2005-12-17 14:10:59 +01002087Notes :
2088-------
2089 - the first line is considered as a header, which makes it possible to rewrite
2090 or filter HTTP requests URIs or response codes.
2091 - 'reqrep' is the equivalent of 'cliexp' in version 1.0, and 'rsprep' is the
2092 equivalent of 'srvexp' in 1.0. Those names are still supported but
2093 deprecated.
2094 - for performances reasons, the number of characters added to a request or to
2095 a response is limited to 4096 since version 1.1.5 (it was 256 before). This
2096 value is easy to modify in the code if needed (#define). If it is too short
2097 on occasional uses, it is possible to gain some space by removing some
2098 useless headers before adding new ones.
willy tarreau982249e2005-12-18 00:57:06 +01002099 - a denied request will generate an "HTTP 403 forbidden" response, while a
2100 denied response will generate an "HTTP 502 Bad gateway" response.
Willy Tarreau2272dc12006-09-03 10:19:38 +02002101 - a tarpitted request will be held open on the client side for a duration
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002102 defined in the contimeout parameter, or untill the client aborts. Nothing
2103 will be sent to any server. When the timeout is reached, the proxy will
2104 reply with a 500 server error response so that the attacker does not
2105 suspect it has been tarpitted. The logs may report the 500, but the
2106 termination flags will indicate 'PT' in this case.
Willy Tarreau2272dc12006-09-03 10:19:38 +02002107
willy tarreaueedaa9f2005-12-17 14:08:03 +01002108
willy tarreau197e8ec2005-12-17 14:10:59 +01002109Examples :
2110----------
willy tarreauc5f73ed2005-12-18 01:26:38 +01002111 ###### a few examples ######
willy tarreau197e8ec2005-12-17 14:10:59 +01002112
willy tarreauc5f73ed2005-12-18 01:26:38 +01002113 # rewrite 'online.fr' instead of 'free.fr' for GET and POST requests
2114 reqrep ^(GET\ .*)(.free.fr)(.*) \1.online.fr\3
2115 reqrep ^(POST\ .*)(.free.fr)(.*) \1.online.fr\3
willy tarreau197e8ec2005-12-17 14:10:59 +01002116
willy tarreauc5f73ed2005-12-18 01:26:38 +01002117 # force proxy connections to close
2118 reqirep ^Proxy-Connection:.* Proxy-Connection:\ close
2119 # rewrite locations
2120 rspirep ^(Location:\ )([^:]*://[^/]*)(.*) \1\3
willy tarreaueedaa9f2005-12-17 14:08:03 +01002121
willy tarreauc5f73ed2005-12-18 01:26:38 +01002122 ###### A full configuration being used on production ######
willy tarreaueedaa9f2005-12-17 14:08:03 +01002123
willy tarreau197e8ec2005-12-17 14:10:59 +01002124 # Every header should end with a colon followed by one space.
willy tarreauc5f73ed2005-12-18 01:26:38 +01002125 reqideny ^[^:\ ]*[\ ]*$
willy tarreaueedaa9f2005-12-17 14:08:03 +01002126
willy tarreau197e8ec2005-12-17 14:10:59 +01002127 # block Apache chunk exploit
willy tarreauc5f73ed2005-12-18 01:26:38 +01002128 reqideny ^Transfer-Encoding:[\ ]*chunked
2129 reqideny ^Host:\ apache-
willy tarreaueedaa9f2005-12-17 14:08:03 +01002130
willy tarreau197e8ec2005-12-17 14:10:59 +01002131 # block annoying worms that fill the logs...
willy tarreauc5f73ed2005-12-18 01:26:38 +01002132 reqideny ^[^:\ ]*\ .*(\.|%2e)(\.|%2e)(%2f|%5c|/|\\\\)
2133 reqideny ^[^:\ ]*\ ([^\ ]*\ [^\ ]*\ |.*%00)
2134 reqideny ^[^:\ ]*\ .*<script
2135 reqideny ^[^:\ ]*\ .*/(root\.exe\?|cmd\.exe\?|default\.ida\?)
willy tarreau197e8ec2005-12-17 14:10:59 +01002136
Willy Tarreau2272dc12006-09-03 10:19:38 +02002137 # tarpit attacks on the login page.
2138 reqtarpit ^[^:\ ]*\ .*\.php?login=[^0-9]
2139
willy tarreau197e8ec2005-12-17 14:10:59 +01002140 # allow other syntactically valid requests, and block any other method
willy tarreauc5f73ed2005-12-18 01:26:38 +01002141 reqipass ^(GET|POST|HEAD|OPTIONS)\ /.*\ HTTP/1\.[01]$
2142 reqipass ^OPTIONS\ \\*\ HTTP/1\.[01]$
2143 reqideny ^[^:\ ]*\
willy tarreau197e8ec2005-12-17 14:10:59 +01002144
2145 # force connection:close, thus disabling HTTP keep-alive
willy tarreauc5f73ed2005-12-18 01:26:38 +01002146 option httpclose
willy tarreau197e8ec2005-12-17 14:10:59 +01002147
willy tarreauc5f73ed2005-12-18 01:26:38 +01002148 # change the server name
2149 rspidel ^Server:\
2150 rspadd Server:\ Formilux/0.1.8
willy tarreau197e8ec2005-12-17 14:10:59 +01002151
2152
willy tarreau982249e2005-12-18 00:57:06 +01002153Also, the 'forwardfor' option creates an HTTP 'X-Forwarded-For' header which
willy tarreauc1cae632005-12-17 14:12:23 +01002154contains the client's IP address. This is useful to let the final web server
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002155know what the client address was (eg for statistics on domains). Starting with
2156version 1.3.8, it is possible to specify the "except" keyword followed by a
2157source IP address or network for which no header will be added. This is very
2158useful when another reverse-proxy which already adds the header runs on the
2159same machine or in a known DMZ, the most common case being the local use of
2160stunnel on the same system.
willy tarreauc1cae632005-12-17 14:12:23 +01002161
willy tarreau982249e2005-12-18 00:57:06 +01002162Last, the 'httpclose' option removes any 'Connection' header both ways, and
2163adds a 'Connection: close' header in each direction. This makes it easier to
Willy TARREAU767ba712006-03-01 22:40:50 +01002164disable HTTP keep-alive than the previous 4-rules block.
willy tarreau982249e2005-12-18 00:57:06 +01002165
willy tarreauc1cae632005-12-17 14:12:23 +01002166Example :
2167---------
2168 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01002169 mode http
2170 log global
2171 option httplog
2172 option dontlognull
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002173 option forwardfor except 127.0.0.1/8
willy tarreauc5f73ed2005-12-18 01:26:38 +01002174 option httpclose
2175
Willy TARREAU767ba712006-03-01 22:40:50 +01002176Note that some HTTP servers do not necessarily close the connections when they
2177receive the 'Connection: close', and if the client does not close either, then
2178the connection will be maintained up to the time-out. This translates into high
2179number of simultaneous sessions and high global session times in the logs. To
2180workaround this, a new option 'forceclose' appeared in version 1.2.9 to enforce
2181the closing of the outgoing server channel as soon as the server begins to
2182reply and only if the request buffer is empty. Note that this should NOT be
2183used if CONNECT requests are expected between the client and the server. The
2184'forceclose' option implies the 'httpclose' option.
2185
2186Example :
2187---------
2188 listen http_proxy 0.0.0.0:80
2189 mode http
2190 log global
2191 option httplog
2192 option dontlognull
2193 option forwardfor
2194 option forceclose
2195
willy tarreau197e8ec2005-12-17 14:10:59 +01002196
21974.4) Load balancing with persistence
2198------------------------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01002199Combining cookie insertion with internal load balancing allows to transparently
2200bring persistence to applications. The principle is quite simple :
2201 - assign a cookie value to each server
2202 - enable the load balancing between servers
2203 - insert a cookie into responses resulting from the balancing algorithm
2204 (indirect accesses), end ensure that no upstream proxy will cache it.
2205 - remove the cookie in the request headers so that the application never sees
2206 it.
2207
2208Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002209---------
2210 listen application 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01002211 mode http
2212 cookie SERVERID insert nocache indirect
2213 balance roundrobin
2214 server srv1 192.168.1.1:80 cookie server01 check
2215 server srv2 192.168.1.2:80 cookie server02 check
willy tarreaueedaa9f2005-12-17 14:08:03 +01002216
willy tarreau0174f312005-12-18 01:02:42 +01002217The other solution brought by versions 1.1.30 and 1.2.3 is to reuse a cookie
2218from the server, and prefix the server's name to it. In this case, don't forget
2219to force "httpclose" mode so that you can be assured that every subsequent
2220request will have its cookie fixed.
2221
2222 listen application 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01002223 mode http
2224 cookie JSESSIONID prefix
2225 balance roundrobin
2226 server srv1 192.168.1.1:80 cookie srv1 check
2227 server srv2 192.168.1.2:80 cookie srv2 check
2228 option httpclose
willy tarreau0174f312005-12-18 01:02:42 +01002229
2230
willy tarreau982249e2005-12-18 00:57:06 +010022314.5) Protection against information leak from the servers
2232---------------------------------------------------------
2233In versions 1.1.28/1.2.1, a new option 'checkcache' was created. It carefully
2234checks 'Cache-control', 'Pragma' and 'Set-cookie' headers in server response
2235to check if there's a risk of caching a cookie on a client-side proxy. When this
2236option is enabled, the only responses which can be delivered to the client are :
2237 - all those without 'Set-Cookie' header ;
2238 - all those with a return code other than 200, 203, 206, 300, 301, 410,
2239 provided that the server has not set a 'Cache-control: public' header ;
2240 - all those that come from a POST request, provided that the server has not
2241 set a 'Cache-Control: public' header ;
2242 - those with a 'Pragma: no-cache' header
2243 - those with a 'Cache-control: private' header
2244 - those with a 'Cache-control: no-store' header
2245 - those with a 'Cache-control: max-age=0' header
2246 - those with a 'Cache-control: s-maxage=0' header
2247 - those with a 'Cache-control: no-cache' header
2248 - those with a 'Cache-control: no-cache="set-cookie"' header
2249 - those with a 'Cache-control: no-cache="set-cookie,' header
2250 (allowing other fields after set-cookie)
willy tarreaueedaa9f2005-12-17 14:08:03 +01002251
willy tarreau982249e2005-12-18 00:57:06 +01002252If a response doesn't respect these requirements, then it will be blocked just
2253as if it was from an 'rspdeny' filter, with an "HTTP 502 bad gateway". The
2254session state shows "PH--" meaning that the proxy blocked the response during
2255headers processing. Additionnaly, an alert will be sent in the logs so that
2256admins are told that there's something to be done.
2257
willy tarreauc5f73ed2005-12-18 01:26:38 +01002258
willy tarreau982249e2005-12-18 00:57:06 +010022594.6) Customizing errors
2260-----------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01002261Some situations can make haproxy return an HTTP error code to the client :
2262 - invalid or too long request => HTTP 400
2263 - request not completely sent in time => HTTP 408
2264 - forbidden request (matches a deny filter) => HTTP 403
2265 - internal error in haproxy => HTTP 500
2266 - the server returned an invalid or incomplete response => HTTP 502
2267 - no server was available to handle the request => HTTP 503
2268 - the server failed to reply in time => HTTP 504
willy tarreaueedaa9f2005-12-17 14:08:03 +01002269
willy tarreau197e8ec2005-12-17 14:10:59 +01002270A succint error message taken from the RFC accompanies these return codes.
2271But depending on the clients knowledge, it may be better to return custom, user
2272friendly, error pages. This is made possible through the use of the 'errorloc'
2273command :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002274
willy tarreau197e8ec2005-12-17 14:10:59 +01002275 errorloc <HTTP_code> <location>
willy tarreaueedaa9f2005-12-17 14:08:03 +01002276
willy tarreau197e8ec2005-12-17 14:10:59 +01002277Instead of generating an HTTP error <HTTP_code> among those above, the proxy
2278will return a temporary redirection code (HTTP 302) towards the address
2279specified in <location>. This address may be either relative to the site or
2280absolute. Since this request will be handled by the client's browser, it's
2281mandatory that the returned address be reachable from the outside.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002282
willy tarreau197e8ec2005-12-17 14:10:59 +01002283Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002284---------
2285 listen application 0.0.0.0:80
2286 errorloc 400 /badrequest.html
2287 errorloc 403 /forbidden.html
2288 errorloc 408 /toolong.html
willy tarreauc5f73ed2005-12-18 01:26:38 +01002289 errorloc 500 http://haproxy.domain.net/bugreport.html
willy tarreaueedaa9f2005-12-17 14:08:03 +01002290 errorloc 502 http://192.168.114.58/error50x.html
2291 errorloc 503 http://192.168.114.58/error50x.html
2292 errorloc 504 http://192.168.114.58/error50x.html
2293
willy tarreauc1f47532005-12-18 01:08:26 +01002294Note: RFC2616 says that a client must reuse the same method to fetch the
2295Location returned by a 302, which causes problems with the POST method.
2296The return code 303 was designed explicitly to force the client to fetch the
2297Location URL with the GET method, but there are some browsers pre-dating
2298HTTP/1.1 which don't support it. Anyway, most browsers still behave with 302 as
willy tarreauc5f73ed2005-12-18 01:26:38 +01002299if it was a 303. In order to allow the user to chose, versions 1.1.31 and 1.2.5
2300bring two new keywords to replace 'errorloc' : 'errorloc302' and 'errorloc303'.
willy tarreauc1f47532005-12-18 01:08:26 +01002301
2302They are preffered over errorloc (which still does 302). Consider using
2303errorloc303 everytime you know that your clients support HTTP 303 responses..
2304
2305
willy tarreau982249e2005-12-18 00:57:06 +010023064.7) Modifying default values
willy tarreau197e8ec2005-12-17 14:10:59 +01002307-----------------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01002308Version 1.1.22 introduced the notion of default values, which eliminates the
2309pain of often repeating common parameters between many instances, such as
2310logs, timeouts, modes, etc...
willy tarreaueedaa9f2005-12-17 14:08:03 +01002311
willy tarreau197e8ec2005-12-17 14:10:59 +01002312Default values are set in a 'defaults' section. Each of these section clears
2313all previously set default parameters, so there may be as many default
2314parameters as needed. Only the last one before a 'listen' section will be
2315used for this section. The 'defaults' section uses the same syntax as the
2316'listen' section, for the supported parameters. The 'defaults' keyword ignores
2317everything on its command line, so that fake instance names can be specified
2318there for better clarity.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002319
willy tarreau982249e2005-12-18 00:57:06 +01002320In version 1.1.28/1.2.1, only those parameters can be preset in the 'default'
willy tarreau197e8ec2005-12-17 14:10:59 +01002321section :
2322 - log (the first and second one)
willy tarreaueedaa9f2005-12-17 14:08:03 +01002323 - mode { tcp, http, health }
2324 - balance { roundrobin }
willy tarreau197e8ec2005-12-17 14:10:59 +01002325 - disabled (to disable every further instances)
2326 - enabled (to enable every further instances, this is the default)
willy tarreaueedaa9f2005-12-17 14:08:03 +01002327 - contimeout, clitimeout, srvtimeout, grace, retries, maxconn
willy tarreau982249e2005-12-18 00:57:06 +01002328 - option { redispatch, transparent, keepalive, forwardfor, logasap, httpclose,
2329 checkcache, httplog, tcplog, dontlognull, persist, httpchk }
willy tarreaueedaa9f2005-12-17 14:08:03 +01002330 - redispatch, redisp, transparent, source { addr:port }
2331 - cookie, capture
2332 - errorloc
2333
willy tarreau197e8ec2005-12-17 14:10:59 +01002334As of 1.1.24, it is not possible to put certain parameters in a 'defaults'
2335section, mainly regular expressions and server configurations :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002336 - dispatch, server,
willy tarreau197e8ec2005-12-17 14:10:59 +01002337 - req*, rsp*
willy tarreaueedaa9f2005-12-17 14:08:03 +01002338
willy tarreau197e8ec2005-12-17 14:10:59 +01002339Last, there's no way yet to change a boolean option from its assigned default
2340value. So if an 'option' statement is set in a 'defaults' section, the only
2341way to flush it is to redefine a new 'defaults' section without this 'option'.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002342
willy tarreau197e8ec2005-12-17 14:10:59 +01002343Examples :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002344----------
2345 defaults applications TCP
willy tarreauc5f73ed2005-12-18 01:26:38 +01002346 log global
2347 mode tcp
2348 balance roundrobin
2349 clitimeout 180000
2350 srvtimeout 180000
2351 contimeout 4000
2352 retries 3
2353 redispatch
willy tarreaueedaa9f2005-12-17 14:08:03 +01002354
2355 listen app_tcp1 10.0.0.1:6000-6063
willy tarreauc5f73ed2005-12-18 01:26:38 +01002356 server srv1 192.168.1.1 check port 6000 inter 10000
2357 server srv2 192.168.1.2 backup
willy tarreaueedaa9f2005-12-17 14:08:03 +01002358
2359 listen app_tcp2 10.0.0.2:6000-6063
willy tarreauc5f73ed2005-12-18 01:26:38 +01002360 server srv1 192.168.2.1 check port 6000 inter 10000
2361 server srv2 192.168.2.2 backup
willy tarreaueedaa9f2005-12-17 14:08:03 +01002362
2363 defaults applications HTTP
willy tarreauc5f73ed2005-12-18 01:26:38 +01002364 log global
2365 mode http
2366 option httplog
2367 option forwardfor
2368 option dontlognull
2369 balance roundrobin
2370 clitimeout 20000
2371 srvtimeout 20000
2372 contimeout 4000
2373 retries 3
willy tarreaueedaa9f2005-12-17 14:08:03 +01002374
2375 listen app_http1 10.0.0.1:80-81
willy tarreauc5f73ed2005-12-18 01:26:38 +01002376 cookie SERVERID postonly insert indirect
2377 capture cookie userid= len 10
2378 server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000
2379 server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000
willy tarreaueedaa9f2005-12-17 14:08:03 +01002380
2381 defaults
willy tarreauc5f73ed2005-12-18 01:26:38 +01002382 # this empty section voids all default parameters
willy tarreaueedaa9f2005-12-17 14:08:03 +01002383
willy tarreau481132e2006-05-21 21:43:10 +02002384
23854.8) Status report in HTML page
2386-------------------------------
2387Starting with 1.2.14, it is possible for HAProxy to intercept requests for a
2388particular URI and return a full report of the proxy's activity and servers
2389statistics. This is available through the 'stats' keyword, associated to any
2390such options :
2391
2392 - stats enable
2393 - stats uri <uri prefix>
2394 - stats realm <authentication realm>
2395 - stats auth <user:password>
2396 - stats scope <proxy_id> | '.'
2397
2398By default, the status report is disabled. Specifying any combination above
2399enables it for the proxy instance referencing it. The easiest solution is to
2400use "stats enable" which will enable the report with default parameters :
2401
2402 - default URI : "/haproxy?stats" (CONFIG_STATS_DEFAULT_URI)
2403 - default auth : unspecified (no authentication)
2404 - default realm : "HAProxy Statistics" (CONFIG_STATS_DEFAULT_REALM)
2405 - default scope : unspecified (access to all instances)
2406
2407The "stats uri <uri_prefix>" option allows one to intercept another URI prefix.
2408Note that any URI that BEGINS with this string will match. For instance, one
2409proxy instance might be dedicated to status page only and would reply to any
2410URI.
2411
2412Example :
2413---------
2414 # catches any URI and returns the status page.
2415 listen stats :8080
2416 mode http
2417 stats uri /
2418
2419The "stats auth <user:password>" option enables Basic authentication and adds a
2420valid user:password combination to the list of authorized accounts. The user
2421and password are passed in the configuration file as clear text, and since this
2422is HTTP Basic authentication, you should be aware that it transits as clear
2423text on the network, so you must not use any sensible account. The list is
2424unlimited in order to provide easy accesses to developpers or customers.
2425
2426The "stats realm <realm>" option defines the "realm" name which is displayed
2427in the popup box when the browser asks for a password. It's important to ensure
2428that this one is not used by the application, otherwise the browser will try to
2429use a cached one from the application. Note that any space in the realm name
2430should be escaped with a backslash ('\').
2431
2432The "stats scope <proxy_id>" option limits the scope of the status report. By
2433default, all proxy instances are listed. But under some circumstances, it would
2434be better to limit the listing to some proxies or only to the current one. This
2435is what this option does. The special proxy name "." (a single dot) references
2436the current proxy. The proxy name can be repeated multiple times, even for
2437proxies defined later in the configuration or some which do not exist. The name
2438is the one which appears after the 'listen' keyword.
2439
2440Example :
2441---------
2442 # simple application with authenticated embedded status report
2443 listen app1 192.168.1.100:80
2444 mode http
willy tarreaud4ba08d2006-05-21 21:54:14 +02002445 option httpclose
willy tarreau481132e2006-05-21 21:43:10 +02002446 balance roundrobin
2447 cookie SERVERID postonly insert indirect
2448 server srv1 192.168.1.1:8080 cookie srv1 check inter 1000
2449 server srv1 192.168.1.2:8080 cookie srv2 check inter 1000
2450 stats uri /my_stats
willy tarreaud4ba08d2006-05-21 21:54:14 +02002451 stats realm Statistics\ for\ MyApp1-2
2452 stats auth guest:guest
2453 stats auth admin:AdMiN123
2454 stats scope .
2455 stats scope app2
willy tarreau481132e2006-05-21 21:43:10 +02002456
2457 # simple application with anonymous embedded status report
2458 listen app2 192.168.2.100:80
2459 mode http
willy tarreaud4ba08d2006-05-21 21:54:14 +02002460 option httpclose
willy tarreau481132e2006-05-21 21:43:10 +02002461 balance roundrobin
2462 cookie SERVERID postonly insert indirect
2463 server srv1 192.168.2.1:8080 cookie srv1 check inter 1000
2464 server srv1 192.168.2.2:8080 cookie srv2 check inter 1000
2465 stats uri /my_stats
willy tarreaud4ba08d2006-05-21 21:54:14 +02002466 stats realm Statistics\ for\ MyApp2
2467 stats scope .
willy tarreau481132e2006-05-21 21:43:10 +02002468
2469 listen admin_page :8080
2470 mode http
2471 stats uri /my_stats
willy tarreaud4ba08d2006-05-21 21:54:14 +02002472 stats realm Global\ statistics
2473 stats auth admin:AdMiN123
willy tarreau481132e2006-05-21 21:43:10 +02002474
2475Notes :
2476-------
2477 - The 'stats' options can also be specified in the 'defaults' section, in
2478 which case it will provide the exact same configuration to all further
2479 instances (hence the usefulness of the scope "."). However, if an instance
2480 redefines any 'stats' parameter, defaults will not be used for this
2481 instance.
2482
2483 - HTTP Basic authentication is very basic and unsecure from snooping. No
2484 sensible password should be used, and be aware that there is no way to
2485 remove it from the browser so it will be sent to the whole application
2486 upon further accesses.
2487
willy tarreaud4ba08d2006-05-21 21:54:14 +02002488 - It is very important that the 'option httpclose' is specified, otherwise
2489 the proxy will not be able to detect the URI within keep-alive sessions
2490 maintained between the browser and the servers, so the stats URI will be
2491 forwarded unmodified to the server as if the option was not set.
2492
willy tarreau481132e2006-05-21 21:43:10 +02002493
willy tarreau197e8ec2005-12-17 14:10:59 +01002494=========================
2495| System-specific setup |
2496=========================
willy tarreaueedaa9f2005-12-17 14:08:03 +01002497
willy tarreau197e8ec2005-12-17 14:10:59 +01002498Linux 2.4
2499=========
willy tarreaueedaa9f2005-12-17 14:08:03 +01002500
2501-- cut here --
2502#!/bin/sh
2503# set this to about 256/4M (16384 for 256M machine)
2504MAXFILES=16384
2505echo $MAXFILES > /proc/sys/fs/file-max
2506ulimit -n $MAXFILES
2507
2508if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then
willy tarreauc5f73ed2005-12-18 01:26:38 +01002509 echo 65536 > /proc/sys/net/ipv4/ip_conntrack_max
willy tarreaueedaa9f2005-12-17 14:08:03 +01002510fi
2511
2512if [ -e /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait ]; then
willy tarreauc5f73ed2005-12-18 01:26:38 +01002513 # 30 seconds for fin, 15 for time wait
2514 echo 3000 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait
2515 echo 1500 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_time_wait
2516 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_invalid_scale
2517 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_out_of_window
willy tarreaueedaa9f2005-12-17 14:08:03 +01002518fi
2519
2520echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range
2521echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
2522echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog
2523echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets
2524echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans
2525echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
2526echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
2527echo 0 > /proc/sys/net/ipv4/tcp_timestamps
2528echo 0 > /proc/sys/net/ipv4/tcp_ecn
willy tarreauc5f73ed2005-12-18 01:26:38 +01002529echo 1 > /proc/sys/net/ipv4/tcp_sack
willy tarreaueedaa9f2005-12-17 14:08:03 +01002530echo 0 > /proc/sys/net/ipv4/tcp_dsack
2531
2532# auto-tuned on 2.4
2533#echo 262143 > /proc/sys/net/core/rmem_max
2534#echo 262143 > /proc/sys/net/core/rmem_default
2535
2536echo 16384 65536 524288 > /proc/sys/net/ipv4/tcp_rmem
2537echo 16384 349520 699040 > /proc/sys/net/ipv4/tcp_wmem
2538
2539-- cut here --
2540
willy tarreau197e8ec2005-12-17 14:10:59 +01002541
2542FreeBSD
2543=======
2544
2545A FreeBSD port of HA-Proxy is now available and maintained, thanks to
2546Clement Laforet <sheepkiller@cultdeadsheep.org>.
2547
2548For more information :
2549http://www.freebsd.org/cgi/url.cgi?ports/net/haproxy/pkg-descr
2550http://www.freebsd.org/cgi/cvsweb.cgi/ports/net/haproxy/
2551http://www.freshports.org/net/haproxy
2552
2553
2554-- end --