blob: 77e260ec0b08408baa9d21230d553f2c31a393e7 [file] [log] [blame]
willy tarreaueedaa9f2005-12-17 14:08:03 +01001
2 H A - P r o x y
3 ---------------
willy tarreau4302f492005-12-18 01:00:37 +01004 version 1.2.2
willy tarreaueedaa9f2005-12-17 14:08:03 +01005 willy tarreau
willy tarreau4302f492005-12-18 01:00:37 +01006 2004/10/18
willy tarreaueedaa9f2005-12-17 14:08:03 +01007
8============
9| Abstract |
10============
11
12HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high
13availability environments. Indeed, it can :
14 - route HTTP requests depending on statically assigned cookies ;
15 - spread the load among several servers while assuring server persistence
16 through the use of HTTP cookies ;
17 - switch to backup servers in the event a main one fails ;
18 - accept connections to special ports dedicated to service monitoring ;
19 - stop accepting connections without breaking existing ones ;
20 - add/modify/delete HTTP headers both ways ;
21 - block requests matching a particular pattern ;
22
23It needs very little resource. Its event-driven architecture allows it to easily
24handle thousands of simultaneous connections on hundreds of instances without
25risking the system's stability.
26
27====================
28| Start parameters |
29====================
30
31There are only a few command line options :
32
33 -f <configuration file>
34 -n <high limit for the total number of simultaneous connections>
35 -N <high limit for the per-proxy number of simultaneous connections>
36 -d starts in foregreound with debugging mode enabled
37 -D starts in daemon mode
willy tarreau982249e2005-12-18 00:57:06 +010038 -q disable messages on output
39 -V displays messages on output even when -q or 'quiet' are specified.
40 -c only checks config file and exits with code 0 if no error was found, or
41 exits with code 1 if a syntax error was found.
willy tarreaufe2c5c12005-12-17 14:14:34 +010042 -p <pidfile> asks the process to write down each of its children's
43 pids to this file in daemon mode.
willy tarreaueedaa9f2005-12-17 14:08:03 +010044 -s shows statistics (only if compiled in)
45 -l shows even more statistics (implies '-s')
46
47
48The maximal number of connections per proxy is used as the default parameter for
49each instance for which the 'maxconn' paramter is not set in the 'listen' section.
50
51The maximal number of total connections limits the number of connections used by
52the whole process if the 'maxconn' parameter is not set in the 'global' section.
53
54The debugging mode has the same effect as the 'debug' option in the 'global'
55section. When the proxy runs in this mode, it dumps every connections,
56disconnections, timestamps, and HTTP headers to stdout. This should NEVER
57be used in an init script since it will prevent the system from starting up.
58
59Statistics are only available if compiled in with the 'STATTIME' option. It's
60only used during code optimization phases.
61
62======================
63| Configuration file |
64======================
65
66Structure
67=========
68
69The configuration file parser ignores empty lines, spaces, tabs. Anything
70between a sharp ('#') not following a backslash ('\'), and the end of a line
71constitutes a comment and is ignored too.
72
73The configuration file is segmented in sections. A section begins whenever
74one of these 3 keywords are encountered :
75
76 - 'global'
77 - 'listen'
78 - 'defaults'
79
80Every parameter refer to the section beginning at the last one of these 3
81keywords.
82
83
841) Global parameters
85====================
86
87Global parameters affect the whole process behaviour. They are all set in the
88'global' section. There may be several 'global' sections if needed, but their
89parameters will only be merged. Allowed parameters in 'global' section include
90the following ones :
91
92 - log <address> <facility> [max_level]
93 - maxconn <number>
94 - uid <user id>
95 - gid <group id>
96 - chroot <directory>
97 - nbproc <number>
98 - daemon
99 - debug
100 - quiet
willy tarreaufe2c5c12005-12-17 14:14:34 +0100101 - pidfile <file>
willy tarreaueedaa9f2005-12-17 14:08:03 +0100102
1031.1) Event logging
104------------------
105Most events are logged : start, stop, servers going up and down, connections and
106errors. Each event generates a syslog message which can be sent to up to 2
107servers. The syntax is :
108
109 log <ip_address> <facility> [max_level]
110
111Connections are logged at level "info". Services initialization and servers
112going up are logged at level "notice", termination signals are logged at
113"warning", and definitive service termination, as well as loss of servers are
114logged at level "alert". The optional parameter <max_level> specifies above
115what level messages should be sent. Level can take one of these 8 values :
116
117 emerg, alert, crit, err, warning, notice, info, debug
118
119For backwards compatibility with versions 1.1.16 and earlier, the default level
120value is "debug" if not specified.
121
122Permitted facilities are :
123 kern, user, mail, daemon, auth, syslog, lpr, news,
124 uucp, cron, auth2, ftp, ntp, audit, alert, cron2,
125 local0, local1, local2, local3, local4, local5, local6, local7
126
127According to RFC3164, messages are truncated to 1024 bytes before being emitted.
128
129Example :
130---------
131 global
132 log 192.168.2.200 local3
133 log 127.0.0.1 local4 notice
134
1351.2) limiting the number of connections
136---------------------------------------
137It is possible and recommended to limit the global number of per-process
138connections. Since one connection includes both a client and a server, it
139means that the max number of TCP sessions will be about the double of this
140number. It's important to understand this when trying to find best values
141for 'ulimit -n' before starting the proxy. To anticipate the number of
142sockets needed, all these parameters must be counted :
143
144 - 1 socket per incoming connection
145 - 1 socket per outgoing connection
146 - 1 socket per address/port/proxy tuple.
147 - 1 socket per server being health-checked
148 - 1 socket for all logs
149
150In simple configurations where each proxy only listens one one address/port,
151set the limit of file descriptors (ulimit -n) to
152(2 * maxconn + nbproxies + nbservers + 1). In a future release, haproxy may
153be able to set this value itself.
154
1551.3) Drop of priviledges
156------------------------
157In order to reduce the risk and consequences of attacks, in the event where a
158yet non-identified vulnerability would be successfully exploited, it's possible
159to lower the process priviledges and even isolate it in a riskless directory.
160
161In the 'global' section, the 'uid' parameter sets a numerical user identifier
162which the process will switch to after binding its listening sockets. The value
163'0', which normally represents the super-user, here indicates that the UID must
164not change during startup. It's the default behaviour. The 'gid' parameter does
165the same for the group identifier. It's particularly advised against use of
166generic accounts such as 'nobody' because it has the same consequences as using
167'root' if other services use them.
168
169The 'chroot' parameter makes the process isolate itself in an empty directory
170just before switching its UID. This type of isolation (chroot) can sometimes
171be worked around on certain OS (Linux, Solaris), provided that the attacker
172has gained 'root' priviledges and has the ability to use or create a directory.
173For this reason, it's capital to use a dedicated directory and not to share one
174between several services of different nature. To make isolation more resistant,
175it's recommended to use an empty directory without any right, and to change the
176UID of the process so that it cannot do anything there.
177
178Note: in the event where such a vulnerability would be exploited, it's most
179likely that first attempts would kill the process due to 'Segmentation Fault',
180'Bus Error' or 'Illegal Instruction' signals. Eventhough it's true that
181isolating the server reduces the risks of intrusion, it's sometimes useful to
182find why a process dies, via the analysis of a 'core' file, although very rare
183(the last bug of this sort was fixed in 1.1.9). For security reasons, most
184systems disable the generation of core file when a process changes its UID. So
185the two workarounds are either to start the process from a restricted user
186account, which will not be able to chroot itself, or start it as root and not
187change the UID. In both cases the core will be either in the start or the chroot
188directories. Do not forget to allow core dumps prior to start the process :
189
190# ulimit -c unlimited
191
192Example :
193---------
194
195 global
196 uid 30000
197 gid 30000
198 chroot /var/chroot/haproxy
199
2001.4) Startup modes
201------------------
202The service can start in several different :
203 - foreground / background
204 - quiet / normal / debug
205
206The default mode is normal, foreground, which means that the program doesn't
207return once started. NEVER EVER use this mode in a system startup script, or
208the system won't boot. It needs to be started in background, so that it
209returns immediately after forking. That's accomplished by the 'daemon' option
210in the 'global' section, which is the equivalent of the '-D' command line
211argument.
212
213Moreover, certain alert messages are still sent to the standard output even
214in 'daemon' mode. To make them disappear, simply add the 'quiet' option in the
215'global' section. This option has no command-line equivalent.
216
217Last, the 'debug' mode, enabled with the 'debug' option in the 'global' section,
218and which is equivalent of the '-d' option, allows deep TCP/HTTP analysis, with
219timestamped display of each connection, disconnection, and HTTP headers for both
220ways. This mode is incompatible with 'daemon' and 'quiet' modes for obvious
221reasons.
222
2231.5) Increasing the overall processing power
224--------------------------------------------
225On multi-processor systems, it may seem to be a shame to use only one processor,
willy tarreau982249e2005-12-18 00:57:06 +0100226eventhough the load needed to saturate a recent processor is far above common
willy tarreaueedaa9f2005-12-17 14:08:03 +0100227usage. Anyway, for very specific needs, the proxy can start several processes
228between which the operating system will spread the incoming connections. The
229number of processes is controlled by the 'nbproc' parameter in the 'global'
willy tarreau4302f492005-12-18 01:00:37 +0100230section. It defaults to 1, and obviously works only in 'daemon' mode. One
231typical usage of this parameter has been to workaround the default per-process
232file-descriptor limit that Solaris imposes to user processes.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100233
234Example :
235---------
236
237 global
238 daemon
239 quiet
240 nbproc 2
241
242
willy tarreaufe2c5c12005-12-17 14:14:34 +01002431.6) Helping process management
244-------------------------------
245Haproxy now supports the notion of pidfile. If the '-p' command line argument,
246or the 'pidfile' global option is followed with a file name, this file will be
247removed, then filled with all children's pids, one per line (only in daemon
248mode). This file is NOT within the chroot, which allows to work with a readonly
249 chroot. It will be owned by the user starting the process, and will have
250permissions 0644.
251
252Example :
253---------
254
255 global
256 daemon
257 quiet
258 nbproc 2
259 pidfile /var/run/haproxy-private.pid
260
261 # to stop only those processes among others :
262 # kill $(</var/run/haproxy-private.pid)
263
264
willy tarreaueedaa9f2005-12-17 14:08:03 +01002652) Declaration of a listening service
266=====================================
267
268Service sections start with the 'listen' keyword :
269
270 listen <instance_name> [ <IP_address>:<port_range>[,...] ]
271
272- <instance_name> is the name of the instance. This name will be reported in
273 logs, so it is good to have it reflect the proxied service. No unicity test
274 is done on this name, and it's not mandatory for it to be unique, but highly
275 recommended.
276
277- <IP_address> is the IP address the proxy binds to. Empty address, '*' and
278 '0.0.0.0' all mean that the proxy listens to all valid addresses on the
279 system.
280
281- <port_range> is either a unique port, or a port range for which the proxy will
282 accept connections for the IP address specified above. This range can be :
283 - a numerical port (ex: '80')
284 - a dash-delimited ports range explicitly stating the lower and upper bounds
285 (ex: '2000-2100') which are included in the range.
286
287 Particular care must be taken against port ranges, because every <addr:port>
288 couple consumes one socket (=a file descriptor), so it's easy to eat lots of
289 descriptors with a simple range. The <addr:port> couple must be used only once
290 among all instances running on a same system. Please note that attaching to
291 ports lower than 1024 need particular priviledges to start the program, which
292 are independant of the 'uid' parameter.
293
294- the <IP_address>:<port_range> couple may be repeated indefinitely to require
295 the proxy to listen to other addresses and/or ports. To achieve this, simply
296 separate them with a coma.
297
298Examples :
299---------
300 listen http_proxy :80
301 listen x11_proxy 127.0.0.1:6000-6009
302 listen smtp_proxy 127.0.0.1:25,127.0.0.1:587
303 listen ldap_proxy :389,:663
304
305In the event that all addresses do not fit line width, it's preferable to
306detach secondary addresses on other lines with the 'bind' keyword. If this
307keyword is used, it's not even necessary to specify the first address on the
308'listen' line, which sometimes makes multiple configuration handling easier :
309
310 bind [ <IP_address>:<port_range>[,...] ]
311
312Examples :
313----------
314 listen http_proxy
315 bind :80,:443
316 bind 10.0.0.1:10080,10.0.0.1:10443
317
3182.1) Inhibiting a service
319-------------------------
320A service may be disabled for maintenance reasons, without needing to comment
321out the whole section, simply by specifying the 'disabled' keyword in the
322section to be disabled :
323
324 listen smtp_proxy 0.0.0.0:25
325 disabled
326
327Note: the 'enabled' keyword allows to enable a service which has been disabled
328 previously by a default configuration.
329
3302.2) Modes of operation
331-----------------------
332A service can work in 3 different distinct modes :
333 - TCP
334 - HTTP
335 - monitoring
336
337TCP mode
338--------
339In this mode, the service relays TCP connections as soon as they're established,
340towards one or several servers. No processing is done on the stream. It's only
341an association of source(addr:port) -> destination(addr:port). To use this mode,
342you must specify 'mode tcp' in the 'listen' section. This is the default mode.
343
344Example :
345---------
346 listen smtp_proxy 0.0.0.0:25
347 mode tcp
348
349HTTP mode
350---------
351In this mode, the service relays TCP connections towards one or several servers,
352when it has enough informations to decide, which normally means that all HTTP
353headers have been read. Some of them may be scanned for a cookie or a pattern
354matching a regex. To use this mode, specify 'mode http' in the 'listen' section.
355
356Example :
357---------
358 listen http_proxy 0.0.0.0:80
359 mode http
360
361Health-checking mode
362--------------------
363This mode provides a way for external components to check the proxy's health.
364It is meant to be used with intelligent load-balancers which can use send/expect
365scripts to check for all of their servers' availability. This one simply accepts
willy tarreau197e8ec2005-12-17 14:10:59 +0100366the connection, returns the word 'OK' and closes it. If the 'option httpchk' is
367set, then the reply will be 'HTTP/1.0 200 OK' with no data, so that it can be
368tested from a tool which supports HTTP health-checks. To enable it, simply
willy tarreaueedaa9f2005-12-17 14:08:03 +0100369specify 'health' as the working mode :
370
371Example :
372---------
willy tarreau197e8ec2005-12-17 14:10:59 +0100373 # simple response : 'OK'
willy tarreaueedaa9f2005-12-17 14:08:03 +0100374 listen health_check 0.0.0.0:60000
375 mode health
376
willy tarreau197e8ec2005-12-17 14:10:59 +0100377 # HTTP response : 'HTTP/1.0 200 OK'
378 listen http_health_check 0.0.0.0:60001
379 mode health
380 option httpchk
381
willy tarreaueedaa9f2005-12-17 14:08:03 +0100382
3832.3) Limiting the number of simultaneous connections
384----------------------------------------------------
385The 'maxconn' parameter allows a proxy to refuse connections above a certain
386amount of simultaneous ones. When the limit is reached, it simply stops
387listening, but the system may still be accepting them because of the back log
willy tarreau982249e2005-12-18 00:57:06 +0100388queue. These connections will be processed later when other ones have freed
willy tarreaueedaa9f2005-12-17 14:08:03 +0100389some slots. This provides a serialization effect which helps very fragile
390servers resist to high loads. Se further for system limitations.
391
392Example :
393---------
394 listen tiny_server 0.0.0.0:80
395 maxconn 10
396
397
3982.4) Soft stop
399--------------
400It is possible to stop services without breaking existing connections by the
401sending of the SIG_USR1 signal to the process. All services are then put into
402soft-stop state, which means that they will refuse to accept new connections,
403except for those which have a non-zero value in the 'grace' parameter, in which
404case they will still accept connections for the specified amount of time, in
405milliseconds. This allows to tell a load-balancer that the service is failing,
406while still doing the job during the time it needs to detect it.
407
408Note: active connections are never killed. In the worst case, the user will have
409to wait for all of them to close or to time-out, or simply kill the process
410normally (SIG_TERM). The default 'grace' value is '0'.
411
412Example :
413---------
414 # enter soft stop after 'killall -USR1 haproxy'
415 # the service will still run 10 seconds after the signal
416 listen http_proxy 0.0.0.0:80
417 mode http
418 grace 10000
419
420 # this port is dedicated to a load-balancer, and must fail immediately
421 listen health_check 0.0.0.0:60000
422 mode health
423 grace 0
424
425
4262.5) Connections expiration time
427--------------------------------
428It is possible (and recommended) to configure several time-outs on TCP
429connections. Three independant timers are adjustable with values specified
430in milliseconds. A session will be terminated if either one of these timers
431expire.
432
433 - the time we accept to wait for data from the client, or for the client to
434 accept data : 'clitimeout' :
435
436 # client time-out set to 2mn30.
437 clitimeout 150000
438
439 - the time we accept to wait for data from the server, or for the server to
440 accept data : 'srvtimeout' :
441
442 # server time-out set to 30s.
443 srvtimeout 30000
444
445 - the time we accept to wait for a connection to establish on a server :
446 'contimeout' :
447
448 # we give up if the connection does not complete within 4 seconds
449 contimeout 4000
450
451Notes :
452-------
453 - 'contimeout' and 'srvtimeout' have no sense on 'health' mode servers ;
454 - under high loads, or with a saturated or defective network, it's possible
455 that some packets get lost. Since the first TCP retransmit only happens
456 after 3 seconds, a time-out equal to, or lower than 3 seconds cannot
457 compensate for a packet loss. A 4 seconds time-out seems a reasonable
458 minimum which will considerably reduce connection failures.
459
4602.6) Attempts to reconnect
461--------------------------
462After a connection failure to a server, it is possible to retry, potentially
463on another server. This is useful if health-checks are too rare and you don't
464want the clients to see the failures. The number of attempts to reconnect is
465set by the 'retries' paramter.
466
467Example :
468---------
469 # we can retry 3 times max after a failure
470 retries 3
471
472
4732.7) Address of the dispatch server (deprecated)
474------------------------------------------------
475The server which will be sent all new connections is defined by the 'dispatch'
476parameter, in the form <address>:<port>. It generally is dedicated to unknown
477connections and will assign them a cookie, in case of HTTP persistence mode,
478or simply is a single server in case of generic TCP proxy. This old mode is only
479provided for backwards compatibility, but doesn't allow to check remote servers
480state, and has a rather limited usage. All new setups should switch to 'balance'
481mode. The principle of the dispatcher is to be able to perform the load
482balancing itself, but work only on new clients so that the server doesn't need
483to be a big machine.
484
485Example :
486---------
487 # all new connections go there
488 dispatch 192.168.1.2:80
489
490Note :
491------
492This parameter has no sense for 'health' servers, and is incompatible with
493'balance' mode.
494
495
4962.8) Outgoing source address
497----------------------------
498It is often necessary to bind to a particular address when connecting to some
499remote hosts. This is done via the 'source' parameter which is a per-proxy
500parameter. A newer version may allow to fix different sources to reach different
501servers. The syntax is 'source <address>[:<port>]', where <address> is a valid
502local address (or '0.0.0.0' or '*' or empty to let the system choose), and
503<port> is an optional parameter allowing the user to force the source port for
504very specific needs. If the port is not specified or is '0', the system will
505choose a free port. Note that as of version 1.1.18, the servers health checks
506are also performed from the same source.
507
508Examples :
509----------
510 listen http_proxy *:80
511 # all connections take 192.168.1.200 as source address
512 source 192.168.1.200:0
513
514 listen rlogin_proxy *:513
515 # use address 192.168.1.200 and the reserved port 900 (needs to be root)
516 source 192.168.1.200:900
517
518
5192.9) Setting the cookie name
520----------------------------
521In HTTP mode, it is possible to look for a particular cookie which will contain
522a server identifier which should handle the connection. The cookie name is set
523via the 'cookie' parameter.
524
525Example :
526---------
527 listen http_proxy :80
528 mode http
529 cookie SERVERID
530
531It is possible to change the cookie behaviour to get a smarter persistence,
532depending on applications. It is notably possible to delete or modify a cookie
533emitted by a server, insert a cookie identifying the server in an HTTP response
534and even add a header to tell upstream caches not to cache this response.
535
536Examples :
537----------
538
539To remove the cookie for direct accesses (ie when the server matches the one
540which was specified in the client cookie) :
541
542 cookie SERVERID indirect
543
544To replace the cookie value with the one assigned to the server if any (no
545cookie will be created if the server does not provide one, nor if the
546configuration does not provide one). This lets the application put the cookie
547exactly on certain pages (eg: successful authentication) :
548
549 cookie SERVERID rewrite
550
551To create a new cookie and assign the server identifier to it (in this case, all
552servers should be associated with a valid cookie, since no cookie will simply
553delete the cookie from the client's browser) :
554
555 cookie SERVERID insert
556
557To insert a cookie and ensure that no upstream cache will store it, add the
558'nocache' option :
559
560 cookie SERVERID insert nocache
561
562To insert a cookie only after a POST request, add 'postonly' after 'insert'.
563This has the advantage that there's no risk of caching, and that all pages
564seen before the POST one can still be cached :
565
566 cookie SERVERID insert postonly
567
568Notes :
569-----------
570- it is possible to combine 'insert' with 'indirect' or 'rewrite' to adapt to
571 applications which already generate the cookie with an invalid content.
572
573- in the case where 'insert' and 'indirect' are both specified, the cookie is
574 never transmitted to the server, since it wouldn't understand it. This is
575 the most application-transparent mode.
576
577- it is particularly recommended to use 'nocache' in 'insert' mode if any
578 upstream HTTP/1.0 cache is susceptible to cache the result, because this may
579 lead to many clients going to the same server, or even worse, some clients
580 having their server changed while retrieving a page from the cache.
581
582- when the application is well known and controlled, the best method is to
583 only add the persistence cookie on a POST form because it's up to the
584 application to select which page it wants the upstream servers to cache.
585 In this case, you would use 'insert postonly indirect'.
586
5872.10) Associating a cookie value with a server
588----------------------------------------------
589In HTTP mode, it's possible to associate a cookie value to each server. This
590was initially used in combination with 'dispatch' mode to handle direct accesses
591but it is now the standard way of doing the load balancing. The syntax is :
592
593 server <identifier> <address>:<port> cookie <value>
594
595- <identifier> is any name which can be used to identify the server in the logs.
596- <address>:<port> specifies where the server is bound.
597- <value> is the value to put in or to read from the cookie.
598
599Example : the 'SERVERID' cookie can be either 'server01' or 'server02'
600---------
601 listen http_proxy :80
602 mode http
603 cookie SERVERID
604 dispatch 192.168.1.100:80
605 server web1 192.168.1.1:80 cookie server01
606 server web2 192.168.1.2:80 cookie server02
607
608Warning : the syntax has changed since version 1.0 !
609---------
610
6113) Autonomous load balancer
612===========================
613
614The proxy can perform the load-balancing itself, both in TCP and in HTTP modes.
615This is the most interesting mode which obsoletes the old 'dispatch' mode
616described above. It has advantages such as server health monitoring, multiple
617port binding and port mapping. To use this mode, the 'balance' keyword is used,
618followed by the selected algorithm. As of version 1.1.23, only 'roundrobin' is
619available, which is also the default value if unspecified. In this mode, there
620will be no dispatch address, but the proxy needs at least one server.
621
622Example : same as the last one, with internal load balancer
623---------
624
625 listen http_proxy :80
626 mode http
627 cookie SERVERID
628 balance roundrobin
629 server web1 192.168.1.1:80 cookie server01
630 server web2 192.168.1.2:80 cookie server02
631
632
633Since version 1.1.22, it is possible to automatically determine on which port
634the server will get the connection, depending on the port the client connected
635to. Indeed, there now are 4 possible combinations for the server's <port> field:
636
637 - unspecified or '0' :
638 the connection will be sent to the same port as the one on which the proxy
639 received the client connection itself.
640
641 - numerical value (the only one supported in versions earlier than 1.1.22) :
642 the connection will always be sent to the specified port.
643
644 - '+' followed by a numerical value :
645 the connection will be sent to the same port as the one on which the proxy
646 received the connection, plus this value.
647
648 - '-' followed by a numerical value :
649 the connection will be sent to the same port as the one on which the proxy
650 received the connection, minus this value.
651
652Examples :
653----------
654
655# same as previous example
656
657 listen http_proxy :80
658 mode http
659 cookie SERVERID
660 balance roundrobin
661 server web1 192.168.1.1 cookie server01
662 server web2 192.168.1.2 cookie server02
663
664# simultaneous relaying of ports 80, 81 and 8080-8089
665
666 listen http_proxy :80,:81,:8080-8089
667 mode http
668 cookie SERVERID
669 balance roundrobin
670 server web1 192.168.1.1 cookie server01
671 server web2 192.168.1.2 cookie server02
672
673# relaying of TCP ports 25, 389 and 663 to ports 1025, 1389 and 1663
674
675 listen http_proxy :25,:389,:663
676 mode tcp
677 balance roundrobin
678 server srv1 192.168.1.1:+1000
679 server srv2 192.168.1.2:+1000
680
681
willy tarreau197e8ec2005-12-17 14:10:59 +01006823.1) Server monitoring
683----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +0100684
685It is possible to check the servers status by trying to establish TCP
686connections or even sending HTTP requests to them. A server which fails to
687reply to health checks as expected will not be used by the load balancing
688algorithms. To enable monitoring, add the 'check' keyword on a server line.
689It is possible to specify the interval between tests (in milliseconds) with
690the 'inter' parameter, the number of failures supported before declaring that
691the server has fallen down with the 'fall' parameter, and the number of valid
692checks needed for the server to fully get up with the 'rise' parameter. Since
693version 1.1.22, it is also possible to send checks to a different port
694(mandatory when none is specified) with the 'port' parameter. The default
695values are the following ones :
696
697 - inter : 2000
698 - rise : 2
699 - fall : 3
700 - port : default server port
701
702The default mode consists in establishing TCP connections only. But in certain
703types of application failures, it is often that the server continues to accept
704connections because the system does it itself while the application is running
705an endless loop, or is completely stuck. So in version 1.1.16 were introduced
706HTTP health checks which only performed simple lightweight requests and analysed
707the response. Now, as of version 1.1.23, it is possible to change the HTTP
708method, the URI, and the HTTP version string (which even allows to send headers
709with a dirty trick). To enable HTTP health-checks, use 'option httpchk'.
710
711By default, requests use the 'OPTIONS' method because it's very light and easy
712to filter from logs, and does it on '/'. Only HTTP responses 2xx and 3xx are
713considered valid ones, and only if they come before the time to send a new
714request is reached ('inter' parameter). If some servers block this type of
715request, 3 other forms help to forge a request :
716
717 - option httpchk -> OPTIONS / HTTP/1.0
718 - option httpchk URI -> OPTIONS <URI> HTTP/1.0
719 - option httpchk METH URI -> <METH> <URI> HTTP/1.0
720 - option httpchk METH URI VER -> <METH> <URI> <VER>
721
722See examples below.
723
724Since version 1.1.17, it is possible to specify backup servers. These servers
725are only sollicited when no other server is available. This may only be useful
726to serve a maintenance page, or define one active and one backup server (seldom
727used in TCP mode). To make a server a backup one, simply add the 'backup' option
728on its line. These servers also support cookies, so if a cookie is specified for
729a backup server, clients assigned to this server will stick to it even when the
730other ones come back. Conversely, if no cookie is assigned to such a server,
731the clients will get their cookies removed (empty cookie = removal), and will
732be balanced against other servers once they come back. Please note that there
733is no load-balancing among backup servers. If there are several backup servers,
734the second one will only be used when the first one dies, and so on.
735
736Since version 1.1.17, it is also possible to visually check the status of all
737servers at once. For this, you just have to send a SIGHUP signal to the proxy.
738The servers status will be dumped into the logs at the 'notice' level, as well
739as on <stderr> if not closed. For this reason, it's always a good idea to have
740one local log server at the 'notice' level.
741
willy tarreau982249e2005-12-18 00:57:06 +0100742Since version 1.1.28 and 1.2.1, if an instance loses all its servers, an
743emergency mesasge will be sent in the logs to inform the administator that an
744immediate action must be taken.
745
746
willy tarreaueedaa9f2005-12-17 14:08:03 +0100747Examples :
748----------
749# same setup as in paragraph 3) with TCP monitoring
750 listen http_proxy 0.0.0.0:80
751 mode http
752 cookie SERVERID
753 balance roundrobin
754 server web1 192.168.1.1:80 cookie server01 check
755 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
756
757# same with HTTP monitoring via 'OPTIONS / HTTP/1.0'
758 listen http_proxy 0.0.0.0:80
759 mode http
760 cookie SERVERID
761 balance roundrobin
762 option httpchk
763 server web1 192.168.1.1:80 cookie server01 check
764 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
765
766# same with HTTP monitoring via 'OPTIONS /index.html HTTP/1.0'
767 listen http_proxy 0.0.0.0:80
768 mode http
769 cookie SERVERID
770 balance roundrobin
771 option httpchk /index.html
772 server web1 192.168.1.1:80 cookie server01 check
773 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
774
775# same with HTTP monitoring via 'HEAD /index.jsp? HTTP/1.1\r\nHost: www'
776 listen http_proxy 0.0.0.0:80
777 mode http
778 cookie SERVERID
779 balance roundrobin
780 option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www
781 server web1 192.168.1.1:80 cookie server01 check
782 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
783
784# automatic insertion of a cookie in the server's response, and automatic
785# deletion of the cookie in the client request, while asking upstream caches
786# not to cache replies.
787 listen web_appl 0.0.0.0:80
788 mode http
789 cookie SERVERID insert nocache indirect
790 balance roundrobin
791 server web1 192.168.1.1:80 cookie server01 check
792 server web2 192.168.1.2:80 cookie server02 check
793
794# same with off-site application backup and local error pages server
795 listen web_appl 0.0.0.0:80
796 mode http
797 cookie SERVERID insert nocache indirect
798 balance roundrobin
799 server web1 192.168.1.1:80 cookie server01 check
800 server web2 192.168.1.2:80 cookie server02 check
801 server web-backup 192.168.2.1:80 cookie server03 check backup
802 server web-excuse 192.168.3.1:80 check backup
803
804# SMTP+TLS relaying with heakth-checks and backup servers
805
806 listen http_proxy :25,:587
807 mode tcp
808 balance roundrobin
809 server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2
810 server srv2 192.168.1.2 backup
811
812
8133.2) Redistribute connections in case of failure
814------------------------------------------------
815In HTTP mode, if a server designated by a cookie does not respond, the clients
816may definitely stick to it because they cannot flush the cookie, so they will
817not be able to access the service anymore. Specifying 'redispatch' will allow
818the proxy to break their persistence and redistribute them to working servers.
819
820Example :
821---------
822 listen http_proxy 0.0.0.0:80
823 mode http
824 cookie SERVERID
825 dispatch 192.168.1.100:80
826 server web1 192.168.1.1:80 cookie server01
827 server web2 192.168.1.2:80 cookie server02
828 redispatch # send back to dispatch in case of connection failure
829
830Up to, and including version 1.1.16, this parameter only applied to connection
831failures. Since version 1.1.17, it also applies to servers which have been
832detected as failed by the health check mechanism. Indeed, a server may be broken
833but still accepting connections, which would not solve every case. But it is
834possible to conserve the old behaviour, that is, make a client insist on trying
835to connect to a server even if it is said to be down, by setting the 'persist'
836option :
837
838 listen http_proxy 0.0.0.0:80
839 mode http
840 option persist
841 cookie SERVERID
842 dispatch 192.168.1.100:80
843 server web1 192.168.1.1:80 cookie server01
844 server web2 192.168.1.2:80 cookie server02
845 redispatch # send back to dispatch in case of connection failure
846
847
8484) Additionnal features
849=======================
850
851Other features are available. They are transparent mode, event logging and
852header rewriting/filtering.
853
8544.1) Transparent mode
855---------------------
856In HTTP mode, the 'transparent' keyword allows to intercept sessions which are
857routed through the system hosting the proxy. This mode was implemented as a
858replacement for the 'dispatch' mode, since connections without cookie will be
859sent to the original address while known cookies will be sent to the servers.
860This mode implies that the system can redirect sessions to a local port.
861
862Example :
863---------
864 listen http_proxy 0.0.0.0:65000
865 mode http
866 transparent
867 cookie SERVERID
868 server server01 192.168.1.1:80
869 server server02 192.168.1.2:80
870
871 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
872 --dport 80 -j REDIRECT --to-ports 65000
873
874Note :
875------
876If the port is left unspecified on the server, the port the client connected to
877will be used. This allows to relay a full port range without using transparent
878mode nor thousands of file descriptors, provided that the system can redirect
879sessions to local ports.
880
881Example :
882---------
883 # redirect all ports to local port 65000, then forward to the server on the
884 # original port.
885 listen http_proxy 0.0.0.0:65000
886 mode tcp
887 server server01 192.168.1.1 check port 60000
888 server server02 192.168.1.2 check port 60000
889
890 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
891 -j REDIRECT --to-ports 65000
892
893
8944.2) Event logging
895------------------
willy tarreauc1cae632005-12-17 14:12:23 +01008964.2.1) Log levels
897-----------------
willy tarreau197e8ec2005-12-17 14:10:59 +0100898TCP and HTTP connections can be logged with informations such as date, time,
899source IP address, destination address, connection duration, response times,
900HTTP request, the HTTP return code, number of bytes transmitted, the conditions
901in which the session ended, and even exchanged cookies values, to track a
902particular user's problems for example. All messages are sent to up to two
903syslog servers. Consult section 1.1 for more info about log facilities. The
904syntax follows :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100905
willy tarreau197e8ec2005-12-17 14:10:59 +0100906 log <address_1> <facility_1> [max_level_1]
907 log <address_2> <facility_2> [max_level_2]
908or
willy tarreaueedaa9f2005-12-17 14:08:03 +0100909 log global
910
willy tarreau197e8ec2005-12-17 14:10:59 +0100911Note :
912------
913The particular syntax 'log global' means that the same log configuration as the
914'global' section will be used.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100915
willy tarreau197e8ec2005-12-17 14:10:59 +0100916Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100917---------
918 listen http_proxy 0.0.0.0:80
919 mode http
920 log 192.168.2.200 local3
921 log 192.168.2.201 local4
922
willy tarreauc1cae632005-12-17 14:12:23 +01009234.2.2) Log format
924-----------------
925By default, connections are logged at the TCP level, as soon as the session
926establishes between the client and the proxy. By enabling the 'tcplog' option,
927the proxy will wait until the session ends to generate an enhanced log
928containing more information such as session duration and its state during the
929disconnection.
930
willy tarreau982249e2005-12-18 00:57:06 +0100931Example :
932---------
933 listen relais-tcp 0.0.0.0:8000
934 mode tcp
935 option tcplog
936 log 192.168.2.200 local3
937
938>>> haproxy[18989]: 127.0.0.1:34550 [15/Oct/2003:15:24:28] relais-tcp Srv1 0/5007 0 --
939
willy tarreauc1cae632005-12-17 14:12:23 +0100940Another option, 'httplog', provides more detailed information about HTTP
941contents, such as the request and some cookies. In the event where an external
942component would establish frequent connections to check the service, logs may be
943full of useless lines. So it is possible not to log any session which didn't
944transfer any data, by the setting of the 'dontlognull' option. This only has
945effect on sessions which are established then closed.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100946
willy tarreau197e8ec2005-12-17 14:10:59 +0100947Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100948---------
949 listen http_proxy 0.0.0.0:80
950 mode http
951 option httplog
952 option dontlognull
953 log 192.168.2.200 local3
954
willy tarreau982249e2005-12-18 00:57:06 +0100955>>> haproxy[674]: 127.0.0.1:33319 [15/Oct/2003:08:31:57] relais-http Srv1 9/7/147/723 200 243 - - ---- "HEAD / HTTP/1.0"
956
957The problem when logging at end of connection is that you have no clue about
958what is happening during very long sessions. To workaround this problem, a
959new option 'logasap' has been introduced in 1.1.28/1.2.1. When specified, the
960proxy will log as soon as possible, just before data transfer begins. This means
961that in case of TCP, it will still log the connection status to the server, and
962in case of HTTP, it will log just after processing the server headers. In this
963case, the number of bytes reported is the number of header bytes sent to the
964client.
965
966In order to avoid confusion with normal logs, the total time field and the
967number of bytes are prefixed with a '+' sign which mean that real numbers are
968certainly bigger.
969
970Example :
971---------
972
973 listen http_proxy 0.0.0.0:80
974 mode http
975 option httplog
976 option dontlognull
977 option logasap
978 log 192.168.2.200 local3
979
980>>> haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/7/14/+30 200 +243 - - ---- "GET /image.iso HTTP/1.0"
981
982
willy tarreauc1cae632005-12-17 14:12:23 +01009834.2.3) Timing events
984--------------------
985Timers provide a great help in trouble shooting network problems. All values
986are reported in milliseconds (ms). In HTTP mode, four control points are
987reported under the form 'Tq/Tc/Tr/Tt' :
988
989 - Tq: total time to get the client request.
990 It's the time elapsed between the moment the client connection was accepted
991 and the moment the proxy received the last HTTP header. The value '-1'
992 indicates that the end of headers (empty line) has never been seen.
993
994 - Tc: total time to establish the TCP connection to the server.
995 It's the time elapsed between the moment the proxy sent the connection
996 request, and the moment it was acknowledged, or between the TCP SYN packet
997 and the matching SYN/ACK in return. The value '-1' means that the
998 connection never established.
999
1000 - Tr: server response time. It's the time elapsed between the moment the
1001 TCP connection was established to the server and the moment it send its
1002 complete response header. It purely shows its request processing time,
1003 without the network overhead due to the data transmission. The value '-1'
1004 means that the last the response header (empty line) was never seen.
1005
1006 - Tt: total session duration time, between the moment the proxy accepted it
willy tarreau982249e2005-12-18 00:57:06 +01001007 and the moment both ends were closed. The exception is when the 'logasap'
1008 option is specified. In this case, it only equals (Tq+Tc+Tr), and is
1009 prefixed with a '+' sign. From this field, we can deduce Td, the data
1010 transmission time, by substracting other timers when valid :
willy tarreauc1cae632005-12-17 14:12:23 +01001011
1012 Td = Tt - (Tq + Tc + Tr)
1013
1014 Timers with '-1' values have to be excluded from this equation.
1015
1016In TCP mode ('option tcplog'), only Tc and Tt are reported.
1017
1018These timers provide precious indications on trouble causes. Since the TCP
1019protocol defines retransmit delays of 3, 6, 12... seconds, we know for sure
1020that timers close to multiples of 3s are nearly always related to packets lost
1021due to network problems (wires or negociation). Moreover, if <Tt> is close to
1022a timeout value specified in the configuration, it often means that a session
1023has been aborted on time-out.
1024
1025Most common cases :
1026
1027 - If Tq is close to 3000, a packet has probably been lost between the client
1028 and the proxy.
1029 - If Tc is close to 3000, a packet has probably been lost between the server
1030 and the proxy during the server connection phase. This one should always be
1031 very low (less than a few tens).
1032 - If Tr is nearly always lower than 3000 except some rare values which seem to
1033 be the average majored by 3000, there are probably some packets lost between
1034 the proxy and the server.
1035 - If Tt is often slightly higher than a time-out, it's often because the
1036 client and the server use HTTP keep-alive and the session is maintained
1037 after the response ends. Se further for how to disable HTTP keep-alive.
1038
1039Other cases ('xx' means any value to be ignored) :
1040 -1/xx/xx/Tt : the client was not able to send its complete request in time,
1041 or that it aborted it too early.
1042 Tq/-1/xx/Tt : the connection could not establish on the server. Either it
1043 refused it or it timed out after Tt-Tq ms.
1044 Tq/Tc/-1/Tt : the server has accepted the connection but did not return a
1045 complete response in time, or it closed its connexion
1046 unexpectedly, after Tt-(Tq+Tc) ms.
1047
10484.2.4) Session state at disconnection
1049-------------------------------------
1050TCP and HTTP logs provide a session completion indicator. It's a 4-characters
1051(2 in TCP) field preceeding the HTTP request, and indicating :
willy tarreau197e8ec2005-12-17 14:10:59 +01001052 - On the first character, a code reporting the first event which caused the
1053 session to terminate :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001054
willy tarreau197e8ec2005-12-17 14:10:59 +01001055 C : the TCP session was aborted by the client.
1056 S : the TCP session was aborted by the server, or the server refused it.
1057 P : the session was abordted prematurely by the proxy, either because of
willy tarreau982249e2005-12-18 00:57:06 +01001058 an internal error, because a DENY filter was matched, or because of
1059 a security check which detected a dangerous error in server
1060 response.
willy tarreau197e8ec2005-12-17 14:10:59 +01001061 c : the client time-out expired first.
1062 s : the server time-out expired first.
1063 - : normal session completion.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001064
willy tarreau197e8ec2005-12-17 14:10:59 +01001065 - on the second character, the HTTP session state when it was closed :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001066
willy tarreau197e8ec2005-12-17 14:10:59 +01001067 R : waiting for complete REQUEST from the client
1068 C : waiting for CONNECTION to establish on the server
willy tarreau982249e2005-12-18 00:57:06 +01001069 H : processing server HEADERS
willy tarreau197e8ec2005-12-17 14:10:59 +01001070 D : the session was in the DATA phase
1071 L : the proxy was still transmitting LAST data to the client while the
1072 server had already finished.
1073 - : normal session completion after end of data transfer.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001074
willy tarreau197e8ec2005-12-17 14:10:59 +01001075 - the third character tells whether the persistence cookie was provided by
willy tarreauc1cae632005-12-17 14:12:23 +01001076 the client (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001077
willy tarreau197e8ec2005-12-17 14:10:59 +01001078 N : the client provided NO cookie.
1079 I : the client provided an INVALID cookie matching no known server.
1080 D : the client provided a cookie designating a server which was DOWN,
1081 so either the 'persist' option was used and the client was sent to
1082 this server, or it was not set and the client was redispatched to
1083 another server.
1084 V : the client provided a valid cookie, and was sent to the associated
1085 server.
1086 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001087
willy tarreau197e8ec2005-12-17 14:10:59 +01001088 - the last character reports what operations were performed on the persistence
willy tarreauc1cae632005-12-17 14:12:23 +01001089 cookie returned by the server (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001090
willy tarreau197e8ec2005-12-17 14:10:59 +01001091 N : NO cookie was provided by the server.
1092 P : a cookie was PROVIDED by the server and transmitted as-is.
1093 I : no cookie was provided by the server, and one was INSERTED by the
willy tarreaueedaa9f2005-12-17 14:08:03 +01001094 proxy.
willy tarreau197e8ec2005-12-17 14:10:59 +01001095 D : the cookie provided by the server was DELETED by the proxy.
1096 R : the cookie provided by the server was REWRITTEN by the proxy.
1097 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001098
willy tarreau197e8ec2005-12-17 14:10:59 +01001099The 'capture' keyword allows to capture and log informations exchanged between
1100clients and servers. As of version 1.1.23, only cookies can be captured, which
1101makes it easy to track a complete user session. The syntax is :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001102
willy tarreau197e8ec2005-12-17 14:10:59 +01001103 capture cookie <cookie_prefix> len <capture_length>
willy tarreaueedaa9f2005-12-17 14:08:03 +01001104
willy tarreau197e8ec2005-12-17 14:10:59 +01001105The FIRST cookie whose name starts with <cookie_prefix> will be captured, and
1106logged as 'NAME=value', without exceeding <capture_length> characters (64 max).
1107When the cookie name is fixed and known, it's preferable to suffix '=' to it to
1108ensure that no other cookie will be logged.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001109
willy tarreau197e8ec2005-12-17 14:10:59 +01001110Examples :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001111----------
willy tarreau197e8ec2005-12-17 14:10:59 +01001112 # capture the first cookie whose name starts with "ASPSESSION"
willy tarreaueedaa9f2005-12-17 14:08:03 +01001113 capture cookie ASPSESSION len 32
1114
willy tarreau197e8ec2005-12-17 14:10:59 +01001115 # capture the first cookie whose name is exactly "vgnvisitor"
willy tarreaueedaa9f2005-12-17 14:08:03 +01001116 capture cookie vgnvisitor= len 32
1117
willy tarreau197e8ec2005-12-17 14:10:59 +01001118In the logs, the field preceeding the completion indicator contains the cookie
1119value as sent by the server, preceeded by the cookie value as sent by the
1120client. Each of these field is replaced with '-' when no cookie was seen.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001121
willy tarreauc1cae632005-12-17 14:12:23 +010011224.2.5) Examples of logs
1123-----------------------
1124- haproxy[674]: 127.0.0.1:33319 [15/Oct/2003:08:31:57] relais-http Srv1 6559/7/147/6723 200 243 - - ---- "HEAD / HTTP/1.0"
1125 => long request (6.5s) entered by hand through 'telnet'. The server replied
1126 in 147 ms, and the session ended normally ('----')
willy tarreaueedaa9f2005-12-17 14:08:03 +01001127
willy tarreau982249e2005-12-18 00:57:06 +01001128- haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/7/14/+30 200 +243 - - ---- "GET /image.iso HTTP/1.0"
1129 => request for a long data transfer. The 'logasap' option was specified, so
1130 the log was produced just before transfering data. The server replied in
1131 14 ms, 243 bytes of headers were sent to the client, and total time from
1132 accept to first data byte is 30 ms.
1133
1134- haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/7/14/30 502 243 - - PH-- "GET /cgi-bin/bug.cgi? HTTP/1.0"
1135 => the proxy blocked a server response either because of an 'rspdeny' or
1136 'rspideny' filter, or because it blocked sensible information which risked
1137 being cached. In this case, the response is replaced with a '502 bad
1138 gateway'.
1139
willy tarreauc1cae632005-12-17 14:12:23 +01001140- haproxy[18113]: 127.0.0.1:34548 [15/Oct/2003:15:18:55] relais-http <NOSRV> -1/-1/-1/8490 -1 0 - - CR-- ""
1141 => the client never completed its request and aborted itself ('C---') after
1142 8.5s, while the proxy was waiting for the request headers ('-R--').
1143 Nothing was sent to the server.
1144
1145- haproxy[18113]: 127.0.0.1:34549 [15/Oct/2003:15:19:06] relais-http <NOSRV> -1/-1/-1/50001 408 0 - - cR-- ""
1146 => The client never completed its request, which was aborted by the time-out
1147 ('c---') after 50s, while the proxy was waiting for the request headers ('-R--').
1148 Nothing was sent to the server, but the proxy could send a 408 return code
1149 to the client.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001150
willy tarreauc1cae632005-12-17 14:12:23 +01001151- haproxy[18989]: 127.0.0.1:34550 [15/Oct/2003:15:24:28] relais-tcp Srv1 0/5007 0 cD
1152 => This is a 'tcplog' entry. Client-side time-out ('c----') occured after 5s.
1153
1154- haproxy[18989]: 10.0.0.1:34552 [15/Oct/2003:15:26:31] relais-http Srv1 3183/-1/-1/11215 503 0 - - SC-- "HEAD / HTTP/1.0"
1155 => The request took 3s to complete (probably a network problem), and the
1156 connection to the server failed ('SC--') after 4 attemps of 2 seconds
1157 (config says 'retries 3'), then a 503 error code was sent to the client.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001158
willy tarreau4302f492005-12-18 01:00:37 +010011594.2.6) Non-printable characters
1160-------------------------------
1161As of version 1.1.29, non-printable characters are not sent as-is into log
1162files, but are converted to their two-digits hexadecimal representation,
1163prefixed by the character '#'. The only characters that can now be logged
1164without being escaped are between 32 and 126 (inclusive). Obviously, the
1165escape character '#' is also encoded to avoid any ambiguity. It is the same for
1166the character '"', as well as '{', '|' and '}' when logging headers.
1167
11684.2.7) Logging HTTP headers
1169---------------------------
1170As of version 1.1.29, it is now possible to log HTTP headers extracts. It is
1171both possible to include request headers and response headers. It is
1172particularly useful to know what virtual server the client requested, to know
1173the content length during a POST request, or a unique request ID set on a
1174previous proxy. In the response, one can search for information about the
1175response length, how the server asked the cache to behave, or an object location
1176during a redirection. The syntax is :
1177
1178 capture request header <name> len <max length>
1179 capture response header <name> len <max length>
1180
1181Note: Header names are not case-sensitive.
1182
1183Examples:
1184---------
1185 # keep the name of the virtual server
1186 capture request header Host len 20
1187 # keep the amount of data uploaded during a POST
1188 capture request header Content-Length len 10
1189
1190 # note the expected cache behaviour on the response
1191 capture response header Cache-Control len 8
1192 # note the URL location during a redirection
1193 capture response header Location len 20
1194
1195Non-existant headers are logged as empty strings, and if one header appears more
1196than once, only its last occurence will be kept. Request headers are grouped
1197within braces '{' and '}' in the same order as they were declared, and delimited
1198with a vertical bar '|' without any space. Response headers follow the same
1199representation, but are displayed after a space following the request headers
1200block. These blocks are displayed just before the HTTP request in the logs.
1201Example :
1202
1203Config:
1204
1205 capture request header Host len 20
1206 capture request header Content-Length len 10
1207 capture request header Referer len 20
1208 capture response header Server len 20
1209 capture response header Content-Length len 10
1210 capture response header Cache-Control len 8
1211 capture response header Via len 20
1212 capture response header Location len 20
1213
1214Log :
1215
1216Aug 9 20:26:09 localhost haproxy[2022]: 127.0.0.1:34014 [09/Aug/2004:20:26:09] relais-http netcache 0/0/162/+162 200 +350 - - ---- {fr.adserver.yahoo.co||http://fr.f416.mail.} {|864|private||} "GET http://fr.adserver.yahoo.com/"
1217Aug 9 20:30:46 localhost haproxy[2022]: 127.0.0.1:34020 [09/Aug/2004:20:30:46] relais-http netcache 0/0/182/+182 200 +279 - - ---- {w.ods.org||} {Formilux/0.1.8|3495|||} "GET http://w.ods.org/sytadin.html HTTP/1.1"
1218Aug 9 20:30:46 localhost haproxy[2022]: 127.0.0.1:34028 [09/Aug/2004:20:30:46] relais-http netcache 0/2/126/+128 200 +223 - - ---- {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"
1219
willy tarreau197e8ec2005-12-17 14:10:59 +010012204.3) HTTP header manipulation
1221-----------------------------
1222In HTTP mode, it is possible to rewrite, add or delete some of the request and
1223response headers based on regular expressions. It is also possible to block a
1224request or a response if a particular header matches a regular expression,
1225which is enough to stops most elementary protocol attacks, and to protect
1226against information leak from the internal network. But there is a limitation
1227to this : since haproxy's HTTP engine knows nothing about keep-alive, only
1228headers passed during the first request of a TCP session will be seen. All
1229subsequent headers will be considered data only and not analyzed. Furthermore,
1230haproxy doesn't touch data contents, it stops at the end of headers.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001231
willy tarreau197e8ec2005-12-17 14:10:59 +01001232The syntax is :
1233 reqadd <string> to add a header to the request
1234 reqrep <search> <replace> to modify the request
1235 reqirep <search> <replace> same, but ignoring the case
1236 reqdel <search> to delete a header in the request
1237 reqidel <search> same, but ignoring the case
1238 reqallow <search> definitely allow a request if a header matches <search>
1239 reqiallow <search> same, but ignoring the case
1240 reqdeny <search> denies a request if a header matches <search>
1241 reqideny <search> same, but ignoring the case
1242 reqpass <search> ignore a header matching <search>
1243 reqipass <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01001244
willy tarreau197e8ec2005-12-17 14:10:59 +01001245 rspadd <string> to add a header to the response
1246 rsprep <search> <replace> to modify the response
1247 rspirep <search> <replace> same, but ignoring the case
1248 rspdel <search> to delete the response
1249 rspidel <search> same, but ignoring the case
willy tarreau982249e2005-12-18 00:57:06 +01001250 rspdeny <search> replaces a response with a HTTP 502 if a header matches <search>
1251 rspideny <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01001252
1253
willy tarreau197e8ec2005-12-17 14:10:59 +01001254<search> is a POSIX regular expression (regex) which supports grouping through
1255parenthesis (without the backslash). Spaces and other delimiters must be
1256prefixed with a backslash ('\') to avoid confusion with a field delimiter.
1257Other characters may be prefixed with a backslash to change their meaning :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001258
willy tarreau197e8ec2005-12-17 14:10:59 +01001259 \t for a tab
1260 \r for a carriage return (CR)
1261 \n for a new line (LF)
1262 \ to mark a space and differentiate it from a delimiter
1263 \# to mark a sharp and differentiate it from a comment
1264 \\ to use a backslash in a regex
1265 \\\\ to use a backslash in the text (*2 for regex, *2 for haproxy)
1266 \xXX to write the ASCII hex code XX as in the C language
willy tarreaueedaa9f2005-12-17 14:08:03 +01001267
1268
willy tarreau197e8ec2005-12-17 14:10:59 +01001269<replace> containst the string to be used to replace the largest portion of text
1270matching the regex. It can make use of the special characters above, and can
1271reference a substring delimited by parenthesis in the regex, by the group
1272numerical order from 1 to 9. In this case, you would write a backslah ('\')
1273immediately followed by one digit indicating the group position.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001274
willy tarreau197e8ec2005-12-17 14:10:59 +01001275<string> represents the string which will systematically be added after the last
1276header line. It can also use special characters above.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001277
willy tarreau197e8ec2005-12-17 14:10:59 +01001278Notes :
1279-------
1280 - the first line is considered as a header, which makes it possible to rewrite
1281 or filter HTTP requests URIs or response codes.
1282 - 'reqrep' is the equivalent of 'cliexp' in version 1.0, and 'rsprep' is the
1283 equivalent of 'srvexp' in 1.0. Those names are still supported but
1284 deprecated.
1285 - for performances reasons, the number of characters added to a request or to
1286 a response is limited to 4096 since version 1.1.5 (it was 256 before). This
1287 value is easy to modify in the code if needed (#define). If it is too short
1288 on occasional uses, it is possible to gain some space by removing some
1289 useless headers before adding new ones.
willy tarreau982249e2005-12-18 00:57:06 +01001290 - a denied request will generate an "HTTP 403 forbidden" response, while a
1291 denied response will generate an "HTTP 502 Bad gateway" response.
1292
willy tarreaueedaa9f2005-12-17 14:08:03 +01001293
willy tarreau197e8ec2005-12-17 14:10:59 +01001294Examples :
1295----------
1296 ###### a few examples ######
1297
1298 # rewrite 'online.fr' instead of 'free.fr' for GET and POST requests
1299 reqrep ^(GET\ .*)(.free.fr)(.*) \1.online.fr\3
1300 reqrep ^(POST\ .*)(.free.fr)(.*) \1.online.fr\3
1301
1302 # force proxy connections to close
willy tarreaueedaa9f2005-12-17 14:08:03 +01001303 reqirep ^Proxy-Connection:.* Proxy-Connection:\ close
willy tarreau197e8ec2005-12-17 14:10:59 +01001304 # rewrite locations
willy tarreaueedaa9f2005-12-17 14:08:03 +01001305 rspirep ^(Location:\ )([^:]*://[^/]*)(.*) \1\3
willy tarreaueedaa9f2005-12-17 14:08:03 +01001306
willy tarreau197e8ec2005-12-17 14:10:59 +01001307 ###### A full configuration being used on production ######
willy tarreaueedaa9f2005-12-17 14:08:03 +01001308
willy tarreau197e8ec2005-12-17 14:10:59 +01001309 # Every header should end with a colon followed by one space.
1310 reqideny ^[^:\ ]*[\ ]*$
willy tarreaueedaa9f2005-12-17 14:08:03 +01001311
willy tarreau197e8ec2005-12-17 14:10:59 +01001312 # block Apache chunk exploit
1313 reqideny ^Transfer-Encoding:[\ ]*chunked
1314 reqideny ^Host:\ apache-
willy tarreaueedaa9f2005-12-17 14:08:03 +01001315
willy tarreau197e8ec2005-12-17 14:10:59 +01001316 # block annoying worms that fill the logs...
1317 reqideny ^[^:\ ]*\ .*(\.|%2e)(\.|%2e)(%2f|%5c|/|\\\\)
1318 reqideny ^[^:\ ]*\ ([^\ ]*\ [^\ ]*\ |.*%00)
1319 reqideny ^[^:\ ]*\ .*<script
1320 reqideny ^[^:\ ]*\ .*/(root\.exe\?|cmd\.exe\?|default\.ida\?)
1321
1322 # allow other syntactically valid requests, and block any other method
1323 reqipass ^(GET|POST|HEAD|OPTIONS)\ /.*\ HTTP/1\.[01]$
1324 reqipass ^OPTIONS\ \\*\ HTTP/1\.[01]$
1325 reqideny ^[^:\ ]*\
1326
1327 # force connection:close, thus disabling HTTP keep-alive
willy tarreau982249e2005-12-18 00:57:06 +01001328 option httpclose
willy tarreau197e8ec2005-12-17 14:10:59 +01001329
1330 # change the server name
1331 rspidel ^Server:\
1332 rspadd Server:\ Formilux/0.1.8
1333
1334
willy tarreau982249e2005-12-18 00:57:06 +01001335Also, the 'forwardfor' option creates an HTTP 'X-Forwarded-For' header which
willy tarreauc1cae632005-12-17 14:12:23 +01001336contains the client's IP address. This is useful to let the final web server
1337know what the client address was (eg for statistics on domains).
1338
willy tarreau982249e2005-12-18 00:57:06 +01001339Last, the 'httpclose' option removes any 'Connection' header both ways, and
1340adds a 'Connection: close' header in each direction. This makes it easier to
1341disable HTTP keep-alive than the previous 4-rules block..
1342
willy tarreauc1cae632005-12-17 14:12:23 +01001343Example :
1344---------
1345 listen http_proxy 0.0.0.0:80
1346 mode http
1347 log global
1348 option httplog
1349 option dontlognull
1350 option forwardfor
willy tarreau982249e2005-12-18 00:57:06 +01001351 option httpclose
willy tarreau197e8ec2005-12-17 14:10:59 +01001352
13534.4) Load balancing with persistence
1354------------------------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01001355Combining cookie insertion with internal load balancing allows to transparently
1356bring persistence to applications. The principle is quite simple :
1357 - assign a cookie value to each server
1358 - enable the load balancing between servers
1359 - insert a cookie into responses resulting from the balancing algorithm
1360 (indirect accesses), end ensure that no upstream proxy will cache it.
1361 - remove the cookie in the request headers so that the application never sees
1362 it.
1363
1364Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001365---------
1366 listen application 0.0.0.0:80
1367 mode http
1368 cookie SERVERID insert nocache indirect
1369 balance roundrobin
1370 server 192.168.1.1:80 cookie server01 check
1371 server 192.168.1.2:80 cookie server02 check
1372
willy tarreau982249e2005-12-18 00:57:06 +010013734.5) Protection against information leak from the servers
1374---------------------------------------------------------
1375In versions 1.1.28/1.2.1, a new option 'checkcache' was created. It carefully
1376checks 'Cache-control', 'Pragma' and 'Set-cookie' headers in server response
1377to check if there's a risk of caching a cookie on a client-side proxy. When this
1378option is enabled, the only responses which can be delivered to the client are :
1379 - all those without 'Set-Cookie' header ;
1380 - all those with a return code other than 200, 203, 206, 300, 301, 410,
1381 provided that the server has not set a 'Cache-control: public' header ;
1382 - all those that come from a POST request, provided that the server has not
1383 set a 'Cache-Control: public' header ;
1384 - those with a 'Pragma: no-cache' header
1385 - those with a 'Cache-control: private' header
1386 - those with a 'Cache-control: no-store' header
1387 - those with a 'Cache-control: max-age=0' header
1388 - those with a 'Cache-control: s-maxage=0' header
1389 - those with a 'Cache-control: no-cache' header
1390 - those with a 'Cache-control: no-cache="set-cookie"' header
1391 - those with a 'Cache-control: no-cache="set-cookie,' header
1392 (allowing other fields after set-cookie)
willy tarreaueedaa9f2005-12-17 14:08:03 +01001393
willy tarreau982249e2005-12-18 00:57:06 +01001394If a response doesn't respect these requirements, then it will be blocked just
1395as if it was from an 'rspdeny' filter, with an "HTTP 502 bad gateway". The
1396session state shows "PH--" meaning that the proxy blocked the response during
1397headers processing. Additionnaly, an alert will be sent in the logs so that
1398admins are told that there's something to be done.
1399
14004.6) Customizing errors
1401-----------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01001402Some situations can make haproxy return an HTTP error code to the client :
1403 - invalid or too long request => HTTP 400
1404 - request not completely sent in time => HTTP 408
1405 - forbidden request (matches a deny filter) => HTTP 403
1406 - internal error in haproxy => HTTP 500
1407 - the server returned an invalid or incomplete response => HTTP 502
1408 - no server was available to handle the request => HTTP 503
1409 - the server failed to reply in time => HTTP 504
willy tarreaueedaa9f2005-12-17 14:08:03 +01001410
willy tarreau197e8ec2005-12-17 14:10:59 +01001411A succint error message taken from the RFC accompanies these return codes.
1412But depending on the clients knowledge, it may be better to return custom, user
1413friendly, error pages. This is made possible through the use of the 'errorloc'
1414command :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001415
willy tarreau197e8ec2005-12-17 14:10:59 +01001416 errorloc <HTTP_code> <location>
willy tarreaueedaa9f2005-12-17 14:08:03 +01001417
willy tarreau197e8ec2005-12-17 14:10:59 +01001418Instead of generating an HTTP error <HTTP_code> among those above, the proxy
1419will return a temporary redirection code (HTTP 302) towards the address
1420specified in <location>. This address may be either relative to the site or
1421absolute. Since this request will be handled by the client's browser, it's
1422mandatory that the returned address be reachable from the outside.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001423
willy tarreau197e8ec2005-12-17 14:10:59 +01001424Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001425---------
1426 listen application 0.0.0.0:80
1427 errorloc 400 /badrequest.html
1428 errorloc 403 /forbidden.html
1429 errorloc 408 /toolong.html
1430 errorloc 500 http://haproxy.domain.net/bugreport.html
1431 errorloc 502 http://192.168.114.58/error50x.html
1432 errorloc 503 http://192.168.114.58/error50x.html
1433 errorloc 504 http://192.168.114.58/error50x.html
1434
willy tarreau982249e2005-12-18 00:57:06 +010014354.7) Modifying default values
willy tarreau197e8ec2005-12-17 14:10:59 +01001436-----------------------------
willy tarreau197e8ec2005-12-17 14:10:59 +01001437Version 1.1.22 introduced the notion of default values, which eliminates the
1438pain of often repeating common parameters between many instances, such as
1439logs, timeouts, modes, etc...
willy tarreaueedaa9f2005-12-17 14:08:03 +01001440
willy tarreau197e8ec2005-12-17 14:10:59 +01001441Default values are set in a 'defaults' section. Each of these section clears
1442all previously set default parameters, so there may be as many default
1443parameters as needed. Only the last one before a 'listen' section will be
1444used for this section. The 'defaults' section uses the same syntax as the
1445'listen' section, for the supported parameters. The 'defaults' keyword ignores
1446everything on its command line, so that fake instance names can be specified
1447there for better clarity.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001448
willy tarreau982249e2005-12-18 00:57:06 +01001449In version 1.1.28/1.2.1, only those parameters can be preset in the 'default'
willy tarreau197e8ec2005-12-17 14:10:59 +01001450section :
1451 - log (the first and second one)
willy tarreaueedaa9f2005-12-17 14:08:03 +01001452 - mode { tcp, http, health }
1453 - balance { roundrobin }
willy tarreau197e8ec2005-12-17 14:10:59 +01001454 - disabled (to disable every further instances)
1455 - enabled (to enable every further instances, this is the default)
willy tarreaueedaa9f2005-12-17 14:08:03 +01001456 - contimeout, clitimeout, srvtimeout, grace, retries, maxconn
willy tarreau982249e2005-12-18 00:57:06 +01001457 - option { redispatch, transparent, keepalive, forwardfor, logasap, httpclose,
1458 checkcache, httplog, tcplog, dontlognull, persist, httpchk }
willy tarreaueedaa9f2005-12-17 14:08:03 +01001459 - redispatch, redisp, transparent, source { addr:port }
1460 - cookie, capture
1461 - errorloc
1462
willy tarreau197e8ec2005-12-17 14:10:59 +01001463As of 1.1.24, it is not possible to put certain parameters in a 'defaults'
1464section, mainly regular expressions and server configurations :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001465 - dispatch, server,
willy tarreau197e8ec2005-12-17 14:10:59 +01001466 - req*, rsp*
willy tarreaueedaa9f2005-12-17 14:08:03 +01001467
willy tarreau197e8ec2005-12-17 14:10:59 +01001468Last, there's no way yet to change a boolean option from its assigned default
1469value. So if an 'option' statement is set in a 'defaults' section, the only
1470way to flush it is to redefine a new 'defaults' section without this 'option'.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001471
willy tarreau197e8ec2005-12-17 14:10:59 +01001472Examples :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001473----------
1474 defaults applications TCP
1475 log global
1476 mode tcp
1477 balance roundrobin
1478 clitimeout 180000
1479 srvtimeout 180000
1480 contimeout 4000
1481 retries 3
1482 redispatch
1483
1484 listen app_tcp1 10.0.0.1:6000-6063
1485 server srv1 192.168.1.1 check port 6000 inter 10000
1486 server srv2 192.168.1.2 backup
1487
1488 listen app_tcp2 10.0.0.2:6000-6063
1489 server srv1 192.168.2.1 check port 6000 inter 10000
1490 server srv2 192.168.2.2 backup
1491
1492 defaults applications HTTP
1493 log global
1494 mode http
1495 option httplog
1496 option forwardfor
1497 option dontlognull
1498 balance roundrobin
1499 clitimeout 20000
1500 srvtimeout 20000
1501 contimeout 4000
1502 retries 3
1503
1504 listen app_http1 10.0.0.1:80-81
1505 cookie SERVERID postonly insert indirect
1506 capture cookie userid= len 10
1507 server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000
1508 server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000
1509
1510 defaults
willy tarreau197e8ec2005-12-17 14:10:59 +01001511 # this empty section voids all default parameters
willy tarreaueedaa9f2005-12-17 14:08:03 +01001512
willy tarreau197e8ec2005-12-17 14:10:59 +01001513=========================
1514| System-specific setup |
1515=========================
willy tarreaueedaa9f2005-12-17 14:08:03 +01001516
willy tarreau197e8ec2005-12-17 14:10:59 +01001517Linux 2.4
1518=========
willy tarreaueedaa9f2005-12-17 14:08:03 +01001519
1520-- cut here --
1521#!/bin/sh
1522# set this to about 256/4M (16384 for 256M machine)
1523MAXFILES=16384
1524echo $MAXFILES > /proc/sys/fs/file-max
1525ulimit -n $MAXFILES
1526
1527if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then
1528 echo 65536 > /proc/sys/net/ipv4/ip_conntrack_max
1529fi
1530
1531if [ -e /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait ]; then
1532 # 30 seconds for fin, 15 for time wait
1533 echo 3000 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait
1534 echo 1500 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_time_wait
1535 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_invalid_scale
1536 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_out_of_window
1537fi
1538
1539echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range
1540echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
1541echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog
1542echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets
1543echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans
1544echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
1545echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
1546echo 0 > /proc/sys/net/ipv4/tcp_timestamps
1547echo 0 > /proc/sys/net/ipv4/tcp_ecn
1548echo 0 > /proc/sys/net/ipv4/tcp_sack
1549echo 0 > /proc/sys/net/ipv4/tcp_dsack
1550
1551# auto-tuned on 2.4
1552#echo 262143 > /proc/sys/net/core/rmem_max
1553#echo 262143 > /proc/sys/net/core/rmem_default
1554
1555echo 16384 65536 524288 > /proc/sys/net/ipv4/tcp_rmem
1556echo 16384 349520 699040 > /proc/sys/net/ipv4/tcp_wmem
1557
1558-- cut here --
1559
willy tarreau197e8ec2005-12-17 14:10:59 +01001560
1561FreeBSD
1562=======
1563
1564A FreeBSD port of HA-Proxy is now available and maintained, thanks to
1565Clement Laforet <sheepkiller@cultdeadsheep.org>.
1566
1567For more information :
1568http://www.freebsd.org/cgi/url.cgi?ports/net/haproxy/pkg-descr
1569http://www.freebsd.org/cgi/cvsweb.cgi/ports/net/haproxy/
1570http://www.freshports.org/net/haproxy
1571
1572
1573-- end --