blob: e7b877fda779407eb15c8cfc7e286a2ac0dd74cf [file] [log] [blame]
willy tarreau0174f312005-12-18 01:02:42 +01001 -------------------
2 H A - P r o x y
3 Reference Manual
4 -------------------
willy tarreau7e6328d2006-05-21 23:26:20 +02005 version 1.2.14
willy tarreauc5f73ed2005-12-18 01:26:38 +01006 willy tarreau
willy tarreau7e6328d2006-05-21 23:26:20 +02007 2006/05/21
willy tarreaueedaa9f2005-12-17 14:08:03 +01008
9============
10| Abstract |
11============
12
13HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high
14availability 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>
115 - chroot <directory>
116 - nbproc <number>
117 - daemon
118 - debug
willy tarreau64a3cc32005-12-18 01:13:11 +0100119 - noepoll
120 - nopoll
willy tarreaueedaa9f2005-12-17 14:08:03 +0100121 - quiet
willy tarreaufe2c5c12005-12-17 14:14:34 +0100122 - pidfile <file>
willy tarreauc5f73ed2005-12-18 01:26:38 +0100123 - ulimit-n <number>
willy tarreau598da412005-12-18 01:07:29 +0100124 - stats
willy tarreaueedaa9f2005-12-17 14:08:03 +0100125
willy tarreauc5f73ed2005-12-18 01:26:38 +0100126
willy tarreaueedaa9f2005-12-17 14:08:03 +01001271.1) Event logging
128------------------
129Most events are logged : start, stop, servers going up and down, connections and
130errors. Each event generates a syslog message which can be sent to up to 2
131servers. The syntax is :
132
133 log <ip_address> <facility> [max_level]
134
135Connections are logged at level "info". Services initialization and servers
136going up are logged at level "notice", termination signals are logged at
137"warning", and definitive service termination, as well as loss of servers are
138logged at level "alert". The optional parameter <max_level> specifies above
139what level messages should be sent. Level can take one of these 8 values :
140
141 emerg, alert, crit, err, warning, notice, info, debug
142
143For backwards compatibility with versions 1.1.16 and earlier, the default level
144value is "debug" if not specified.
145
146Permitted facilities are :
147 kern, user, mail, daemon, auth, syslog, lpr, news,
148 uucp, cron, auth2, ftp, ntp, audit, alert, cron2,
149 local0, local1, local2, local3, local4, local5, local6, local7
150
151According to RFC3164, messages are truncated to 1024 bytes before being emitted.
152
153Example :
154---------
155 global
willy tarreauc5f73ed2005-12-18 01:26:38 +0100156 log 192.168.2.200 local3
157 log 127.0.0.1 local4 notice
158
willy tarreaueedaa9f2005-12-17 14:08:03 +0100159
1601.2) limiting the number of connections
161---------------------------------------
162It is possible and recommended to limit the global number of per-process
willy tarreauc5f73ed2005-12-18 01:26:38 +0100163connections using the 'maxconn' global keyword. Since one connection includes
164both a client and a server, it means that the max number of TCP sessions will
165be about the double of this number. It's important to understand this when
166trying to find best values for 'ulimit -n' before starting the proxy. To
167anticipate the number of sockets needed, all these parameters must be counted :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100168
169 - 1 socket per incoming connection
170 - 1 socket per outgoing connection
171 - 1 socket per address/port/proxy tuple.
172 - 1 socket per server being health-checked
173 - 1 socket for all logs
174
175In simple configurations where each proxy only listens one one address/port,
willy tarreauc5f73ed2005-12-18 01:26:38 +0100176set the limit of file descriptors (ulimit -n) to
177(2 * maxconn + nbproxies + nbservers + 1). Starting with versions 1.1.32/1.2.6,
178it is now possible to set the limit in the configuration using the 'ulimit-n'
179global keyword, provided the proxy is started as root. This puts an end to the
180recurrent problem of ensuring that the system limits are adapted to the proxy
181values. Note that these limits are per-process.
182
183Example :
184---------
185 global
186 maxconn 32000
187 ulimit-n 65536
188
willy tarreaueedaa9f2005-12-17 14:08:03 +0100189
1901.3) Drop of priviledges
191------------------------
192In order to reduce the risk and consequences of attacks, in the event where a
193yet non-identified vulnerability would be successfully exploited, it's possible
194to lower the process priviledges and even isolate it in a riskless directory.
195
196In the 'global' section, the 'uid' parameter sets a numerical user identifier
197which the process will switch to after binding its listening sockets. The value
198'0', which normally represents the super-user, here indicates that the UID must
199not change during startup. It's the default behaviour. The 'gid' parameter does
200the same for the group identifier. It's particularly advised against use of
201generic accounts such as 'nobody' because it has the same consequences as using
202'root' if other services use them.
203
204The 'chroot' parameter makes the process isolate itself in an empty directory
205just before switching its UID. This type of isolation (chroot) can sometimes
206be worked around on certain OS (Linux, Solaris), provided that the attacker
207has gained 'root' priviledges and has the ability to use or create a directory.
208For this reason, it's capital to use a dedicated directory and not to share one
209between several services of different nature. To make isolation more resistant,
210it's recommended to use an empty directory without any right, and to change the
211UID of the process so that it cannot do anything there.
212
213Note: in the event where such a vulnerability would be exploited, it's most
214likely that first attempts would kill the process due to 'Segmentation Fault',
215'Bus Error' or 'Illegal Instruction' signals. Eventhough it's true that
216isolating the server reduces the risks of intrusion, it's sometimes useful to
217find why a process dies, via the analysis of a 'core' file, although very rare
218(the last bug of this sort was fixed in 1.1.9). For security reasons, most
219systems disable the generation of core file when a process changes its UID. So
220the two workarounds are either to start the process from a restricted user
221account, which will not be able to chroot itself, or start it as root and not
222change the UID. In both cases the core will be either in the start or the chroot
223directories. Do not forget to allow core dumps prior to start the process :
224
225# ulimit -c unlimited
226
227Example :
228---------
229
230 global
willy tarreauc5f73ed2005-12-18 01:26:38 +0100231 uid 30000
232 gid 30000
233 chroot /var/chroot/haproxy
234
willy tarreaueedaa9f2005-12-17 14:08:03 +0100235
2361.4) Startup modes
237------------------
willy tarreau34f45302006-04-15 21:37:14 +0200238The service can start in several different modes :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100239 - foreground / background
240 - quiet / normal / debug
241
242The default mode is normal, foreground, which means that the program doesn't
243return once started. NEVER EVER use this mode in a system startup script, or
244the system won't boot. It needs to be started in background, so that it
245returns immediately after forking. That's accomplished by the 'daemon' option
246in the 'global' section, which is the equivalent of the '-D' command line
247argument.
248
willy tarreau34f45302006-04-15 21:37:14 +0200249The '-db' command line argument overrides the 'daemon' and 'nbproc' global
250options to make the process run in normal, foreground mode.
251
willy tarreaueedaa9f2005-12-17 14:08:03 +0100252Moreover, certain alert messages are still sent to the standard output even
253in 'daemon' mode. To make them disappear, simply add the 'quiet' option in the
254'global' section. This option has no command-line equivalent.
255
256Last, the 'debug' mode, enabled with the 'debug' option in the 'global' section,
257and which is equivalent of the '-d' option, allows deep TCP/HTTP analysis, with
258timestamped display of each connection, disconnection, and HTTP headers for both
259ways. This mode is incompatible with 'daemon' and 'quiet' modes for obvious
260reasons.
261
willy tarreauc5f73ed2005-12-18 01:26:38 +0100262
willy tarreaueedaa9f2005-12-17 14:08:03 +01002631.5) Increasing the overall processing power
264--------------------------------------------
265On multi-processor systems, it may seem to be a shame to use only one processor,
willy tarreau982249e2005-12-18 00:57:06 +0100266eventhough the load needed to saturate a recent processor is far above common
willy tarreaueedaa9f2005-12-17 14:08:03 +0100267usage. Anyway, for very specific needs, the proxy can start several processes
268between which the operating system will spread the incoming connections. The
269number of processes is controlled by the 'nbproc' parameter in the 'global'
willy tarreau4302f492005-12-18 01:00:37 +0100270section. It defaults to 1, and obviously works only in 'daemon' mode. One
271typical usage of this parameter has been to workaround the default per-process
272file-descriptor limit that Solaris imposes to user processes.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100273
274Example :
275---------
276
277 global
willy tarreauc5f73ed2005-12-18 01:26:38 +0100278 daemon
279 quiet
280 nbproc 2
willy tarreaueedaa9f2005-12-17 14:08:03 +0100281
282
willy tarreaufe2c5c12005-12-17 14:14:34 +01002831.6) Helping process management
284-------------------------------
285Haproxy now supports the notion of pidfile. If the '-p' command line argument,
286or the 'pidfile' global option is followed with a file name, this file will be
287removed, then filled with all children's pids, one per line (only in daemon
288mode). This file is NOT within the chroot, which allows to work with a readonly
289 chroot. It will be owned by the user starting the process, and will have
290permissions 0644.
291
292Example :
293---------
294
295 global
296 daemon
297 quiet
willy tarreauc5f73ed2005-12-18 01:26:38 +0100298 nbproc 2
willy tarreaufe2c5c12005-12-17 14:14:34 +0100299 pidfile /var/run/haproxy-private.pid
300
301 # to stop only those processes among others :
302 # kill $(</var/run/haproxy-private.pid)
303
willy tarreau34f45302006-04-15 21:37:14 +0200304 # to reload a new configuration with minimal service impact and without
305 # breaking existing sessions :
306 # haproxy -f haproxy.cfg -p $(</var/run/haproxy-private.pid) -st $(</var/run/haproxy-private.pid)
willy tarreaufe2c5c12005-12-17 14:14:34 +0100307
willy tarreau64a3cc32005-12-18 01:13:11 +01003081.7) Polling mechanisms
309-----------------------
310Starting from version 1.2.5, haproxy supports the poll() and epoll() polling
311mechanisms. On systems where select() is limited by FD_SETSIZE (like Solaris),
312poll() can be an interesting alternative. Performance tests show that Solaris'
313poll() performance does not decay as fast as the numbers of sockets increase,
314making it a safe solution for high loads. However, Solaris already uses poll()
315to emulate select(), so as long as the number of sockets has no reason to go
316higher than FD_SETSIZE, poll() should not provide any better performance. On
317Linux systems with the epoll() patch (or any 2.6 version), haproxy will use
318epoll() which is extremely fast and non dependant on the number of sockets.
319Tests have shown constant performance from 1 to 20000 simultaneous sessions.
320
321Haproxy will use epoll() when available, and will fall back to poll(), then to
322select(). However, if for any reason you need to disable epoll() or poll() (eg.
323because of a bug or just to compare performance), two new global options have
324been created for this matter : 'noepoll' and 'nopoll'.
325
326Example :
327---------
328
329 global
330 # use only select()
331 noepoll
332 nopoll
333
334Note :
335------
336For the sake of configuration file portability, these options are accepted but
337ignored if the poll() or epoll() mechanisms have not been enabled at compile
338time.
339
340To make debugging easier, the '-de' runtime argument disables epoll support and
341the '-dp' argument disables poll support. They are respectively equivalent to
342'noepoll' and 'nopoll'.
343
344
willy tarreaueedaa9f2005-12-17 14:08:03 +01003452) Declaration of a listening service
346=====================================
347
348Service sections start with the 'listen' keyword :
349
350 listen <instance_name> [ <IP_address>:<port_range>[,...] ]
351
352- <instance_name> is the name of the instance. This name will be reported in
353 logs, so it is good to have it reflect the proxied service. No unicity test
354 is done on this name, and it's not mandatory for it to be unique, but highly
355 recommended.
356
357- <IP_address> is the IP address the proxy binds to. Empty address, '*' and
358 '0.0.0.0' all mean that the proxy listens to all valid addresses on the
359 system.
360
361- <port_range> is either a unique port, or a port range for which the proxy will
362 accept connections for the IP address specified above. This range can be :
363 - a numerical port (ex: '80')
364 - a dash-delimited ports range explicitly stating the lower and upper bounds
365 (ex: '2000-2100') which are included in the range.
366
367 Particular care must be taken against port ranges, because every <addr:port>
368 couple consumes one socket (=a file descriptor), so it's easy to eat lots of
369 descriptors with a simple range. The <addr:port> couple must be used only once
370 among all instances running on a same system. Please note that attaching to
371 ports lower than 1024 need particular priviledges to start the program, which
372 are independant of the 'uid' parameter.
373
374- the <IP_address>:<port_range> couple may be repeated indefinitely to require
375 the proxy to listen to other addresses and/or ports. To achieve this, simply
376 separate them with a coma.
377
378Examples :
379---------
380 listen http_proxy :80
381 listen x11_proxy 127.0.0.1:6000-6009
382 listen smtp_proxy 127.0.0.1:25,127.0.0.1:587
383 listen ldap_proxy :389,:663
384
385In the event that all addresses do not fit line width, it's preferable to
386detach secondary addresses on other lines with the 'bind' keyword. If this
387keyword is used, it's not even necessary to specify the first address on the
388'listen' line, which sometimes makes multiple configuration handling easier :
389
390 bind [ <IP_address>:<port_range>[,...] ]
391
392Examples :
393----------
394 listen http_proxy
395 bind :80,:443
willy tarreauc5f73ed2005-12-18 01:26:38 +0100396 bind 10.0.0.1:10080,10.0.0.1:10443
397
willy tarreaueedaa9f2005-12-17 14:08:03 +0100398
3992.1) Inhibiting a service
400-------------------------
401A service may be disabled for maintenance reasons, without needing to comment
402out the whole section, simply by specifying the 'disabled' keyword in the
403section to be disabled :
404
405 listen smtp_proxy 0.0.0.0:25
willy tarreauc5f73ed2005-12-18 01:26:38 +0100406 disabled
willy tarreaueedaa9f2005-12-17 14:08:03 +0100407
408Note: the 'enabled' keyword allows to enable a service which has been disabled
409 previously by a default configuration.
410
willy tarreauc5f73ed2005-12-18 01:26:38 +0100411
willy tarreaueedaa9f2005-12-17 14:08:03 +01004122.2) Modes of operation
413-----------------------
414A service can work in 3 different distinct modes :
415 - TCP
416 - HTTP
willy tarreau532bb552006-05-13 18:40:37 +0200417 - health
willy tarreaueedaa9f2005-12-17 14:08:03 +0100418
419TCP mode
420--------
421In this mode, the service relays TCP connections as soon as they're established,
422towards one or several servers. No processing is done on the stream. It's only
423an association of source(addr:port) -> destination(addr:port). To use this mode,
424you must specify 'mode tcp' in the 'listen' section. This is the default mode.
425
426Example :
427---------
428 listen smtp_proxy 0.0.0.0:25
willy tarreauc5f73ed2005-12-18 01:26:38 +0100429 mode tcp
willy tarreaueedaa9f2005-12-17 14:08:03 +0100430
431HTTP mode
432---------
433In this mode, the service relays TCP connections towards one or several servers,
434when it has enough informations to decide, which normally means that all HTTP
435headers have been read. Some of them may be scanned for a cookie or a pattern
436matching a regex. To use this mode, specify 'mode http' in the 'listen' section.
437
438Example :
439---------
440 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100441 mode http
willy tarreaueedaa9f2005-12-17 14:08:03 +0100442
443Health-checking mode
444--------------------
445This mode provides a way for external components to check the proxy's health.
446It is meant to be used with intelligent load-balancers which can use send/expect
447scripts to check for all of their servers' availability. This one simply accepts
willy tarreau197e8ec2005-12-17 14:10:59 +0100448the connection, returns the word 'OK' and closes it. If the 'option httpchk' is
449set, then the reply will be 'HTTP/1.0 200 OK' with no data, so that it can be
450tested from a tool which supports HTTP health-checks. To enable it, simply
willy tarreaueedaa9f2005-12-17 14:08:03 +0100451specify 'health' as the working mode :
452
453Example :
454---------
willy tarreau197e8ec2005-12-17 14:10:59 +0100455 # simple response : 'OK'
willy tarreaueedaa9f2005-12-17 14:08:03 +0100456 listen health_check 0.0.0.0:60000
willy tarreauc5f73ed2005-12-18 01:26:38 +0100457 mode health
willy tarreaueedaa9f2005-12-17 14:08:03 +0100458
willy tarreau197e8ec2005-12-17 14:10:59 +0100459 # HTTP response : 'HTTP/1.0 200 OK'
460 listen http_health_check 0.0.0.0:60001
willy tarreauc5f73ed2005-12-18 01:26:38 +0100461 mode health
462 option httpchk
463
willy tarreau532bb552006-05-13 18:40:37 +02004642.2.1 Monitoring
465----------------
willy tarreauc5f73ed2005-12-18 01:26:38 +0100466Versions 1.1.32 and 1.2.6 provide a new solution to check the proxy's
467availability without perturbating the service. The 'monitor-net' keyword was
468created to specify a network of equipments which CANNOT use the service for
469anything but health-checks. This is particularly suited to TCP proxies, because
470it prevents the proxy from relaying the monitor's connection to the remote
471server.
472
473When used with TCP, the connection is accepted then closed and nothing is
474logged. This is enough for a front-end load-balancer to detect the service as
475available.
willy tarreau197e8ec2005-12-17 14:10:59 +0100476
willy tarreauc5f73ed2005-12-18 01:26:38 +0100477When used with HTTP, the connection is accepted, nothing is logged, the
478following response is sent, then the session is closed : "HTTP/1.0 200 OK".
479This is normally enough for any front-end HTTP load-balancer to detect the
480service as available too, both with TCP and HTTP checks.
481
482Proxies using the "monitor-net" keyword can remove the "option dontlognull", as
483it will make them log empty connections from hosts outside the monitoring
484network.
485
486Example :
487---------
488
489 listen tse-proxy
490 bind :3389,:1494,:5900 # TSE, ICA and VNC at once.
491 mode tcp
492 balance roundrobin
493 server tse-farm 192.168.1.10
494 monitor-net 192.168.1.252/31 # L4 load-balancers on .252 and .253
495
willy tarreaueedaa9f2005-12-17 14:08:03 +0100496
4972.3) Limiting the number of simultaneous connections
498----------------------------------------------------
499The 'maxconn' parameter allows a proxy to refuse connections above a certain
500amount of simultaneous ones. When the limit is reached, it simply stops
501listening, but the system may still be accepting them because of the back log
willy tarreau982249e2005-12-18 00:57:06 +0100502queue. These connections will be processed later when other ones have freed
willy tarreaueedaa9f2005-12-17 14:08:03 +0100503some slots. This provides a serialization effect which helps very fragile
willy tarreau34f45302006-04-15 21:37:14 +0200504servers resist to high loads. See further for system limitations.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100505
506Example :
507---------
508 listen tiny_server 0.0.0.0:80
509 maxconn 10
510
511
5122.4) Soft stop
513--------------
514It is possible to stop services without breaking existing connections by the
willy tarreau22739ef2006-01-20 20:43:32 +0100515sending of the SIGUSR1 signal to the process. All services are then put into
willy tarreaueedaa9f2005-12-17 14:08:03 +0100516soft-stop state, which means that they will refuse to accept new connections,
517except for those which have a non-zero value in the 'grace' parameter, in which
518case they will still accept connections for the specified amount of time, in
willy tarreau22739ef2006-01-20 20:43:32 +0100519milliseconds. This makes it possible to tell a load-balancer that the service
520is failing, while still doing the job during the time it needs to detect it.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100521
522Note: active connections are never killed. In the worst case, the user will have
523to wait for all of them to close or to time-out, or simply kill the process
willy tarreau22739ef2006-01-20 20:43:32 +0100524normally (SIGTERM). The default 'grace' value is '0'.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100525
526Example :
527---------
528 # enter soft stop after 'killall -USR1 haproxy'
529 # the service will still run 10 seconds after the signal
530 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100531 mode http
532 grace 10000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100533
534 # this port is dedicated to a load-balancer, and must fail immediately
535 listen health_check 0.0.0.0:60000
willy tarreauc5f73ed2005-12-18 01:26:38 +0100536 mode health
537 grace 0
willy tarreaueedaa9f2005-12-17 14:08:03 +0100538
539
willy tarreau39df2dc2006-01-29 21:56:05 +0100540As of version 1.2.8, a new soft-reconfiguration mechanism has been introduced.
willy tarreau22739ef2006-01-20 20:43:32 +0100541It is now possible to "pause" all the proxies by sending a SIGTTOU signal to
542the processes. This will disable the listening socket without breaking existing
543connections. After that, sending a SIGTTIN signal to those processes enables
544the listening sockets again. This is very useful to try to load a new
545configuration or even a new version of haproxy without breaking existing
546connections. If the load succeeds, then simply send a SIGUSR1 which will make
547the previous proxies exit immediately once their sessions are closed ; and if
548the load fails, then simply send a SIGTTIN to restore the service immediately.
549Please note that the 'grace' parameter is ignored for SIGTTOU, as well as for
550SIGUSR1 when the process was in the pause mode. Please also note that it would
551be useful to save the pidfile before starting a new instance.
552
willy tarreau34f45302006-04-15 21:37:14 +0200553This mechanism fully exploited since 1.2.11 with the '-st' and '-sf' options
willy tarreau532bb552006-05-13 18:40:37 +0200554(see below).
555
5562.4.1) Hot reconfiguration
557--------------------------
558The '-st' and '-sf' command line options are used to inform previously running
559processes that a configuration is being reloaded. They will receive the SIGTTOU
560signal to ask them to temporarily stop listening to the ports so that the new
561process can grab them. If anything wrong happens, the new process will send
562them a SIGTTIN to tell them to re-listen to the ports and continue their normal
563work. Otherwise, it will either ask them to finish (-sf) their work then softly
564exit, or immediately terminate (-st), breaking existing sessions. A typical use
565of this allows a configuration reload without service interruption :
566
567 # haproxy -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)
568
willy tarreau22739ef2006-01-20 20:43:32 +0100569
willy tarreaueedaa9f2005-12-17 14:08:03 +01005702.5) Connections expiration time
571--------------------------------
572It is possible (and recommended) to configure several time-outs on TCP
573connections. Three independant timers are adjustable with values specified
574in milliseconds. A session will be terminated if either one of these timers
575expire.
576
577 - the time we accept to wait for data from the client, or for the client to
578 accept data : 'clitimeout' :
579
willy tarreauc5f73ed2005-12-18 01:26:38 +0100580 # client time-out set to 2mn30.
581 clitimeout 150000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100582
583 - the time we accept to wait for data from the server, or for the server to
584 accept data : 'srvtimeout' :
585
willy tarreauc5f73ed2005-12-18 01:26:38 +0100586 # server time-out set to 30s.
587 srvtimeout 30000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100588
589 - the time we accept to wait for a connection to establish on a server :
590 'contimeout' :
591
592 # we give up if the connection does not complete within 4 seconds
willy tarreauc5f73ed2005-12-18 01:26:38 +0100593 contimeout 4000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100594
595Notes :
596-------
597 - 'contimeout' and 'srvtimeout' have no sense on 'health' mode servers ;
598 - under high loads, or with a saturated or defective network, it's possible
599 that some packets get lost. Since the first TCP retransmit only happens
600 after 3 seconds, a time-out equal to, or lower than 3 seconds cannot
601 compensate for a packet loss. A 4 seconds time-out seems a reasonable
602 minimum which will considerably reduce connection failures.
603
willy tarreauc5f73ed2005-12-18 01:26:38 +0100604
willy tarreaueedaa9f2005-12-17 14:08:03 +01006052.6) Attempts to reconnect
606--------------------------
607After a connection failure to a server, it is possible to retry, potentially
608on another server. This is useful if health-checks are too rare and you don't
609want the clients to see the failures. The number of attempts to reconnect is
610set by the 'retries' paramter.
611
612Example :
613---------
willy tarreauc5f73ed2005-12-18 01:26:38 +0100614 # we can retry 3 times max after a failure
615 retries 3
willy tarreaueedaa9f2005-12-17 14:08:03 +0100616
willy tarreau34f45302006-04-15 21:37:14 +0200617Please note that the reconnection attempt may lead to getting the connection
618sent to a new server if the original one died between connection attempts.
619
willy tarreaueedaa9f2005-12-17 14:08:03 +0100620
6212.7) Address of the dispatch server (deprecated)
622------------------------------------------------
623The server which will be sent all new connections is defined by the 'dispatch'
624parameter, in the form <address>:<port>. It generally is dedicated to unknown
625connections and will assign them a cookie, in case of HTTP persistence mode,
626or simply is a single server in case of generic TCP proxy. This old mode is only
627provided for backwards compatibility, but doesn't allow to check remote servers
628state, and has a rather limited usage. All new setups should switch to 'balance'
629mode. The principle of the dispatcher is to be able to perform the load
630balancing itself, but work only on new clients so that the server doesn't need
631to be a big machine.
632
633Example :
634---------
willy tarreauc5f73ed2005-12-18 01:26:38 +0100635 # all new connections go there
636 dispatch 192.168.1.2:80
willy tarreaueedaa9f2005-12-17 14:08:03 +0100637
638Note :
639------
640This parameter has no sense for 'health' servers, and is incompatible with
641'balance' mode.
642
643
6442.8) Outgoing source address
645----------------------------
646It is often necessary to bind to a particular address when connecting to some
647remote hosts. This is done via the 'source' parameter which is a per-proxy
648parameter. A newer version may allow to fix different sources to reach different
649servers. The syntax is 'source <address>[:<port>]', where <address> is a valid
650local address (or '0.0.0.0' or '*' or empty to let the system choose), and
651<port> is an optional parameter allowing the user to force the source port for
652very specific needs. If the port is not specified or is '0', the system will
653choose a free port. Note that as of version 1.1.18, the servers health checks
654are also performed from the same source.
655
656Examples :
657----------
658 listen http_proxy *:80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100659 # all connections take 192.168.1.200 as source address
660 source 192.168.1.200:0
willy tarreaueedaa9f2005-12-17 14:08:03 +0100661
662 listen rlogin_proxy *:513
willy tarreauc5f73ed2005-12-18 01:26:38 +0100663 # use address 192.168.1.200 and the reserved port 900 (needs to be root)
664 source 192.168.1.200:900
willy tarreaueedaa9f2005-12-17 14:08:03 +0100665
666
6672.9) Setting the cookie name
668----------------------------
669In HTTP mode, it is possible to look for a particular cookie which will contain
670a server identifier which should handle the connection. The cookie name is set
671via the 'cookie' parameter.
672
673Example :
674---------
675 listen http_proxy :80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100676 mode http
677 cookie SERVERID
willy tarreaueedaa9f2005-12-17 14:08:03 +0100678
679It is possible to change the cookie behaviour to get a smarter persistence,
680depending on applications. It is notably possible to delete or modify a cookie
681emitted by a server, insert a cookie identifying the server in an HTTP response
682and even add a header to tell upstream caches not to cache this response.
683
684Examples :
685----------
686
687To remove the cookie for direct accesses (ie when the server matches the one
688which was specified in the client cookie) :
689
willy tarreauc5f73ed2005-12-18 01:26:38 +0100690 cookie SERVERID indirect
willy tarreaueedaa9f2005-12-17 14:08:03 +0100691
692To replace the cookie value with the one assigned to the server if any (no
693cookie will be created if the server does not provide one, nor if the
694configuration does not provide one). This lets the application put the cookie
695exactly on certain pages (eg: successful authentication) :
696
willy tarreauc5f73ed2005-12-18 01:26:38 +0100697 cookie SERVERID rewrite
willy tarreaueedaa9f2005-12-17 14:08:03 +0100698
699To create a new cookie and assign the server identifier to it (in this case, all
700servers should be associated with a valid cookie, since no cookie will simply
701delete the cookie from the client's browser) :
702
willy tarreauc5f73ed2005-12-18 01:26:38 +0100703 cookie SERVERID insert
willy tarreaueedaa9f2005-12-17 14:08:03 +0100704
willy tarreau0174f312005-12-18 01:02:42 +0100705To reuse an existing application cookie and prefix it with the server's
706identifier, and remove it in the request, use the 'prefix' option. This allows
707to insert a haproxy in front of an application without risking to break clients
708which does not support more than one cookie :
709
willy tarreauc5f73ed2005-12-18 01:26:38 +0100710 cookie JSESSIONID prefix
willy tarreau0174f312005-12-18 01:02:42 +0100711
willy tarreaueedaa9f2005-12-17 14:08:03 +0100712To insert a cookie and ensure that no upstream cache will store it, add the
713'nocache' option :
714
willy tarreauc5f73ed2005-12-18 01:26:38 +0100715 cookie SERVERID insert nocache
willy tarreaueedaa9f2005-12-17 14:08:03 +0100716
717To insert a cookie only after a POST request, add 'postonly' after 'insert'.
718This has the advantage that there's no risk of caching, and that all pages
719seen before the POST one can still be cached :
720
willy tarreauc5f73ed2005-12-18 01:26:38 +0100721 cookie SERVERID insert postonly
willy tarreaueedaa9f2005-12-17 14:08:03 +0100722
723Notes :
724-----------
725- it is possible to combine 'insert' with 'indirect' or 'rewrite' to adapt to
726 applications which already generate the cookie with an invalid content.
727
728- in the case where 'insert' and 'indirect' are both specified, the cookie is
willy tarreau0174f312005-12-18 01:02:42 +0100729 never transmitted to the server, since it wouldn't understand it. This is the
730 most application-transparent mode.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100731
732- it is particularly recommended to use 'nocache' in 'insert' mode if any
733 upstream HTTP/1.0 cache is susceptible to cache the result, because this may
734 lead to many clients going to the same server, or even worse, some clients
735 having their server changed while retrieving a page from the cache.
736
willy tarreau0174f312005-12-18 01:02:42 +0100737- the 'prefix' mode normally does not need 'indirect', 'nocache', nor
738 'postonly', because just as in the 'rewrite' mode, it relies on the
739 application to know when a cookie can be emitted. However, since it has to
740 fix the cookie name in every subsequent requests, you must ensure that the
741 proxy will be used without any "HTTP keep-alive". Use option "httpclose" if
742 unsure.
743
willy tarreaueedaa9f2005-12-17 14:08:03 +0100744- when the application is well known and controlled, the best method is to
745 only add the persistence cookie on a POST form because it's up to the
willy tarreau0174f312005-12-18 01:02:42 +0100746 application to select which page it wants the upstream servers to cache. In
747 this case, you would use 'insert postonly indirect'.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100748
willy tarreauc5f73ed2005-12-18 01:26:38 +0100749
willy tarreaueedaa9f2005-12-17 14:08:03 +01007502.10) Associating a cookie value with a server
751----------------------------------------------
752In HTTP mode, it's possible to associate a cookie value to each server. This
753was initially used in combination with 'dispatch' mode to handle direct accesses
754but it is now the standard way of doing the load balancing. The syntax is :
755
756 server <identifier> <address>:<port> cookie <value>
757
758- <identifier> is any name which can be used to identify the server in the logs.
759- <address>:<port> specifies where the server is bound.
760- <value> is the value to put in or to read from the cookie.
761
762Example : the 'SERVERID' cookie can be either 'server01' or 'server02'
763---------
764 listen http_proxy :80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100765 mode http
766 cookie SERVERID
767 dispatch 192.168.1.100:80
768 server web1 192.168.1.1:80 cookie server01
769 server web2 192.168.1.2:80 cookie server02
willy tarreaueedaa9f2005-12-17 14:08:03 +0100770
771Warning : the syntax has changed since version 1.0 !
772---------
773
willy tarreauc5f73ed2005-12-18 01:26:38 +0100774
willy tarreau598da412005-12-18 01:07:29 +01007752.11) Application Cookies
776-------------------------
777Since 1.2.4 it is possible to catch the cookie that comes from an
778application server in order to apply "application session stickyness".
779The server's response is searched for 'appsession' cookie, the first
780'len' bytes are used for matching and it is stored for a period of
781'timeout'.
782The syntax is:
783
willy tarreau532bb552006-05-13 18:40:37 +0200784 appsession <session_cookie> len <match_length> timeout <holdtime>
willy tarreau598da412005-12-18 01:07:29 +0100785
willy tarreau532bb552006-05-13 18:40:37 +0200786- <session_cookie> is the cookie, the server uses for it's session-handling
787- <match_length> how many bytes/characters should be used for matching equal
willy tarreau598da412005-12-18 01:07:29 +0100788 sessions
willy tarreau532bb552006-05-13 18:40:37 +0200789- <holdtime> after this inactivaty time, in ms, the cookie will be deleted
willy tarreau598da412005-12-18 01:07:29 +0100790 from the sessionstore
791
792The appsession is only per 'listen' section possible.
793
794Example :
795---------
willy tarreau532bb552006-05-13 18:40:37 +0200796 listen http_lb1 192.168.3.4:80
797 mode http
798 capture request header Cookie len 200
799 # Havind a ServerID cookie on the client allows him to reach
800 # the right server even after expiration of the appsession.
801 cookie ServerID insert nocache indirect
802 # Will memorize 52 bytes of the cookie 'JSESSIONID' and keep them
803 # for 3 hours. It will match it in the cookie and the URL field.
804 appsession JSESSIONID len 52 timeout 10800000
805 server first1 10.3.9.2:10805 check inter 3000 cookie first
806 server secon1 10.3.9.3:10805 check inter 3000 cookie secon
807 server first1 10.3.9.4:10805 check inter 3000 cookie first
808 server secon2 10.3.9.5:10805 check inter 3000 cookie secon
809 option httpchk GET /test.jsp
willy tarreau598da412005-12-18 01:07:29 +0100810
willy tarreauc5f73ed2005-12-18 01:26:38 +0100811
willy tarreaueedaa9f2005-12-17 14:08:03 +01008123) Autonomous load balancer
813===========================
814
815The proxy can perform the load-balancing itself, both in TCP and in HTTP modes.
816This is the most interesting mode which obsoletes the old 'dispatch' mode
817described above. It has advantages such as server health monitoring, multiple
818port binding and port mapping. To use this mode, the 'balance' keyword is used,
willy tarreau34f45302006-04-15 21:37:14 +0200819followed by the selected algorithm. Up to version 1.2.11, only 'roundrobin' was
820available, which is also the default value if unspecified. Starting with
821version 1.2.12, a new 'source' keyword appeared. In this mode, there will be no
822dispatch address, but the proxy needs at least one server.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100823
824Example : same as the last one, with internal load balancer
825---------
826
827 listen http_proxy :80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100828 mode http
829 cookie SERVERID
830 balance roundrobin
831 server web1 192.168.1.1:80 cookie server01
832 server web2 192.168.1.2:80 cookie server02
willy tarreaueedaa9f2005-12-17 14:08:03 +0100833
834
835Since version 1.1.22, it is possible to automatically determine on which port
836the server will get the connection, depending on the port the client connected
837to. Indeed, there now are 4 possible combinations for the server's <port> field:
838
839 - unspecified or '0' :
840 the connection will be sent to the same port as the one on which the proxy
841 received the client connection itself.
842
843 - numerical value (the only one supported in versions earlier than 1.1.22) :
844 the connection will always be sent to the specified port.
845
846 - '+' followed by a numerical value :
847 the connection will be sent to the same port as the one on which the proxy
848 received the connection, plus this value.
849
850 - '-' followed by a numerical value :
851 the connection will be sent to the same port as the one on which the proxy
852 received the connection, minus this value.
853
854Examples :
855----------
856
857# same as previous example
858
859 listen http_proxy :80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100860 mode http
861 cookie SERVERID
862 balance roundrobin
863 server web1 192.168.1.1 cookie server01
864 server web2 192.168.1.2 cookie server02
willy tarreaueedaa9f2005-12-17 14:08:03 +0100865
866# simultaneous relaying of ports 80, 81 and 8080-8089
867
868 listen http_proxy :80,:81,:8080-8089
willy tarreauc5f73ed2005-12-18 01:26:38 +0100869 mode http
870 cookie SERVERID
871 balance roundrobin
872 server web1 192.168.1.1 cookie server01
873 server web2 192.168.1.2 cookie server02
willy tarreaueedaa9f2005-12-17 14:08:03 +0100874
875# relaying of TCP ports 25, 389 and 663 to ports 1025, 1389 and 1663
876
877 listen http_proxy :25,:389,:663
willy tarreauc5f73ed2005-12-18 01:26:38 +0100878 mode tcp
879 balance roundrobin
880 server srv1 192.168.1.1:+1000
881 server srv2 192.168.1.2:+1000
willy tarreaueedaa9f2005-12-17 14:08:03 +0100882
willy tarreau34f45302006-04-15 21:37:14 +0200883As previously stated, version 1.2.12 brought the 'source' keyword. When this
884keyword is used, the client's IP address is hashed and evenly distributed among
885the available servers so that a same source IP will always go to the same
886server as long as there are no change in the number of available servers. This
887can be used for instance to bind HTTP and HTTPS to the same server. It can also
888be used to improve stickyness when one part of the client population does not
889accept cookies. In this case, only those ones will be perturbated should a
890server fail.
891
892NOTE: It is important to consider the fact that many clients surf the net
893 through proxy farms which assign different IP addresses for each
894 request. Others use dialup connections with a different IP at each
895 connection. Thus, the 'source' parameter should be used with extreme
896 care.
897
898Examples :
899----------
900
901# make a same IP go to the same server whatever the service
902
903 listen http_proxy
904 bind :80,:443
905 mode http
906 balance source
907 server web1 192.168.1.1
908 server web2 192.168.1.2
909
910# try to improve client-server binding by using both source IP and cookie :
911
912 listen http_proxy :80
913 mode http
914 cookie SERVERID
915 balance source
916 server web1 192.168.1.1 cookie server01
917 server web2 192.168.1.2 cookie server02
918
willy tarreaueedaa9f2005-12-17 14:08:03 +0100919
willy tarreau197e8ec2005-12-17 14:10:59 +01009203.1) Server monitoring
921----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +0100922It is possible to check the servers status by trying to establish TCP
923connections or even sending HTTP requests to them. A server which fails to
924reply to health checks as expected will not be used by the load balancing
925algorithms. To enable monitoring, add the 'check' keyword on a server line.
926It is possible to specify the interval between tests (in milliseconds) with
927the 'inter' parameter, the number of failures supported before declaring that
928the server has fallen down with the 'fall' parameter, and the number of valid
929checks needed for the server to fully get up with the 'rise' parameter. Since
930version 1.1.22, it is also possible to send checks to a different port
931(mandatory when none is specified) with the 'port' parameter. The default
932values are the following ones :
933
934 - inter : 2000
935 - rise : 2
936 - fall : 3
937 - port : default server port
938
939The default mode consists in establishing TCP connections only. But in certain
940types of application failures, it is often that the server continues to accept
941connections because the system does it itself while the application is running
942an endless loop, or is completely stuck. So in version 1.1.16 were introduced
943HTTP health checks which only performed simple lightweight requests and analysed
944the response. Now, as of version 1.1.23, it is possible to change the HTTP
945method, the URI, and the HTTP version string (which even allows to send headers
946with a dirty trick). To enable HTTP health-checks, use 'option httpchk'.
947
948By default, requests use the 'OPTIONS' method because it's very light and easy
949to filter from logs, and does it on '/'. Only HTTP responses 2xx and 3xx are
950considered valid ones, and only if they come before the time to send a new
951request is reached ('inter' parameter). If some servers block this type of
952request, 3 other forms help to forge a request :
953
954 - option httpchk -> OPTIONS / HTTP/1.0
955 - option httpchk URI -> OPTIONS <URI> HTTP/1.0
956 - option httpchk METH URI -> <METH> <URI> HTTP/1.0
957 - option httpchk METH URI VER -> <METH> <URI> <VER>
958
959See examples below.
960
961Since version 1.1.17, it is possible to specify backup servers. These servers
962are only sollicited when no other server is available. This may only be useful
963to serve a maintenance page, or define one active and one backup server (seldom
964used in TCP mode). To make a server a backup one, simply add the 'backup' option
965on its line. These servers also support cookies, so if a cookie is specified for
966a backup server, clients assigned to this server will stick to it even when the
967other ones come back. Conversely, if no cookie is assigned to such a server,
968the clients will get their cookies removed (empty cookie = removal), and will
969be balanced against other servers once they come back. Please note that there
Willy TARREAU3481c462006-03-01 22:37:57 +0100970is no load-balancing among backup servers by default. If there are several
971backup servers, the second one will only be used when the first one dies, and
972so on. To force load-balancing between backup servers, specify the 'allbackups'
973option.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100974
975Since version 1.1.17, it is also possible to visually check the status of all
976servers at once. For this, you just have to send a SIGHUP signal to the proxy.
977The servers status will be dumped into the logs at the 'notice' level, as well
978as on <stderr> if not closed. For this reason, it's always a good idea to have
979one local log server at the 'notice' level.
980
willy tarreau982249e2005-12-18 00:57:06 +0100981Since version 1.1.28 and 1.2.1, if an instance loses all its servers, an
willy tarreau0174f312005-12-18 01:02:42 +0100982emergency message will be sent in the logs to inform the administator that an
willy tarreau982249e2005-12-18 00:57:06 +0100983immediate action must be taken.
984
willy tarreau0174f312005-12-18 01:02:42 +0100985Since version 1.1.30 and 1.2.3, several servers can share the same cookie
986value. This is particularly useful in backup mode, to select alternate paths
987for a given server for example, to provide soft-stop, or to direct the clients
988to a temporary page during an application restart. The principle is that when
989a server is dead, the proxy will first look for another server which shares the
990same cookie value for every client which presents the cookie. If there is no
991standard server for this cookie, it will then look for a backup server which
992shares the same name. Please consult the architecture guide for more information.
willy tarreau982249e2005-12-18 00:57:06 +0100993
willy tarreaueedaa9f2005-12-17 14:08:03 +0100994Examples :
995----------
996# same setup as in paragraph 3) with TCP monitoring
997 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +0100998 mode http
999 cookie SERVERID
1000 balance roundrobin
1001 server web1 192.168.1.1:80 cookie server01 check
1002 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
willy tarreaueedaa9f2005-12-17 14:08:03 +01001003
1004# same with HTTP monitoring via 'OPTIONS / HTTP/1.0'
1005 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001006 mode http
1007 cookie SERVERID
1008 balance roundrobin
1009 option httpchk
1010 server web1 192.168.1.1:80 cookie server01 check
1011 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
willy tarreaueedaa9f2005-12-17 14:08:03 +01001012
1013# same with HTTP monitoring via 'OPTIONS /index.html HTTP/1.0'
1014 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001015 mode http
1016 cookie SERVERID
1017 balance roundrobin
1018 option httpchk /index.html
1019 server web1 192.168.1.1:80 cookie server01 check
1020 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
willy tarreaueedaa9f2005-12-17 14:08:03 +01001021
1022# same with HTTP monitoring via 'HEAD /index.jsp? HTTP/1.1\r\nHost: www'
1023 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001024 mode http
1025 cookie SERVERID
1026 balance roundrobin
1027 option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www
1028 server web1 192.168.1.1:80 cookie server01 check
1029 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
willy tarreaueedaa9f2005-12-17 14:08:03 +01001030
willy tarreau0174f312005-12-18 01:02:42 +01001031# Load-balancing with 'prefixed cookie' persistence, and soft-stop using an
1032# alternate port 81 on the server for health-checks.
1033 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001034 mode http
1035 cookie JSESSIONID prefix
1036 balance roundrobin
1037 option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www
1038 server web1-norm 192.168.1.1:80 cookie s1 check port 81
1039 server web2-norm 192.168.1.2:80 cookie s2 check port 81
1040 server web1-stop 192.168.1.1:80 cookie s1 check port 80 backup
1041 server web2-stop 192.168.1.2:80 cookie s2 check port 80 backup
willy tarreau0174f312005-12-18 01:02:42 +01001042
willy tarreaueedaa9f2005-12-17 14:08:03 +01001043# automatic insertion of a cookie in the server's response, and automatic
1044# deletion of the cookie in the client request, while asking upstream caches
1045# not to cache replies.
1046 listen web_appl 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001047 mode http
1048 cookie SERVERID insert nocache indirect
1049 balance roundrobin
1050 server web1 192.168.1.1:80 cookie server01 check
1051 server web2 192.168.1.2:80 cookie server02 check
willy tarreaueedaa9f2005-12-17 14:08:03 +01001052
1053# same with off-site application backup and local error pages server
1054 listen web_appl 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001055 mode http
1056 cookie SERVERID insert nocache indirect
1057 balance roundrobin
1058 server web1 192.168.1.1:80 cookie server01 check
1059 server web2 192.168.1.2:80 cookie server02 check
1060 server web-backup 192.168.2.1:80 cookie server03 check backup
1061 server web-excuse 192.168.3.1:80 check backup
willy tarreaueedaa9f2005-12-17 14:08:03 +01001062
willy tarreauc5f73ed2005-12-18 01:26:38 +01001063# SMTP+TLS relaying with health-checks and backup servers
willy tarreaueedaa9f2005-12-17 14:08:03 +01001064
1065 listen http_proxy :25,:587
willy tarreauc5f73ed2005-12-18 01:26:38 +01001066 mode tcp
1067 balance roundrobin
1068 server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2
1069 server srv2 192.168.1.2 backup
willy tarreaueedaa9f2005-12-17 14:08:03 +01001070
Willy TARREAU3481c462006-03-01 22:37:57 +01001071# Load-balancing using a backup pool (requires haproxy 1.2.9)
1072 listen http_proxy 0.0.0.0:80
1073 mode http
1074 balance roundrobin
1075 option httpchk
1076 server inst1 192.168.1.1:80 cookie s1 check
1077 server inst2 192.168.1.2:80 cookie s2 check
1078 server inst3 192.168.1.3:80 cookie s3 check
1079 server back1 192.168.1.10:80 check backup
1080 server back2 192.168.1.11:80 check backup
1081 option allbackups # all backups will be used
1082
willy tarreaueedaa9f2005-12-17 14:08:03 +01001083
10843.2) Redistribute connections in case of failure
1085------------------------------------------------
1086In HTTP mode, if a server designated by a cookie does not respond, the clients
1087may definitely stick to it because they cannot flush the cookie, so they will
1088not be able to access the service anymore. Specifying 'redispatch' will allow
1089the proxy to break their persistence and redistribute them to working servers.
1090
1091Example :
1092---------
1093 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001094 mode http
1095 cookie SERVERID
1096 dispatch 192.168.1.100:80
1097 server web1 192.168.1.1:80 cookie server01
1098 server web2 192.168.1.2:80 cookie server02
1099 redispatch # send back to dispatch in case of connection failure
willy tarreaueedaa9f2005-12-17 14:08:03 +01001100
1101Up to, and including version 1.1.16, this parameter only applied to connection
1102failures. Since version 1.1.17, it also applies to servers which have been
1103detected as failed by the health check mechanism. Indeed, a server may be broken
1104but still accepting connections, which would not solve every case. But it is
1105possible to conserve the old behaviour, that is, make a client insist on trying
1106to connect to a server even if it is said to be down, by setting the 'persist'
1107option :
1108
1109 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001110 mode http
1111 option persist
1112 cookie SERVERID
1113 dispatch 192.168.1.100:80
1114 server web1 192.168.1.1:80 cookie server01
1115 server web2 192.168.1.2:80 cookie server02
1116 redispatch # send back to dispatch in case of connection failure
willy tarreaueedaa9f2005-12-17 14:08:03 +01001117
1118
willy tarreau34f45302006-04-15 21:37:14 +020011193.3) Assigning different weights to servers
1120-------------------------------------------
1121Sometimes you will need to bring new servers to increase your server farm's
1122capacity, but the new server will be either smaller (emergency use of anything
1123that fits) or bigger (when investing in new hardware). For this reason, it
1124might be wise to be able to send more clients to biggest servers. Till version
11251.2.11, it was necessary to replicate the same server multiple times in the
1126configuration. Starting with 1.2.12, the 'weight' option is available. HAProxy
1127then computes the most homogenous possible map of servers based on their
willy tarreau532bb552006-05-13 18:40:37 +02001128weights so that the load gets distributed as smoothly as possible among them.
1129The weight, between 1 and 256, should reflect one server's capacity relative to
1130others. Weight 1 represents the lowest frequency and 256 the highest. This way,
1131if a server fails, the remaining capacities are still respected.
willy tarreau34f45302006-04-15 21:37:14 +02001132
1133Example :
1134---------
1135# fair distribution among two opterons and one old pentium3
1136
1137 listen web_appl 0.0.0.0:80
1138 mode http
1139 cookie SERVERID insert nocache indirect
1140 balance roundrobin
1141 server pentium3-800 192.168.1.1:80 cookie server01 weight 8 check
1142 server opteron-2.0G 192.168.1.2:80 cookie server02 weight 20 check
1143 server opteron-2.4G 192.168.1.3:80 cookie server03 weight 24 check
1144 server web-backup1 192.168.2.1:80 cookie server04 check backup
1145 server web-excuse 192.168.3.1:80 check backup
1146
1147Notes :
1148-------
1149 - if unspecified, the default weight is 1
1150
1151 - the weight does not impact health checks, so it is cleaner to use weights
1152 than replicating the same server several times
1153
1154 - weights also work on backup servers if the 'allbackups' option is used
1155
1156 - the weights also apply to the source address load balancing
1157 ('balance source').
1158
1159 - whatever the weights, the first server will always be assigned first. This
1160 is helpful for troubleshooting.
1161
1162 - for the purists, the map calculation algorithm gives precedence to first
1163 server, so the map is the most uniform when servers are declared in
1164 ascending order relative to their weights.
1165
willy tarreau532bb552006-05-13 18:40:37 +02001166The load distribution will follow exactly this sequence :
1167
1168 Request| 1 1 1 1
1169 number | 1 2 3 4 5 6 7 8 9 0 1 2 3
1170 --------+---------------------------
1171 p3-800 | X . . . . . . X . . . . .
1172 opt-20 | . X . X . X . . . X . X .
1173 opt-24 | . . X . X . X . X . X . X
1174
1175
11763.4) Limiting the number of concurrent sessions on each server
1177--------------------------------------------------------------
1178Some pre-forked servers such as Apache suffer from too many concurrent
1179sessions, because it's very expensive to run hundreds or thousands of
1180processes on one system. One solution is to increase the number of servers
1181and load-balance between them, but it is a problem when the only goal is
1182to resist to short surges.
1183
1184To solve this problem, a new feature was implemented in HAProxy 1.2.13.
1185It's a per-server 'maxconn', associated with a per-server and a per-proxy
1186queue. This transforms haproxy into a request buffer between the thousands of
1187clients and the few servers. On many circumstances, lowering the maxconn value
1188will increase the server's performance and decrease the overall response times
1189because the servers will be less congested.
1190
1191When a request tries to reach any server, the first non-saturated server is
1192used, respective to the load balancing algorithm. If all servers are saturated,
1193then the request gets queued into the instance's global queue. It will be
1194dequeued once a server will have freed a session and all previously queued
1195requests have been processed.
1196
1197If a request references a particular server (eg: source hashing, or persistence
1198cookie), and if this server is full, then the request will be queued into the
1199server's dedicated queue. This queue has higher priority than the global queue,
1200so it's easier for already registered users to enter the site than for new
1201users.
1202
1203For this, the logs have been enhanced to show the number of sessions per
1204server, the request's position in the queue and the time spent in the queue.
1205This helps doing capacity planning. See the 'logs' section below for more info.
1206
1207Example :
1208---------
1209 # be nice with P3 which only has 256 MB of RAM.
1210 listen web_appl 0.0.0.0:80
1211 maxconn 10000
1212 mode http
1213 cookie SERVERID insert nocache indirect
1214 balance roundrobin
1215 server pentium3-800 192.168.1.1:80 cookie s1 weight 8 maxconn 100 check
1216 server opteron-2.0G 192.168.1.2:80 cookie s2 weight 20 maxconn 300 check
1217 server opteron-2.4G 192.168.1.3:80 cookie s3 weight 24 maxconn 300 check
1218 server web-backup1 192.168.2.1:80 cookie s4 check maxconn 200 backup
1219 server web-excuse 192.168.3.1:80 check backup
1220
willy tarreauf76e6ca2006-05-21 21:09:55 +02001221
1222This was so much efficient at reducing the server's response time that some
1223users wanted to use low values to improve their server's performance. However,
1224they were not able anymore to handle very large loads because it was not
1225possible anymore to saturate the servers. For this reason, version 1.2.14 has
1226brought dynamic limitation with the addition of the parameter 'minconn'. When
1227this parameter is set along with maxconn, it will enable dynamic limitation
1228based on the instance's load. The maximum number of concurrent sessions on a
1229server will be proportionnal to the number of sessions on the instance relative
1230to its maxconn. A minimum of <minconn> will be allowed whatever the load. This
1231will ensure that servers will perform at their best level under normal loads,
1232while still handling surges when needed. The dynamic limit is computed like
1233this :
1234
1235 srv.dyn_limit = max(srv.minconn, srv.maxconn * inst.sess / inst.maxconn)
1236
1237Example :
1238---------
1239 # be nice with P3 which only has 256 MB of RAM.
1240 listen web_appl 0.0.0.0:80
1241 maxconn 10000
1242 mode http
1243 cookie SERVERID insert nocache indirect
1244 balance roundrobin
1245 server pentium3-800 192.168.1.1:80 cookie s1 weight 8 minconn 10 maxconn 100 check
1246 server opteron-2.0G 192.168.1.2:80 cookie s2 weight 20 minconn 30 maxconn 300 check
1247 server opteron-2.4G 192.168.1.3:80 cookie s3 weight 24 minconn 30 maxconn 300 check
1248 server web-backup1 192.168.2.1:80 cookie s4 check maxconn 200 backup
1249 server web-excuse 192.168.3.1:80 check backup
1250
1251In the example above, the server 'pentium3-800' will receive at most 100
1252simultaneous sessions when the proxy instance will reach 10000 sessions, and
1253will receive only 10 simultaneous sessions when the proxy will be under 1000
1254sessions.
1255
willy tarreau532bb552006-05-13 18:40:37 +02001256Notes :
1257-------
1258 - The requests will not stay indefinitely in the queue, they follow the
1259 'contimeout' parameter, and if a request cannot be dequeued within this
1260 timeout because the server is saturated or because the queue is filled,
1261 the session will expire with a 503 error.
1262
willy tarreauf76e6ca2006-05-21 21:09:55 +02001263 - if only <minconn> is specified, it has the same effect as <maxconn>
1264
willy tarreau532bb552006-05-13 18:40:37 +02001265 - setting too low values for maxconn might improve performance but might also
1266 allow slow users to block access to the server for other users.
1267
willy tarreau34f45302006-04-15 21:37:14 +02001268
willy tarreaue0bdd622006-05-21 20:51:54 +020012693.5) Dropping aborted requests
1270------------------------------
1271In presence of very high loads, the servers will take some time to respond. The
1272per-proxy's connection queue will inflate, and the response time will increase
1273respective to the size of the queue times the average per-session response
1274time. When clients will wait for more than a few seconds, they will often hit
1275the 'STOP' button on their browser, leaving a useless request in the queue, and
1276slowing down other users.
1277
1278As there is no way to distinguish between a full STOP and a simple
1279shutdown(SHUT_WR) on the client side, HTTP agents should be conservative and
1280consider that the client might only have closed its output channel while
1281waiting for the response. However, this introduces risks of congestion when
1282lots of users do the same, and is completely useless nowadays because probably
1283no client at all will close the session while waiting for the response. Some
1284HTTP agents support this (Squid, Apache, HAProxy), and others do not (TUX, most
1285hardware-based load balancers). So the probability for a closed input channel
1286to represent a user hitting the 'STOP' button is close to 100%, and it is very
1287tempting to be able to abort the session early without polluting the servers.
1288
1289For this reason, a new option "abortonclose" was introduced in version 1.2.14.
1290By default (without the option) the behaviour is HTTP-compliant. But when the
1291option is specified, a session with an incoming channel closed will be aborted
1292if it's still possible, which means that it's either waiting for a connect() to
1293establish or it is queued waiting for a connection slot. This considerably
1294reduces the queue size and the load on saturated servers when users are tempted
1295to click on STOP, which in turn reduces the response time for other users.
1296
1297Example :
1298---------
1299 listen web_appl 0.0.0.0:80
1300 maxconn 10000
1301 mode http
1302 cookie SERVERID insert nocache indirect
1303 balance roundrobin
1304 server web1 192.168.1.1:80 cookie s1 weight 10 maxconn 100 check
1305 server web2 192.168.1.2:80 cookie s2 weight 10 maxconn 100 check
1306 server web3 192.168.1.3:80 cookie s3 weight 10 maxconn 100 check
1307 server bck1 192.168.2.1:80 cookie s4 check maxconn 200 backup
1308 option abortonclose
1309
1310
willy tarreaueedaa9f2005-12-17 14:08:03 +010013114) Additionnal features
1312=======================
1313
willy tarreau481132e2006-05-21 21:43:10 +02001314Other features are available. They are transparent mode, event logging, header
1315rewriting/filtering, and the status as an HTML page.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001316
willy tarreauc5f73ed2005-12-18 01:26:38 +01001317
willy tarreau0174f312005-12-18 01:02:42 +010013184.1) Network features
willy tarreaueedaa9f2005-12-17 14:08:03 +01001319---------------------
willy tarreau0174f312005-12-18 01:02:42 +010013204.1.1) Transparent mode
1321-----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +01001322In HTTP mode, the 'transparent' keyword allows to intercept sessions which are
1323routed through the system hosting the proxy. This mode was implemented as a
1324replacement for the 'dispatch' mode, since connections without cookie will be
1325sent to the original address while known cookies will be sent to the servers.
1326This mode implies that the system can redirect sessions to a local port.
1327
1328Example :
1329---------
1330 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001331 mode http
1332 transparent
1333 cookie SERVERID
1334 server server01 192.168.1.1:80
1335 server server02 192.168.1.2:80
willy tarreaueedaa9f2005-12-17 14:08:03 +01001336
1337 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
1338 --dport 80 -j REDIRECT --to-ports 65000
1339
1340Note :
1341------
1342If the port is left unspecified on the server, the port the client connected to
1343will be used. This allows to relay a full port range without using transparent
1344mode nor thousands of file descriptors, provided that the system can redirect
1345sessions to local ports.
1346
1347Example :
1348---------
1349 # redirect all ports to local port 65000, then forward to the server on the
1350 # original port.
1351 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001352 mode tcp
1353 server server01 192.168.1.1 check port 60000
1354 server server02 192.168.1.2 check port 60000
willy tarreaueedaa9f2005-12-17 14:08:03 +01001355
1356 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
1357 -j REDIRECT --to-ports 65000
1358
willy tarreau0174f312005-12-18 01:02:42 +010013594.1.2) Per-server source address binding
1360----------------------------------------
1361As of versions 1.1.30 and 1.2.3, it is possible to specify a particular source
1362to reach each server. This is useful when reaching backup servers from a
1363different LAN, or to use an alternate path to reach the same server. It is also
1364usable to provide source load-balancing for outgoing connections. Obviously,
1365the same source address is used to send health-checks.
1366
1367Example :
1368---------
1369 # use a particular source to reach both servers
1370 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001371 mode http
1372 balance roundrobin
1373 server server01 192.168.1.1:80 source 192.168.2.13
1374 server server02 192.168.1.2:80 source 192.168.2.13
willy tarreau0174f312005-12-18 01:02:42 +01001375
1376Example :
1377---------
1378 # use a particular source to reach each servers
1379 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001380 mode http
1381 balance roundrobin
1382 server server01 192.168.1.1:80 source 192.168.1.1
1383 server server02 192.168.2.1:80 source 192.168.2.1
willy tarreau0174f312005-12-18 01:02:42 +01001384
1385Example :
1386---------
1387 # provide source load-balancing to reach the same proxy through 2 WAN links
1388 listen http_proxy 0.0.0.0:65000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001389 mode http
1390 balance roundrobin
1391 server remote-proxy-way1 192.168.1.1:3128 source 192.168.2.1
1392 server remote-proxy-way2 192.168.1.1:3128 source 192.168.3.1
willy tarreau0174f312005-12-18 01:02:42 +01001393
1394Example :
1395---------
1396 # force a TCP connection to bind to a specific port
1397 listen http_proxy 0.0.0.0:2000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001398 mode tcp
1399 balance roundrobin
1400 server srv1 192.168.1.1:80 source 192.168.2.1:20
1401 server srv2 192.168.1.2:80 source 192.168.2.1:20
willy tarreau0174f312005-12-18 01:02:42 +01001402
willy tarreaub952e1d2005-12-18 01:31:20 +010014034.1.3) TCP keep-alive
1404---------------------
1405With version 1.2.7, it becomes possible to enable TCP keep-alives on both the
1406client and server sides. This makes it possible to prevent long sessions from
1407expiring on external layer 4 components such as firewalls and load-balancers.
1408It also allows the system to terminate dead sessions when no timeout has been
1409set (not recommanded). The proxy cannot set the keep-alive probes intervals nor
1410maximal count, consult your operating system manual for this. There are 3
1411options to enable TCP keep-alive :
1412
1413 option tcpka # enables keep-alive both on client and server side
1414 option clitcpka # enables keep-alive only on client side
1415 option srvtcpka # enables keep-alive only on server side
1416
willy tarreaueedaa9f2005-12-17 14:08:03 +01001417
14184.2) Event logging
1419------------------
willy tarreauc5f73ed2005-12-18 01:26:38 +01001420
1421HAProxy's strength certainly lies in its precise logs. It probably provides the
1422finest level of information available for such a product, which is very
1423important for troubleshooting complex environments. Standard log information
1424include client ports, TCP/HTTP state timers, precise session state at
1425termination and precise termination cause, information about decisions to
1426direct trafic to a server, and of course the ability to capture arbitrary
1427headers.
1428
1429In order to improve administrators reactivity, it offers a great transparency
1430about encountered problems, both internal and external, and it is possible to
1431send logs to different sources at the same time with different level filters :
1432
1433 - global process-level logs (system errors, start/stop, etc..)
1434 - per-listener system and internal errors (lack of resource, bugs, ...)
1435 - per-listener external troubles (servers up/down, max connections)
1436 - per-listener activity (client connections), either at the establishment or
1437 at the termination.
1438
1439The ability to distribute different levels of logs to different log servers
1440allow several production teams to interact and to fix their problems as soon
1441as possible. For example, the system team might monitor system-wide errors,
1442while the application team might be monitoring the up/down for their servers in
1443real time, and the security team might analyze the activity logs with one hour
1444delay.
1445
willy tarreauc1cae632005-12-17 14:12:23 +010014464.2.1) Log levels
1447-----------------
willy tarreau197e8ec2005-12-17 14:10:59 +01001448TCP and HTTP connections can be logged with informations such as date, time,
1449source IP address, destination address, connection duration, response times,
1450HTTP request, the HTTP return code, number of bytes transmitted, the conditions
1451in which the session ended, and even exchanged cookies values, to track a
1452particular user's problems for example. All messages are sent to up to two
1453syslog servers. Consult section 1.1 for more info about log facilities. The
1454syntax follows :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001455
willy tarreau197e8ec2005-12-17 14:10:59 +01001456 log <address_1> <facility_1> [max_level_1]
1457 log <address_2> <facility_2> [max_level_2]
1458or
willy tarreaueedaa9f2005-12-17 14:08:03 +01001459 log global
1460
willy tarreau197e8ec2005-12-17 14:10:59 +01001461Note :
1462------
1463The particular syntax 'log global' means that the same log configuration as the
1464'global' section will be used.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001465
willy tarreau197e8ec2005-12-17 14:10:59 +01001466Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001467---------
1468 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001469 mode http
1470 log 192.168.2.200 local3
1471 log 192.168.2.201 local4
willy tarreaueedaa9f2005-12-17 14:08:03 +01001472
willy tarreauc1cae632005-12-17 14:12:23 +010014734.2.2) Log format
1474-----------------
1475By default, connections are logged at the TCP level, as soon as the session
1476establishes between the client and the proxy. By enabling the 'tcplog' option,
1477the proxy will wait until the session ends to generate an enhanced log
1478containing more information such as session duration and its state during the
willy tarreau532bb552006-05-13 18:40:37 +02001479disconnection. The number of remaining session after disconnection is also
1480indicated (for the server, the listener, and the process).
willy tarreauc1cae632005-12-17 14:12:23 +01001481
willy tarreauc5f73ed2005-12-18 01:26:38 +01001482Example of TCP logging :
1483------------------------
willy tarreau982249e2005-12-18 00:57:06 +01001484 listen relais-tcp 0.0.0.0:8000
willy tarreauc5f73ed2005-12-18 01:26:38 +01001485 mode tcp
1486 option tcplog
1487 log 192.168.2.200 local3
willy tarreau982249e2005-12-18 00:57:06 +01001488
willy tarreau532bb552006-05-13 18:40:37 +02001489>>> 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 +01001490
willy tarreau532bb552006-05-13 18:40:37 +02001491 Field Format Example
willy tarreauc5f73ed2005-12-18 01:26:38 +01001492
willy tarreau532bb552006-05-13 18:40:37 +02001493 1 process_name '[' pid ']:' haproxy[18989]:
1494 2 client_ip ':' client_port 127.0.0.1:34550
1495 3 '[' date ']' [15/Oct/2003:15:24:28]
1496 4 listener_name relais-tcp
1497 5 server_name Srv1
1498 6 queue_time '/' connect_time '/' total_time 0/0/5007
1499 7 bytes_read 0
1500 8 termination_state --
1501 9 srv_conn '/' listener_conn '/' process_conn 1/1/1
1502 10 position in srv_queue / listener_queue 0/0
1503
willy tarreau982249e2005-12-18 00:57:06 +01001504
willy tarreauc1cae632005-12-17 14:12:23 +01001505Another option, 'httplog', provides more detailed information about HTTP
1506contents, such as the request and some cookies. In the event where an external
1507component would establish frequent connections to check the service, logs may be
1508full of useless lines. So it is possible not to log any session which didn't
1509transfer any data, by the setting of the 'dontlognull' option. This only has
1510effect on sessions which are established then closed.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001511
willy tarreauc5f73ed2005-12-18 01:26:38 +01001512Example of HTTP logging :
1513-------------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +01001514 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001515 mode http
1516 option httplog
1517 option dontlognull
1518 log 192.168.2.200 local3
1519
willy tarreau532bb552006-05-13 18:40:37 +02001520>>> 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 +01001521
willy tarreauc5f73ed2005-12-18 01:26:38 +01001522More complete example
willy tarreau532bb552006-05-13 18:40:37 +02001523 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 +01001524
willy tarreau532bb552006-05-13 18:40:37 +02001525 Field Format Example
willy tarreauc5f73ed2005-12-18 01:26:38 +01001526
willy tarreau532bb552006-05-13 18:40:37 +02001527 1 process_name '[' pid ']:' haproxy[18989]:
1528 2 client_ip ':' client_port 10.0.0.1:34552
1529 3 '[' date ']' [15/Oct/2003:15:26:31]
1530 4 listener_name relais-http
1531 5 server_name Srv1
1532 6 Tq '/' Tw '/' Tc '/' Tr '/' Tt 3183/-1/-1/-1/11215
1533 7 HTTP_return_code 503
1534 8 bytes_read 0
1535 9 captured_request_cookie -
1536 10 captured_response_cookie -
1537 11 termination_state SC--
1538 12 srv_conn '/' listener_conn '/' process_conn 137/202/205
1539 13 position in srv_queue / listener_queue 0/0
1540 14 '{' captured_request_headers '}' {w.ods.org|Mozilla}
1541 15 '{' captured_response_headers '}' {}
1542 16 '"' HTTP_request '"' "HEAD / HTTP/1.0"
willy tarreauc5f73ed2005-12-18 01:26:38 +01001543
1544Note for log parsers: the URI is ALWAYS the end of the line starting with the
1545 first double quote '"'.
willy tarreau982249e2005-12-18 00:57:06 +01001546
1547The problem when logging at end of connection is that you have no clue about
1548what is happening during very long sessions. To workaround this problem, a
1549new option 'logasap' has been introduced in 1.1.28/1.2.1. When specified, the
1550proxy will log as soon as possible, just before data transfer begins. This means
1551that in case of TCP, it will still log the connection status to the server, and
1552in case of HTTP, it will log just after processing the server headers. In this
1553case, the number of bytes reported is the number of header bytes sent to the
1554client.
1555
1556In order to avoid confusion with normal logs, the total time field and the
1557number of bytes are prefixed with a '+' sign which mean that real numbers are
1558certainly bigger.
1559
1560Example :
1561---------
1562
1563 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01001564 mode http
1565 option httplog
1566 option dontlognull
1567 option logasap
1568 log 192.168.2.200 local3
willy tarreau982249e2005-12-18 00:57:06 +01001569
willy tarreau532bb552006-05-13 18:40:37 +02001570>>> 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 +01001571
willy tarreauc1cae632005-12-17 14:12:23 +010015724.2.3) Timing events
1573--------------------
1574Timers provide a great help in trouble shooting network problems. All values
1575are reported in milliseconds (ms). In HTTP mode, four control points are
willy tarreau532bb552006-05-13 18:40:37 +02001576reported under the form 'Tq/Tw/Tc/Tr/Tt' :
willy tarreauc1cae632005-12-17 14:12:23 +01001577
1578 - Tq: total time to get the client request.
1579 It's the time elapsed between the moment the client connection was accepted
1580 and the moment the proxy received the last HTTP header. The value '-1'
1581 indicates that the end of headers (empty line) has never been seen.
1582
willy tarreau532bb552006-05-13 18:40:37 +02001583 - Tw: total time spent in the queues waiting for a connection slot. It
1584 accounts for listener's queue as well as the server's queue, and depends
1585 on the queue size, and the time needed for the server to complete previous
1586 sessions. The value '-1' means that the request was killed before reaching
1587 the queue.
1588
willy tarreauc1cae632005-12-17 14:12:23 +01001589 - Tc: total time to establish the TCP connection to the server.
1590 It's the time elapsed between the moment the proxy sent the connection
1591 request, and the moment it was acknowledged, or between the TCP SYN packet
1592 and the matching SYN/ACK in return. The value '-1' means that the
1593 connection never established.
1594
1595 - Tr: server response time. It's the time elapsed between the moment the
1596 TCP connection was established to the server and the moment it send its
1597 complete response header. It purely shows its request processing time,
1598 without the network overhead due to the data transmission. The value '-1'
1599 means that the last the response header (empty line) was never seen.
1600
1601 - Tt: total session duration time, between the moment the proxy accepted it
willy tarreau982249e2005-12-18 00:57:06 +01001602 and the moment both ends were closed. The exception is when the 'logasap'
willy tarreau532bb552006-05-13 18:40:37 +02001603 option is specified. In this case, it only equals (Tq+Tw+Tc+Tr), and is
willy tarreau982249e2005-12-18 00:57:06 +01001604 prefixed with a '+' sign. From this field, we can deduce Td, the data
1605 transmission time, by substracting other timers when valid :
willy tarreauc1cae632005-12-17 14:12:23 +01001606
willy tarreau532bb552006-05-13 18:40:37 +02001607 Td = Tt - (Tq + Tw + Tc + Tr)
willy tarreauc1cae632005-12-17 14:12:23 +01001608
1609 Timers with '-1' values have to be excluded from this equation.
1610
willy tarreau532bb552006-05-13 18:40:37 +02001611In TCP mode ('option tcplog'), only Tw, Tc and Tt are reported.
willy tarreauc1cae632005-12-17 14:12:23 +01001612
1613These timers provide precious indications on trouble causes. Since the TCP
1614protocol defines retransmit delays of 3, 6, 12... seconds, we know for sure
1615that timers close to multiples of 3s are nearly always related to packets lost
1616due to network problems (wires or negociation). Moreover, if <Tt> is close to
1617a timeout value specified in the configuration, it often means that a session
1618has been aborted on time-out.
1619
1620Most common cases :
1621
1622 - If Tq is close to 3000, a packet has probably been lost between the client
1623 and the proxy.
1624 - If Tc is close to 3000, a packet has probably been lost between the server
1625 and the proxy during the server connection phase. This one should always be
1626 very low (less than a few tens).
1627 - If Tr is nearly always lower than 3000 except some rare values which seem to
1628 be the average majored by 3000, there are probably some packets lost between
1629 the proxy and the server.
1630 - If Tt is often slightly higher than a time-out, it's often because the
1631 client and the server use HTTP keep-alive and the session is maintained
1632 after the response ends. Se further for how to disable HTTP keep-alive.
1633
1634Other cases ('xx' means any value to be ignored) :
willy tarreau532bb552006-05-13 18:40:37 +02001635 -1/xx/xx/xx/Tt: the client was not able to send its complete request in time,
1636 or that it aborted it too early.
1637 Tq/-1/xx/xx/Tt: it was not possible to process the request, maybe because
1638 servers were out of order.
1639 Tq/Tw/-1/xx/Tt: the connection could not establish on the server. Either it
1640 refused it or it timed out after Tt-(Tq+Tw) ms.
1641 Tq/Tw/Tc/-1/Tt: the server has accepted the connection but did not return a
1642 complete response in time, or it closed its connexion
1643 unexpectedly, after Tt-(Tq+Tw+Tc) ms.
willy tarreauc1cae632005-12-17 14:12:23 +01001644
16454.2.4) Session state at disconnection
1646-------------------------------------
willy tarreauc5f73ed2005-12-18 01:26:38 +01001647TCP and HTTP logs provide a session completion indicator in the
1648<termination_state> field, just before the number of active
1649connections. It is 2-characters long in TCP, and 4-characters long in
1650HTTP, each of which has a special meaning :
1651
willy tarreau197e8ec2005-12-17 14:10:59 +01001652 - On the first character, a code reporting the first event which caused the
1653 session to terminate :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001654
willy tarreauc5f73ed2005-12-18 01:26:38 +01001655 C : the TCP session was unexpectedly aborted by the client.
1656
1657 S : the TCP session was unexpectedly aborted by the server, or the
1658 server explicitly refused it.
1659
1660 P : the session was prematurely aborted by the proxy, because of a
1661 connection limit enforcement, because a DENY filter was matched,
1662 or because of a security check which detected and blocked a
1663 dangerous error in server response which might have caused
1664 information leak (eg: cacheable cookie).
1665
1666 R : a resource on the proxy has been exhausted (memory, sockets, source
1667 ports, ...). Usually, this appears during the connection phase, and
1668 system logs should contain a copy of the precise error.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001669
willy tarreauc5f73ed2005-12-18 01:26:38 +01001670 I : an internal error was identified by the proxy during a self-check.
1671 This should NEVER happen, and you are encouraged to report any log
1672 containing this, because this is a bug.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001673
willy tarreauc5f73ed2005-12-18 01:26:38 +01001674 c : the client-side time-out expired first.
1675
1676 s : the server-side time-out expired first.
1677
1678 - : normal session completion.
1679
1680 - on the second character, the TCP/HTTP session state when it was closed :
1681
1682 R : waiting for complete REQUEST from the client (HTTP only). Nothing
1683 was sent to any server.
1684
willy tarreau532bb552006-05-13 18:40:37 +02001685 Q : waiting in the QUEUE for a connection slot. This can only happen on
1686 servers which have a 'maxconn' parameter set. No connection attempt
1687 was made to any server.
1688
willy tarreauc5f73ed2005-12-18 01:26:38 +01001689 C : waiting for CONNECTION to establish on the server. The server might
1690 at most have noticed a connection attempt.
1691
1692 H : waiting for, receiving and processing server HEADERS (HTTP only).
1693
1694 D : the session was in the DATA phase.
1695
1696 L : the proxy was still transmitting LAST data to the client while the
1697 server had already finished.
1698
1699 - : normal session completion after end of data transfer.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001700
willy tarreau197e8ec2005-12-17 14:10:59 +01001701 - the third character tells whether the persistence cookie was provided by
willy tarreauc1cae632005-12-17 14:12:23 +01001702 the client (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001703
willy tarreauc5f73ed2005-12-18 01:26:38 +01001704 N : the client provided NO cookie. This is usually the case on new
1705 connections.
1706
1707 I : the client provided an INVALID cookie matching no known
1708 server. This might be caused by a recent configuration change,
1709 mixed cookies between HTTP/HTTPS sites, or an attack.
1710
1711 D : the client provided a cookie designating a server which was DOWN,
1712 so either the 'persist' option was used and the client was sent to
1713 this server, or it was not set and the client was redispatched to
1714 another server.
1715
1716 V : the client provided a valid cookie, and was sent to the associated
1717 server.
1718
1719 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001720
willy tarreau197e8ec2005-12-17 14:10:59 +01001721 - the last character reports what operations were performed on the persistence
willy tarreauc1cae632005-12-17 14:12:23 +01001722 cookie returned by the server (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001723
willy tarreauc5f73ed2005-12-18 01:26:38 +01001724 N : NO cookie was provided by the server, and none was inserted either.
1725
1726 I : no cookie was provided by the server, and the proxy INSERTED one.
1727
willy tarreau197e8ec2005-12-17 14:10:59 +01001728 P : a cookie was PROVIDED by the server and transmitted as-is.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001729
willy tarreauc5f73ed2005-12-18 01:26:38 +01001730 R : the cookie provided by the server was REWRITTEN by the proxy.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001731
willy tarreauc5f73ed2005-12-18 01:26:38 +01001732 D : the cookie provided by the server was DELETED by the proxy.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001733
willy tarreauc5f73ed2005-12-18 01:26:38 +01001734 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001735
willy tarreauc5f73ed2005-12-18 01:26:38 +01001736The combination of the two first flags give a lot of information about what was
1737happening when the session terminated. It can be helpful to detect server
1738saturation, network troubles, local system resource starvation, attacks, etc...
willy tarreaueedaa9f2005-12-17 14:08:03 +01001739
willy tarreauc5f73ed2005-12-18 01:26:38 +01001740The most common termination flags combinations are indicated here.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001741
willy tarreauc5f73ed2005-12-18 01:26:38 +01001742 Flags Reason
1743 CR The client aborted before sending a full request. Most probably the
1744 request was done by hand using a telnet client, and aborted early.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001745
willy tarreauc5f73ed2005-12-18 01:26:38 +01001746 cR The client timed out before sending a full request. This is sometimes
1747 caused by too large TCP MSS values on the client side for PPPoE
1748 networks which cannot transport full-sized packets, or by clients
1749 sending requests by hand and not typing fast enough.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001750
willy tarreauc5f73ed2005-12-18 01:26:38 +01001751 SC The server explicitly refused the connection (the proxy received a
1752 TCP RST or an ICMP in return). Under some circumstances, it can
1753 also be the network stack telling the proxy that the server is
1754 unreachable (eg: no route, or no ARP response on local network).
willy tarreau982249e2005-12-18 00:57:06 +01001755
willy tarreauc5f73ed2005-12-18 01:26:38 +01001756 sC The connection to the server did not complete during contimeout.
willy tarreau982249e2005-12-18 00:57:06 +01001757
willy tarreauc5f73ed2005-12-18 01:26:38 +01001758 PC The proxy refused to establish a connection to the server because the
1759 maxconn limit has been reached. The listener's maxconn parameter may
1760 be increased in the proxy configuration, as well as the global
1761 maxconn parameter.
willy tarreauc1cae632005-12-17 14:12:23 +01001762
willy tarreauc5f73ed2005-12-18 01:26:38 +01001763 RC A local resource has been exhausted (memory, sockets, source ports)
1764 preventing the connection to the server from establishing. The error
1765 logs will tell precisely what was missing. Anyway, this can only be
1766 solved by system tuning.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001767
willy tarreauc5f73ed2005-12-18 01:26:38 +01001768 cH The client timed out during a POST request. This is sometimes caused
1769 by too large TCP MSS values for PPPoE networks which cannot transport
1770 full-sized packets.
willy tarreauc1cae632005-12-17 14:12:23 +01001771
willy tarreau078c79a2006-05-13 12:23:58 +02001772 CH The client aborted while waiting for the server to start responding.
1773 It might be the server taking too long to respond or the client
1774 clicking the 'Stop' button too fast.
1775
1776 CQ The client aborted while its session was queued, waiting for a server
1777 with enough empty slots to accept it. It might be that either all the
1778 servers were saturated or the assigned server taking too long to
1779 respond.
1780
1781 sQ The session spent too much time in queue and has been expired.
1782
willy tarreauc5f73ed2005-12-18 01:26:38 +01001783 SH The server aborted before sending its full headers, or it crashed.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001784
willy tarreauc5f73ed2005-12-18 01:26:38 +01001785 sH The server failed to reply during the srvtimeout delay, which
1786 indicates too long transactions, probably caused by back-end
1787 saturation. The only solutions are to fix the problem on the
1788 application or to increase the 'srvtimeout' parameter to support
1789 longer delays (at the risk of the client giving up anyway).
1790
1791 PR The proxy blocked the client's request, either because of an invalid
1792 HTTP syntax, in which case it returned an HTTP 400 error to the
1793 client, or because a deny filter matched, in which case it returned
1794 an HTTP 403 error.
1795
1796 PH The proxy blocked the server's response, because it was invalid,
1797 incomplete, dangerous (cache control), or matched a security filter.
1798 In any case, an HTTP 502 error is sent to the client.
1799
1800 cD The client did not read any data for as long as the clitimeout delay.
1801 This is often caused by network failures on the client side.
1802
1803 CD The client unexpectedly aborted during data transfer. This is either
1804 caused by a browser crash, or by a keep-alive session between the
1805 server and the client terminated first by the client.
1806
1807 sD The server did nothing during the srvtimeout delay. This is often
1808 caused by too short timeouts on L4 equipements before the server
1809 (firewalls, load-balancers, ...).
1810
18114.2.5) Non-printable characters
willy tarreau4302f492005-12-18 01:00:37 +01001812-------------------------------
1813As of version 1.1.29, non-printable characters are not sent as-is into log
1814files, but are converted to their two-digits hexadecimal representation,
1815prefixed by the character '#'. The only characters that can now be logged
1816without being escaped are between 32 and 126 (inclusive). Obviously, the
1817escape character '#' is also encoded to avoid any ambiguity. It is the same for
1818the character '"', as well as '{', '|' and '}' when logging headers.
1819
willy tarreauc5f73ed2005-12-18 01:26:38 +010018204.2.6) Capturing HTTP headers and cookies
1821-----------------------------------------
1822Version 1.1.23 brought cookie capture, and 1.1.29 the header capture. All this
1823is performed using the 'capture' keyword.
1824
1825Cookie capture makes it easy to track a complete user session. The syntax is :
1826
1827 capture cookie <cookie_prefix> len <capture_length>
1828
1829This will enable cookie capture from both requests and responses. This way,
1830it's easy to detect when a user switches to a new session for example, because
1831the server will reassign it a new cookie.
1832
1833The FIRST cookie whose name starts with <cookie_prefix> will be captured, and
1834logged as 'NAME=value', without exceeding <capture_length> characters (64 max).
1835When the cookie name is fixed and known, it's preferable to suffix '=' to it to
1836ensure that no other cookie will be logged.
1837
1838Examples :
1839----------
1840 # capture the first cookie whose name starts with "ASPSESSION"
1841 capture cookie ASPSESSION len 32
1842
1843 # capture the first cookie whose name is exactly "vgnvisitor"
1844 capture cookie vgnvisitor= len 32
1845
1846In the logs, the field preceeding the completion indicator contains the cookie
1847value as sent by the server, preceeded by the cookie value as sent by the
1848client. Each of these field is replaced with '-' when no cookie was seen or
1849when the option is disabled.
1850
1851Header captures have a different goal. They are useful to track unique request
1852identifiers set by a previous proxy, virtual host names, user-agents, POST
1853content-length, referrers, etc. In the response, one can search for information
1854about the response length, how the server asked the cache to behave, or an
1855object location during a redirection. As for cookie captures, it is both
1856possible to include request headers and response headers at the same time. The
1857syntax is :
willy tarreau4302f492005-12-18 01:00:37 +01001858
1859 capture request header <name> len <max length>
1860 capture response header <name> len <max length>
1861
1862Note: Header names are not case-sensitive.
1863
1864Examples:
1865---------
1866 # keep the name of the virtual server
1867 capture request header Host len 20
1868 # keep the amount of data uploaded during a POST
1869 capture request header Content-Length len 10
1870
1871 # note the expected cache behaviour on the response
1872 capture response header Cache-Control len 8
1873 # note the URL location during a redirection
1874 capture response header Location len 20
1875
1876Non-existant headers are logged as empty strings, and if one header appears more
1877than once, only its last occurence will be kept. Request headers are grouped
1878within braces '{' and '}' in the same order as they were declared, and delimited
1879with a vertical bar '|' without any space. Response headers follow the same
1880representation, but are displayed after a space following the request headers
1881block. These blocks are displayed just before the HTTP request in the logs.
willy tarreauc5f73ed2005-12-18 01:26:38 +01001882
willy tarreau4302f492005-12-18 01:00:37 +01001883Example :
1884
willy tarreauc5f73ed2005-12-18 01:26:38 +01001885 Config:
1886
1887 capture request header Host len 20
1888 capture request header Content-Length len 10
1889 capture request header Referer len 20
1890 capture response header Server len 20
1891 capture response header Content-Length len 10
1892 capture response header Cache-Control len 8
1893 capture response header Via len 20
1894 capture response header Location len 20
1895
1896 Log :
1897
willy tarreau532bb552006-05-13 18:40:37 +02001898 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/"
1899 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"
1900 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 +01001901
1902
19034.2.7) Examples of logs
1904-----------------------
willy tarreau532bb552006-05-13 18:40:37 +02001905- 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 +01001906 => long request (6.5s) entered by hand through 'telnet'. The server replied
1907 in 147 ms, and the session ended normally ('----')
1908
willy tarreau532bb552006-05-13 18:40:37 +02001909- 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"
1910 => Idem, but the request was queued in the global queue behind 9 other
1911 requests, and waited there for 1230 ms.
1912
1913- 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 +01001914 => request for a long data transfer. The 'logasap' option was specified, so
1915 the log was produced just before transfering data. The server replied in
1916 14 ms, 243 bytes of headers were sent to the client, and total time from
1917 accept to first data byte is 30 ms.
1918
willy tarreau532bb552006-05-13 18:40:37 +02001919- 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 +01001920 => the proxy blocked a server response either because of an 'rspdeny' or
1921 'rspideny' filter, or because it blocked sensible information which risked
1922 being cached. In this case, the response is replaced with a '502 bad
1923 gateway'.
1924
willy tarreau532bb552006-05-13 18:40:37 +02001925- 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 +01001926 => the client never completed its request and aborted itself ('C---') after
1927 8.5s, while the proxy was waiting for the request headers ('-R--').
1928 Nothing was sent to the server.
1929
willy tarreau532bb552006-05-13 18:40:37 +02001930- 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 +01001931 => The client never completed its request, which was aborted by the time-out
1932 ('c---') after 50s, while the proxy was waiting for the request headers ('-R--').
1933 Nothing was sent to the server, but the proxy could send a 408 return code
1934 to the client.
willy tarreau4302f492005-12-18 01:00:37 +01001935
willy tarreau532bb552006-05-13 18:40:37 +02001936- 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 +01001937 => This is a 'tcplog' entry. Client-side time-out ('c----') occured after 5s.
willy tarreau4302f492005-12-18 01:00:37 +01001938
willy tarreau532bb552006-05-13 18:40:37 +02001939- 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 +01001940 => The request took 3s to complete (probably a network problem), and the
1941 connection to the server failed ('SC--') after 4 attemps of 2 seconds
1942 (config says 'retries 3'), then a 503 error code was sent to the client.
willy tarreau532bb552006-05-13 18:40:37 +02001943 There were 115 connections on this server, 202 connections on this proxy,
1944 and 205 on the global process. It is possible that the server refused the
1945 connection because of too many already established.
willy tarreau4302f492005-12-18 01:00:37 +01001946
willy tarreau4302f492005-12-18 01:00:37 +01001947
willy tarreau197e8ec2005-12-17 14:10:59 +010019484.3) HTTP header manipulation
1949-----------------------------
1950In HTTP mode, it is possible to rewrite, add or delete some of the request and
1951response headers based on regular expressions. It is also possible to block a
1952request or a response if a particular header matches a regular expression,
1953which is enough to stops most elementary protocol attacks, and to protect
1954against information leak from the internal network. But there is a limitation
1955to this : since haproxy's HTTP engine knows nothing about keep-alive, only
1956headers passed during the first request of a TCP session will be seen. All
1957subsequent headers will be considered data only and not analyzed. Furthermore,
1958haproxy doesn't touch data contents, it stops at the end of headers.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001959
willy tarreau197e8ec2005-12-17 14:10:59 +01001960The syntax is :
1961 reqadd <string> to add a header to the request
1962 reqrep <search> <replace> to modify the request
1963 reqirep <search> <replace> same, but ignoring the case
1964 reqdel <search> to delete a header in the request
1965 reqidel <search> same, but ignoring the case
1966 reqallow <search> definitely allow a request if a header matches <search>
1967 reqiallow <search> same, but ignoring the case
1968 reqdeny <search> denies a request if a header matches <search>
1969 reqideny <search> same, but ignoring the case
1970 reqpass <search> ignore a header matching <search>
1971 reqipass <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01001972
willy tarreau197e8ec2005-12-17 14:10:59 +01001973 rspadd <string> to add a header to the response
1974 rsprep <search> <replace> to modify the response
1975 rspirep <search> <replace> same, but ignoring the case
1976 rspdel <search> to delete the response
1977 rspidel <search> same, but ignoring the case
willy tarreau982249e2005-12-18 00:57:06 +01001978 rspdeny <search> replaces a response with a HTTP 502 if a header matches <search>
1979 rspideny <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01001980
1981
willy tarreau197e8ec2005-12-17 14:10:59 +01001982<search> is a POSIX regular expression (regex) which supports grouping through
1983parenthesis (without the backslash). Spaces and other delimiters must be
1984prefixed with a backslash ('\') to avoid confusion with a field delimiter.
1985Other characters may be prefixed with a backslash to change their meaning :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001986
willy tarreau197e8ec2005-12-17 14:10:59 +01001987 \t for a tab
1988 \r for a carriage return (CR)
1989 \n for a new line (LF)
1990 \ to mark a space and differentiate it from a delimiter
1991 \# to mark a sharp and differentiate it from a comment
1992 \\ to use a backslash in a regex
1993 \\\\ to use a backslash in the text (*2 for regex, *2 for haproxy)
1994 \xXX to write the ASCII hex code XX as in the C language
willy tarreaueedaa9f2005-12-17 14:08:03 +01001995
1996
willy tarreau197e8ec2005-12-17 14:10:59 +01001997<replace> containst the string to be used to replace the largest portion of text
1998matching the regex. It can make use of the special characters above, and can
1999reference a substring delimited by parenthesis in the regex, by the group
2000numerical order from 1 to 9. In this case, you would write a backslah ('\')
2001immediately followed by one digit indicating the group position.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002002
willy tarreau197e8ec2005-12-17 14:10:59 +01002003<string> represents the string which will systematically be added after the last
2004header line. It can also use special characters above.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002005
willy tarreau197e8ec2005-12-17 14:10:59 +01002006Notes :
2007-------
2008 - the first line is considered as a header, which makes it possible to rewrite
2009 or filter HTTP requests URIs or response codes.
2010 - 'reqrep' is the equivalent of 'cliexp' in version 1.0, and 'rsprep' is the
2011 equivalent of 'srvexp' in 1.0. Those names are still supported but
2012 deprecated.
2013 - for performances reasons, the number of characters added to a request or to
2014 a response is limited to 4096 since version 1.1.5 (it was 256 before). This
2015 value is easy to modify in the code if needed (#define). If it is too short
2016 on occasional uses, it is possible to gain some space by removing some
2017 useless headers before adding new ones.
willy tarreau982249e2005-12-18 00:57:06 +01002018 - a denied request will generate an "HTTP 403 forbidden" response, while a
2019 denied response will generate an "HTTP 502 Bad gateway" response.
2020
willy tarreaueedaa9f2005-12-17 14:08:03 +01002021
willy tarreau197e8ec2005-12-17 14:10:59 +01002022Examples :
2023----------
willy tarreauc5f73ed2005-12-18 01:26:38 +01002024 ###### a few examples ######
willy tarreau197e8ec2005-12-17 14:10:59 +01002025
willy tarreauc5f73ed2005-12-18 01:26:38 +01002026 # rewrite 'online.fr' instead of 'free.fr' for GET and POST requests
2027 reqrep ^(GET\ .*)(.free.fr)(.*) \1.online.fr\3
2028 reqrep ^(POST\ .*)(.free.fr)(.*) \1.online.fr\3
willy tarreau197e8ec2005-12-17 14:10:59 +01002029
willy tarreauc5f73ed2005-12-18 01:26:38 +01002030 # force proxy connections to close
2031 reqirep ^Proxy-Connection:.* Proxy-Connection:\ close
2032 # rewrite locations
2033 rspirep ^(Location:\ )([^:]*://[^/]*)(.*) \1\3
willy tarreaueedaa9f2005-12-17 14:08:03 +01002034
willy tarreauc5f73ed2005-12-18 01:26:38 +01002035 ###### A full configuration being used on production ######
willy tarreaueedaa9f2005-12-17 14:08:03 +01002036
willy tarreau197e8ec2005-12-17 14:10:59 +01002037 # Every header should end with a colon followed by one space.
willy tarreauc5f73ed2005-12-18 01:26:38 +01002038 reqideny ^[^:\ ]*[\ ]*$
willy tarreaueedaa9f2005-12-17 14:08:03 +01002039
willy tarreau197e8ec2005-12-17 14:10:59 +01002040 # block Apache chunk exploit
willy tarreauc5f73ed2005-12-18 01:26:38 +01002041 reqideny ^Transfer-Encoding:[\ ]*chunked
2042 reqideny ^Host:\ apache-
willy tarreaueedaa9f2005-12-17 14:08:03 +01002043
willy tarreau197e8ec2005-12-17 14:10:59 +01002044 # block annoying worms that fill the logs...
willy tarreauc5f73ed2005-12-18 01:26:38 +01002045 reqideny ^[^:\ ]*\ .*(\.|%2e)(\.|%2e)(%2f|%5c|/|\\\\)
2046 reqideny ^[^:\ ]*\ ([^\ ]*\ [^\ ]*\ |.*%00)
2047 reqideny ^[^:\ ]*\ .*<script
2048 reqideny ^[^:\ ]*\ .*/(root\.exe\?|cmd\.exe\?|default\.ida\?)
willy tarreau197e8ec2005-12-17 14:10:59 +01002049
2050 # allow other syntactically valid requests, and block any other method
willy tarreauc5f73ed2005-12-18 01:26:38 +01002051 reqipass ^(GET|POST|HEAD|OPTIONS)\ /.*\ HTTP/1\.[01]$
2052 reqipass ^OPTIONS\ \\*\ HTTP/1\.[01]$
2053 reqideny ^[^:\ ]*\
willy tarreau197e8ec2005-12-17 14:10:59 +01002054
2055 # force connection:close, thus disabling HTTP keep-alive
willy tarreauc5f73ed2005-12-18 01:26:38 +01002056 option httpclose
willy tarreau197e8ec2005-12-17 14:10:59 +01002057
willy tarreauc5f73ed2005-12-18 01:26:38 +01002058 # change the server name
2059 rspidel ^Server:\
2060 rspadd Server:\ Formilux/0.1.8
willy tarreau197e8ec2005-12-17 14:10:59 +01002061
2062
willy tarreau982249e2005-12-18 00:57:06 +01002063Also, the 'forwardfor' option creates an HTTP 'X-Forwarded-For' header which
willy tarreauc1cae632005-12-17 14:12:23 +01002064contains the client's IP address. This is useful to let the final web server
2065know what the client address was (eg for statistics on domains).
2066
willy tarreau982249e2005-12-18 00:57:06 +01002067Last, the 'httpclose' option removes any 'Connection' header both ways, and
2068adds a 'Connection: close' header in each direction. This makes it easier to
Willy TARREAU767ba712006-03-01 22:40:50 +01002069disable HTTP keep-alive than the previous 4-rules block.
willy tarreau982249e2005-12-18 00:57:06 +01002070
willy tarreauc1cae632005-12-17 14:12:23 +01002071Example :
2072---------
2073 listen http_proxy 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01002074 mode http
2075 log global
2076 option httplog
2077 option dontlognull
2078 option forwardfor
2079 option httpclose
2080
Willy TARREAU767ba712006-03-01 22:40:50 +01002081Note that some HTTP servers do not necessarily close the connections when they
2082receive the 'Connection: close', and if the client does not close either, then
2083the connection will be maintained up to the time-out. This translates into high
2084number of simultaneous sessions and high global session times in the logs. To
2085workaround this, a new option 'forceclose' appeared in version 1.2.9 to enforce
2086the closing of the outgoing server channel as soon as the server begins to
2087reply and only if the request buffer is empty. Note that this should NOT be
2088used if CONNECT requests are expected between the client and the server. The
2089'forceclose' option implies the 'httpclose' option.
2090
2091Example :
2092---------
2093 listen http_proxy 0.0.0.0:80
2094 mode http
2095 log global
2096 option httplog
2097 option dontlognull
2098 option forwardfor
2099 option forceclose
2100
willy tarreau197e8ec2005-12-17 14:10:59 +01002101
21024.4) Load balancing with persistence
2103------------------------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01002104Combining cookie insertion with internal load balancing allows to transparently
2105bring persistence to applications. The principle is quite simple :
2106 - assign a cookie value to each server
2107 - enable the load balancing between servers
2108 - insert a cookie into responses resulting from the balancing algorithm
2109 (indirect accesses), end ensure that no upstream proxy will cache it.
2110 - remove the cookie in the request headers so that the application never sees
2111 it.
2112
2113Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002114---------
2115 listen application 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01002116 mode http
2117 cookie SERVERID insert nocache indirect
2118 balance roundrobin
2119 server srv1 192.168.1.1:80 cookie server01 check
2120 server srv2 192.168.1.2:80 cookie server02 check
willy tarreaueedaa9f2005-12-17 14:08:03 +01002121
willy tarreau0174f312005-12-18 01:02:42 +01002122The other solution brought by versions 1.1.30 and 1.2.3 is to reuse a cookie
2123from the server, and prefix the server's name to it. In this case, don't forget
2124to force "httpclose" mode so that you can be assured that every subsequent
2125request will have its cookie fixed.
2126
2127 listen application 0.0.0.0:80
willy tarreauc5f73ed2005-12-18 01:26:38 +01002128 mode http
2129 cookie JSESSIONID prefix
2130 balance roundrobin
2131 server srv1 192.168.1.1:80 cookie srv1 check
2132 server srv2 192.168.1.2:80 cookie srv2 check
2133 option httpclose
willy tarreau0174f312005-12-18 01:02:42 +01002134
2135
willy tarreau982249e2005-12-18 00:57:06 +010021364.5) Protection against information leak from the servers
2137---------------------------------------------------------
2138In versions 1.1.28/1.2.1, a new option 'checkcache' was created. It carefully
2139checks 'Cache-control', 'Pragma' and 'Set-cookie' headers in server response
2140to check if there's a risk of caching a cookie on a client-side proxy. When this
2141option is enabled, the only responses which can be delivered to the client are :
2142 - all those without 'Set-Cookie' header ;
2143 - all those with a return code other than 200, 203, 206, 300, 301, 410,
2144 provided that the server has not set a 'Cache-control: public' header ;
2145 - all those that come from a POST request, provided that the server has not
2146 set a 'Cache-Control: public' header ;
2147 - those with a 'Pragma: no-cache' header
2148 - those with a 'Cache-control: private' header
2149 - those with a 'Cache-control: no-store' header
2150 - those with a 'Cache-control: max-age=0' header
2151 - those with a 'Cache-control: s-maxage=0' header
2152 - those with a 'Cache-control: no-cache' header
2153 - those with a 'Cache-control: no-cache="set-cookie"' header
2154 - those with a 'Cache-control: no-cache="set-cookie,' header
2155 (allowing other fields after set-cookie)
willy tarreaueedaa9f2005-12-17 14:08:03 +01002156
willy tarreau982249e2005-12-18 00:57:06 +01002157If a response doesn't respect these requirements, then it will be blocked just
2158as if it was from an 'rspdeny' filter, with an "HTTP 502 bad gateway". The
2159session state shows "PH--" meaning that the proxy blocked the response during
2160headers processing. Additionnaly, an alert will be sent in the logs so that
2161admins are told that there's something to be done.
2162
willy tarreauc5f73ed2005-12-18 01:26:38 +01002163
willy tarreau982249e2005-12-18 00:57:06 +010021644.6) Customizing errors
2165-----------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01002166Some situations can make haproxy return an HTTP error code to the client :
2167 - invalid or too long request => HTTP 400
2168 - request not completely sent in time => HTTP 408
2169 - forbidden request (matches a deny filter) => HTTP 403
2170 - internal error in haproxy => HTTP 500
2171 - the server returned an invalid or incomplete response => HTTP 502
2172 - no server was available to handle the request => HTTP 503
2173 - the server failed to reply in time => HTTP 504
willy tarreaueedaa9f2005-12-17 14:08:03 +01002174
willy tarreau197e8ec2005-12-17 14:10:59 +01002175A succint error message taken from the RFC accompanies these return codes.
2176But depending on the clients knowledge, it may be better to return custom, user
2177friendly, error pages. This is made possible through the use of the 'errorloc'
2178command :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002179
willy tarreau197e8ec2005-12-17 14:10:59 +01002180 errorloc <HTTP_code> <location>
willy tarreaueedaa9f2005-12-17 14:08:03 +01002181
willy tarreau197e8ec2005-12-17 14:10:59 +01002182Instead of generating an HTTP error <HTTP_code> among those above, the proxy
2183will return a temporary redirection code (HTTP 302) towards the address
2184specified in <location>. This address may be either relative to the site or
2185absolute. Since this request will be handled by the client's browser, it's
2186mandatory that the returned address be reachable from the outside.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002187
willy tarreau197e8ec2005-12-17 14:10:59 +01002188Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002189---------
2190 listen application 0.0.0.0:80
2191 errorloc 400 /badrequest.html
2192 errorloc 403 /forbidden.html
2193 errorloc 408 /toolong.html
willy tarreauc5f73ed2005-12-18 01:26:38 +01002194 errorloc 500 http://haproxy.domain.net/bugreport.html
willy tarreaueedaa9f2005-12-17 14:08:03 +01002195 errorloc 502 http://192.168.114.58/error50x.html
2196 errorloc 503 http://192.168.114.58/error50x.html
2197 errorloc 504 http://192.168.114.58/error50x.html
2198
willy tarreauc1f47532005-12-18 01:08:26 +01002199Note: RFC2616 says that a client must reuse the same method to fetch the
2200Location returned by a 302, which causes problems with the POST method.
2201The return code 303 was designed explicitly to force the client to fetch the
2202Location URL with the GET method, but there are some browsers pre-dating
2203HTTP/1.1 which don't support it. Anyway, most browsers still behave with 302 as
willy tarreauc5f73ed2005-12-18 01:26:38 +01002204if it was a 303. In order to allow the user to chose, versions 1.1.31 and 1.2.5
2205bring two new keywords to replace 'errorloc' : 'errorloc302' and 'errorloc303'.
willy tarreauc1f47532005-12-18 01:08:26 +01002206
2207They are preffered over errorloc (which still does 302). Consider using
2208errorloc303 everytime you know that your clients support HTTP 303 responses..
2209
2210
willy tarreau982249e2005-12-18 00:57:06 +010022114.7) Modifying default values
willy tarreau197e8ec2005-12-17 14:10:59 +01002212-----------------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01002213Version 1.1.22 introduced the notion of default values, which eliminates the
2214pain of often repeating common parameters between many instances, such as
2215logs, timeouts, modes, etc...
willy tarreaueedaa9f2005-12-17 14:08:03 +01002216
willy tarreau197e8ec2005-12-17 14:10:59 +01002217Default values are set in a 'defaults' section. Each of these section clears
2218all previously set default parameters, so there may be as many default
2219parameters as needed. Only the last one before a 'listen' section will be
2220used for this section. The 'defaults' section uses the same syntax as the
2221'listen' section, for the supported parameters. The 'defaults' keyword ignores
2222everything on its command line, so that fake instance names can be specified
2223there for better clarity.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002224
willy tarreau982249e2005-12-18 00:57:06 +01002225In version 1.1.28/1.2.1, only those parameters can be preset in the 'default'
willy tarreau197e8ec2005-12-17 14:10:59 +01002226section :
2227 - log (the first and second one)
willy tarreaueedaa9f2005-12-17 14:08:03 +01002228 - mode { tcp, http, health }
2229 - balance { roundrobin }
willy tarreau197e8ec2005-12-17 14:10:59 +01002230 - disabled (to disable every further instances)
2231 - enabled (to enable every further instances, this is the default)
willy tarreaueedaa9f2005-12-17 14:08:03 +01002232 - contimeout, clitimeout, srvtimeout, grace, retries, maxconn
willy tarreau982249e2005-12-18 00:57:06 +01002233 - option { redispatch, transparent, keepalive, forwardfor, logasap, httpclose,
2234 checkcache, httplog, tcplog, dontlognull, persist, httpchk }
willy tarreaueedaa9f2005-12-17 14:08:03 +01002235 - redispatch, redisp, transparent, source { addr:port }
2236 - cookie, capture
2237 - errorloc
2238
willy tarreau197e8ec2005-12-17 14:10:59 +01002239As of 1.1.24, it is not possible to put certain parameters in a 'defaults'
2240section, mainly regular expressions and server configurations :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002241 - dispatch, server,
willy tarreau197e8ec2005-12-17 14:10:59 +01002242 - req*, rsp*
willy tarreaueedaa9f2005-12-17 14:08:03 +01002243
willy tarreau197e8ec2005-12-17 14:10:59 +01002244Last, there's no way yet to change a boolean option from its assigned default
2245value. So if an 'option' statement is set in a 'defaults' section, the only
2246way to flush it is to redefine a new 'defaults' section without this 'option'.
willy tarreaueedaa9f2005-12-17 14:08:03 +01002247
willy tarreau197e8ec2005-12-17 14:10:59 +01002248Examples :
willy tarreaueedaa9f2005-12-17 14:08:03 +01002249----------
2250 defaults applications TCP
willy tarreauc5f73ed2005-12-18 01:26:38 +01002251 log global
2252 mode tcp
2253 balance roundrobin
2254 clitimeout 180000
2255 srvtimeout 180000
2256 contimeout 4000
2257 retries 3
2258 redispatch
willy tarreaueedaa9f2005-12-17 14:08:03 +01002259
2260 listen app_tcp1 10.0.0.1:6000-6063
willy tarreauc5f73ed2005-12-18 01:26:38 +01002261 server srv1 192.168.1.1 check port 6000 inter 10000
2262 server srv2 192.168.1.2 backup
willy tarreaueedaa9f2005-12-17 14:08:03 +01002263
2264 listen app_tcp2 10.0.0.2:6000-6063
willy tarreauc5f73ed2005-12-18 01:26:38 +01002265 server srv1 192.168.2.1 check port 6000 inter 10000
2266 server srv2 192.168.2.2 backup
willy tarreaueedaa9f2005-12-17 14:08:03 +01002267
2268 defaults applications HTTP
willy tarreauc5f73ed2005-12-18 01:26:38 +01002269 log global
2270 mode http
2271 option httplog
2272 option forwardfor
2273 option dontlognull
2274 balance roundrobin
2275 clitimeout 20000
2276 srvtimeout 20000
2277 contimeout 4000
2278 retries 3
willy tarreaueedaa9f2005-12-17 14:08:03 +01002279
2280 listen app_http1 10.0.0.1:80-81
willy tarreauc5f73ed2005-12-18 01:26:38 +01002281 cookie SERVERID postonly insert indirect
2282 capture cookie userid= len 10
2283 server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000
2284 server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000
willy tarreaueedaa9f2005-12-17 14:08:03 +01002285
2286 defaults
willy tarreauc5f73ed2005-12-18 01:26:38 +01002287 # this empty section voids all default parameters
willy tarreaueedaa9f2005-12-17 14:08:03 +01002288
willy tarreau481132e2006-05-21 21:43:10 +02002289
22904.8) Status report in HTML page
2291-------------------------------
2292Starting with 1.2.14, it is possible for HAProxy to intercept requests for a
2293particular URI and return a full report of the proxy's activity and servers
2294statistics. This is available through the 'stats' keyword, associated to any
2295such options :
2296
2297 - stats enable
2298 - stats uri <uri prefix>
2299 - stats realm <authentication realm>
2300 - stats auth <user:password>
2301 - stats scope <proxy_id> | '.'
2302
2303By default, the status report is disabled. Specifying any combination above
2304enables it for the proxy instance referencing it. The easiest solution is to
2305use "stats enable" which will enable the report with default parameters :
2306
2307 - default URI : "/haproxy?stats" (CONFIG_STATS_DEFAULT_URI)
2308 - default auth : unspecified (no authentication)
2309 - default realm : "HAProxy Statistics" (CONFIG_STATS_DEFAULT_REALM)
2310 - default scope : unspecified (access to all instances)
2311
2312The "stats uri <uri_prefix>" option allows one to intercept another URI prefix.
2313Note that any URI that BEGINS with this string will match. For instance, one
2314proxy instance might be dedicated to status page only and would reply to any
2315URI.
2316
2317Example :
2318---------
2319 # catches any URI and returns the status page.
2320 listen stats :8080
2321 mode http
2322 stats uri /
2323
2324The "stats auth <user:password>" option enables Basic authentication and adds a
2325valid user:password combination to the list of authorized accounts. The user
2326and password are passed in the configuration file as clear text, and since this
2327is HTTP Basic authentication, you should be aware that it transits as clear
2328text on the network, so you must not use any sensible account. The list is
2329unlimited in order to provide easy accesses to developpers or customers.
2330
2331The "stats realm <realm>" option defines the "realm" name which is displayed
2332in the popup box when the browser asks for a password. It's important to ensure
2333that this one is not used by the application, otherwise the browser will try to
2334use a cached one from the application. Note that any space in the realm name
2335should be escaped with a backslash ('\').
2336
2337The "stats scope <proxy_id>" option limits the scope of the status report. By
2338default, all proxy instances are listed. But under some circumstances, it would
2339be better to limit the listing to some proxies or only to the current one. This
2340is what this option does. The special proxy name "." (a single dot) references
2341the current proxy. The proxy name can be repeated multiple times, even for
2342proxies defined later in the configuration or some which do not exist. The name
2343is the one which appears after the 'listen' keyword.
2344
2345Example :
2346---------
2347 # simple application with authenticated embedded status report
2348 listen app1 192.168.1.100:80
2349 mode http
willy tarreaud4ba08d2006-05-21 21:54:14 +02002350 option httpclose
willy tarreau481132e2006-05-21 21:43:10 +02002351 balance roundrobin
2352 cookie SERVERID postonly insert indirect
2353 server srv1 192.168.1.1:8080 cookie srv1 check inter 1000
2354 server srv1 192.168.1.2:8080 cookie srv2 check inter 1000
2355 stats uri /my_stats
willy tarreaud4ba08d2006-05-21 21:54:14 +02002356 stats realm Statistics\ for\ MyApp1-2
2357 stats auth guest:guest
2358 stats auth admin:AdMiN123
2359 stats scope .
2360 stats scope app2
willy tarreau481132e2006-05-21 21:43:10 +02002361
2362 # simple application with anonymous embedded status report
2363 listen app2 192.168.2.100:80
2364 mode http
willy tarreaud4ba08d2006-05-21 21:54:14 +02002365 option httpclose
willy tarreau481132e2006-05-21 21:43:10 +02002366 balance roundrobin
2367 cookie SERVERID postonly insert indirect
2368 server srv1 192.168.2.1:8080 cookie srv1 check inter 1000
2369 server srv1 192.168.2.2:8080 cookie srv2 check inter 1000
2370 stats uri /my_stats
willy tarreaud4ba08d2006-05-21 21:54:14 +02002371 stats realm Statistics\ for\ MyApp2
2372 stats scope .
willy tarreau481132e2006-05-21 21:43:10 +02002373
2374 listen admin_page :8080
2375 mode http
2376 stats uri /my_stats
willy tarreaud4ba08d2006-05-21 21:54:14 +02002377 stats realm Global\ statistics
2378 stats auth admin:AdMiN123
willy tarreau481132e2006-05-21 21:43:10 +02002379
2380Notes :
2381-------
2382 - The 'stats' options can also be specified in the 'defaults' section, in
2383 which case it will provide the exact same configuration to all further
2384 instances (hence the usefulness of the scope "."). However, if an instance
2385 redefines any 'stats' parameter, defaults will not be used for this
2386 instance.
2387
2388 - HTTP Basic authentication is very basic and unsecure from snooping. No
2389 sensible password should be used, and be aware that there is no way to
2390 remove it from the browser so it will be sent to the whole application
2391 upon further accesses.
2392
willy tarreaud4ba08d2006-05-21 21:54:14 +02002393 - It is very important that the 'option httpclose' is specified, otherwise
2394 the proxy will not be able to detect the URI within keep-alive sessions
2395 maintained between the browser and the servers, so the stats URI will be
2396 forwarded unmodified to the server as if the option was not set.
2397
willy tarreau481132e2006-05-21 21:43:10 +02002398
willy tarreau197e8ec2005-12-17 14:10:59 +01002399=========================
2400| System-specific setup |
2401=========================
willy tarreaueedaa9f2005-12-17 14:08:03 +01002402
willy tarreau197e8ec2005-12-17 14:10:59 +01002403Linux 2.4
2404=========
willy tarreaueedaa9f2005-12-17 14:08:03 +01002405
2406-- cut here --
2407#!/bin/sh
2408# set this to about 256/4M (16384 for 256M machine)
2409MAXFILES=16384
2410echo $MAXFILES > /proc/sys/fs/file-max
2411ulimit -n $MAXFILES
2412
2413if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then
willy tarreauc5f73ed2005-12-18 01:26:38 +01002414 echo 65536 > /proc/sys/net/ipv4/ip_conntrack_max
willy tarreaueedaa9f2005-12-17 14:08:03 +01002415fi
2416
2417if [ -e /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait ]; then
willy tarreauc5f73ed2005-12-18 01:26:38 +01002418 # 30 seconds for fin, 15 for time wait
2419 echo 3000 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait
2420 echo 1500 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_time_wait
2421 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_invalid_scale
2422 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_out_of_window
willy tarreaueedaa9f2005-12-17 14:08:03 +01002423fi
2424
2425echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range
2426echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
2427echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog
2428echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets
2429echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans
2430echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
2431echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
2432echo 0 > /proc/sys/net/ipv4/tcp_timestamps
2433echo 0 > /proc/sys/net/ipv4/tcp_ecn
willy tarreauc5f73ed2005-12-18 01:26:38 +01002434echo 1 > /proc/sys/net/ipv4/tcp_sack
willy tarreaueedaa9f2005-12-17 14:08:03 +01002435echo 0 > /proc/sys/net/ipv4/tcp_dsack
2436
2437# auto-tuned on 2.4
2438#echo 262143 > /proc/sys/net/core/rmem_max
2439#echo 262143 > /proc/sys/net/core/rmem_default
2440
2441echo 16384 65536 524288 > /proc/sys/net/ipv4/tcp_rmem
2442echo 16384 349520 699040 > /proc/sys/net/ipv4/tcp_wmem
2443
2444-- cut here --
2445
willy tarreau197e8ec2005-12-17 14:10:59 +01002446
2447FreeBSD
2448=======
2449
2450A FreeBSD port of HA-Proxy is now available and maintained, thanks to
2451Clement Laforet <sheepkiller@cultdeadsheep.org>.
2452
2453For more information :
2454http://www.freebsd.org/cgi/url.cgi?ports/net/haproxy/pkg-descr
2455http://www.freebsd.org/cgi/cvsweb.cgi/ports/net/haproxy/
2456http://www.freshports.org/net/haproxy
2457
2458
2459-- end --