blob: ad825212d76448a7988c3ef021197f2589b211cd [file] [log] [blame]
willy tarreaueedaa9f2005-12-17 14:08:03 +01001
2 H A - P r o x y
3 ---------------
willy tarreaufe2c5c12005-12-17 14:14:34 +01004 version 1.1.27
willy tarreaueedaa9f2005-12-17 14:08:03 +01005 willy tarreau
willy tarreaufe2c5c12005-12-17 14:14:34 +01006 2003/10/27
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 tarreaufe2c5c12005-12-17 14:14:34 +010038 -p <pidfile> asks the process to write down each of its children's
39 pids to this file in daemon mode.
willy tarreaueedaa9f2005-12-17 14:08:03 +010040 -s shows statistics (only if compiled in)
41 -l shows even more statistics (implies '-s')
42
43
44The maximal number of connections per proxy is used as the default parameter for
45each instance for which the 'maxconn' paramter is not set in the 'listen' section.
46
47The maximal number of total connections limits the number of connections used by
48the whole process if the 'maxconn' parameter is not set in the 'global' section.
49
50The debugging mode has the same effect as the 'debug' option in the 'global'
51section. When the proxy runs in this mode, it dumps every connections,
52disconnections, timestamps, and HTTP headers to stdout. This should NEVER
53be used in an init script since it will prevent the system from starting up.
54
55Statistics are only available if compiled in with the 'STATTIME' option. It's
56only used during code optimization phases.
57
58======================
59| Configuration file |
60======================
61
62Structure
63=========
64
65The configuration file parser ignores empty lines, spaces, tabs. Anything
66between a sharp ('#') not following a backslash ('\'), and the end of a line
67constitutes a comment and is ignored too.
68
69The configuration file is segmented in sections. A section begins whenever
70one of these 3 keywords are encountered :
71
72 - 'global'
73 - 'listen'
74 - 'defaults'
75
76Every parameter refer to the section beginning at the last one of these 3
77keywords.
78
79
801) Global parameters
81====================
82
83Global parameters affect the whole process behaviour. They are all set in the
84'global' section. There may be several 'global' sections if needed, but their
85parameters will only be merged. Allowed parameters in 'global' section include
86the following ones :
87
88 - log <address> <facility> [max_level]
89 - maxconn <number>
90 - uid <user id>
91 - gid <group id>
92 - chroot <directory>
93 - nbproc <number>
94 - daemon
95 - debug
96 - quiet
willy tarreaufe2c5c12005-12-17 14:14:34 +010097 - pidfile <file>
willy tarreaueedaa9f2005-12-17 14:08:03 +010098
991.1) Event logging
100------------------
101Most events are logged : start, stop, servers going up and down, connections and
102errors. Each event generates a syslog message which can be sent to up to 2
103servers. The syntax is :
104
105 log <ip_address> <facility> [max_level]
106
107Connections are logged at level "info". Services initialization and servers
108going up are logged at level "notice", termination signals are logged at
109"warning", and definitive service termination, as well as loss of servers are
110logged at level "alert". The optional parameter <max_level> specifies above
111what level messages should be sent. Level can take one of these 8 values :
112
113 emerg, alert, crit, err, warning, notice, info, debug
114
115For backwards compatibility with versions 1.1.16 and earlier, the default level
116value is "debug" if not specified.
117
118Permitted facilities are :
119 kern, user, mail, daemon, auth, syslog, lpr, news,
120 uucp, cron, auth2, ftp, ntp, audit, alert, cron2,
121 local0, local1, local2, local3, local4, local5, local6, local7
122
123According to RFC3164, messages are truncated to 1024 bytes before being emitted.
124
125Example :
126---------
127 global
128 log 192.168.2.200 local3
129 log 127.0.0.1 local4 notice
130
1311.2) limiting the number of connections
132---------------------------------------
133It is possible and recommended to limit the global number of per-process
134connections. Since one connection includes both a client and a server, it
135means that the max number of TCP sessions will be about the double of this
136number. It's important to understand this when trying to find best values
137for 'ulimit -n' before starting the proxy. To anticipate the number of
138sockets needed, all these parameters must be counted :
139
140 - 1 socket per incoming connection
141 - 1 socket per outgoing connection
142 - 1 socket per address/port/proxy tuple.
143 - 1 socket per server being health-checked
144 - 1 socket for all logs
145
146In simple configurations where each proxy only listens one one address/port,
147set the limit of file descriptors (ulimit -n) to
148(2 * maxconn + nbproxies + nbservers + 1). In a future release, haproxy may
149be able to set this value itself.
150
1511.3) Drop of priviledges
152------------------------
153In order to reduce the risk and consequences of attacks, in the event where a
154yet non-identified vulnerability would be successfully exploited, it's possible
155to lower the process priviledges and even isolate it in a riskless directory.
156
157In the 'global' section, the 'uid' parameter sets a numerical user identifier
158which the process will switch to after binding its listening sockets. The value
159'0', which normally represents the super-user, here indicates that the UID must
160not change during startup. It's the default behaviour. The 'gid' parameter does
161the same for the group identifier. It's particularly advised against use of
162generic accounts such as 'nobody' because it has the same consequences as using
163'root' if other services use them.
164
165The 'chroot' parameter makes the process isolate itself in an empty directory
166just before switching its UID. This type of isolation (chroot) can sometimes
167be worked around on certain OS (Linux, Solaris), provided that the attacker
168has gained 'root' priviledges and has the ability to use or create a directory.
169For this reason, it's capital to use a dedicated directory and not to share one
170between several services of different nature. To make isolation more resistant,
171it's recommended to use an empty directory without any right, and to change the
172UID of the process so that it cannot do anything there.
173
174Note: in the event where such a vulnerability would be exploited, it's most
175likely that first attempts would kill the process due to 'Segmentation Fault',
176'Bus Error' or 'Illegal Instruction' signals. Eventhough it's true that
177isolating the server reduces the risks of intrusion, it's sometimes useful to
178find why a process dies, via the analysis of a 'core' file, although very rare
179(the last bug of this sort was fixed in 1.1.9). For security reasons, most
180systems disable the generation of core file when a process changes its UID. So
181the two workarounds are either to start the process from a restricted user
182account, which will not be able to chroot itself, or start it as root and not
183change the UID. In both cases the core will be either in the start or the chroot
184directories. Do not forget to allow core dumps prior to start the process :
185
186# ulimit -c unlimited
187
188Example :
189---------
190
191 global
192 uid 30000
193 gid 30000
194 chroot /var/chroot/haproxy
195
1961.4) Startup modes
197------------------
198The service can start in several different :
199 - foreground / background
200 - quiet / normal / debug
201
202The default mode is normal, foreground, which means that the program doesn't
203return once started. NEVER EVER use this mode in a system startup script, or
204the system won't boot. It needs to be started in background, so that it
205returns immediately after forking. That's accomplished by the 'daemon' option
206in the 'global' section, which is the equivalent of the '-D' command line
207argument.
208
209Moreover, certain alert messages are still sent to the standard output even
210in 'daemon' mode. To make them disappear, simply add the 'quiet' option in the
211'global' section. This option has no command-line equivalent.
212
213Last, the 'debug' mode, enabled with the 'debug' option in the 'global' section,
214and which is equivalent of the '-d' option, allows deep TCP/HTTP analysis, with
215timestamped display of each connection, disconnection, and HTTP headers for both
216ways. This mode is incompatible with 'daemon' and 'quiet' modes for obvious
217reasons.
218
2191.5) Increasing the overall processing power
220--------------------------------------------
221On multi-processor systems, it may seem to be a shame to use only one processor,
222eventhough the load needed to saturate a recent processor are far above common
223usage. Anyway, for very specific needs, the proxy can start several processes
224between which the operating system will spread the incoming connections. The
225number of processes is controlled by the 'nbproc' parameter in the 'global'
226section. It defaults to 1, and obviously works only in 'daemon' mode.
227
228Example :
229---------
230
231 global
232 daemon
233 quiet
234 nbproc 2
235
236
willy tarreaufe2c5c12005-12-17 14:14:34 +01002371.6) Helping process management
238-------------------------------
239Haproxy now supports the notion of pidfile. If the '-p' command line argument,
240or the 'pidfile' global option is followed with a file name, this file will be
241removed, then filled with all children's pids, one per line (only in daemon
242mode). This file is NOT within the chroot, which allows to work with a readonly
243 chroot. It will be owned by the user starting the process, and will have
244permissions 0644.
245
246Example :
247---------
248
249 global
250 daemon
251 quiet
252 nbproc 2
253 pidfile /var/run/haproxy-private.pid
254
255 # to stop only those processes among others :
256 # kill $(</var/run/haproxy-private.pid)
257
258
willy tarreaueedaa9f2005-12-17 14:08:03 +01002592) Declaration of a listening service
260=====================================
261
262Service sections start with the 'listen' keyword :
263
264 listen <instance_name> [ <IP_address>:<port_range>[,...] ]
265
266- <instance_name> is the name of the instance. This name will be reported in
267 logs, so it is good to have it reflect the proxied service. No unicity test
268 is done on this name, and it's not mandatory for it to be unique, but highly
269 recommended.
270
271- <IP_address> is the IP address the proxy binds to. Empty address, '*' and
272 '0.0.0.0' all mean that the proxy listens to all valid addresses on the
273 system.
274
275- <port_range> is either a unique port, or a port range for which the proxy will
276 accept connections for the IP address specified above. This range can be :
277 - a numerical port (ex: '80')
278 - a dash-delimited ports range explicitly stating the lower and upper bounds
279 (ex: '2000-2100') which are included in the range.
280
281 Particular care must be taken against port ranges, because every <addr:port>
282 couple consumes one socket (=a file descriptor), so it's easy to eat lots of
283 descriptors with a simple range. The <addr:port> couple must be used only once
284 among all instances running on a same system. Please note that attaching to
285 ports lower than 1024 need particular priviledges to start the program, which
286 are independant of the 'uid' parameter.
287
288- the <IP_address>:<port_range> couple may be repeated indefinitely to require
289 the proxy to listen to other addresses and/or ports. To achieve this, simply
290 separate them with a coma.
291
292Examples :
293---------
294 listen http_proxy :80
295 listen x11_proxy 127.0.0.1:6000-6009
296 listen smtp_proxy 127.0.0.1:25,127.0.0.1:587
297 listen ldap_proxy :389,:663
298
299In the event that all addresses do not fit line width, it's preferable to
300detach secondary addresses on other lines with the 'bind' keyword. If this
301keyword is used, it's not even necessary to specify the first address on the
302'listen' line, which sometimes makes multiple configuration handling easier :
303
304 bind [ <IP_address>:<port_range>[,...] ]
305
306Examples :
307----------
308 listen http_proxy
309 bind :80,:443
310 bind 10.0.0.1:10080,10.0.0.1:10443
311
3122.1) Inhibiting a service
313-------------------------
314A service may be disabled for maintenance reasons, without needing to comment
315out the whole section, simply by specifying the 'disabled' keyword in the
316section to be disabled :
317
318 listen smtp_proxy 0.0.0.0:25
319 disabled
320
321Note: the 'enabled' keyword allows to enable a service which has been disabled
322 previously by a default configuration.
323
3242.2) Modes of operation
325-----------------------
326A service can work in 3 different distinct modes :
327 - TCP
328 - HTTP
329 - monitoring
330
331TCP mode
332--------
333In this mode, the service relays TCP connections as soon as they're established,
334towards one or several servers. No processing is done on the stream. It's only
335an association of source(addr:port) -> destination(addr:port). To use this mode,
336you must specify 'mode tcp' in the 'listen' section. This is the default mode.
337
338Example :
339---------
340 listen smtp_proxy 0.0.0.0:25
341 mode tcp
342
343HTTP mode
344---------
345In this mode, the service relays TCP connections towards one or several servers,
346when it has enough informations to decide, which normally means that all HTTP
347headers have been read. Some of them may be scanned for a cookie or a pattern
348matching a regex. To use this mode, specify 'mode http' in the 'listen' section.
349
350Example :
351---------
352 listen http_proxy 0.0.0.0:80
353 mode http
354
355Health-checking mode
356--------------------
357This mode provides a way for external components to check the proxy's health.
358It is meant to be used with intelligent load-balancers which can use send/expect
359scripts to check for all of their servers' availability. This one simply accepts
willy tarreau197e8ec2005-12-17 14:10:59 +0100360the connection, returns the word 'OK' and closes it. If the 'option httpchk' is
361set, then the reply will be 'HTTP/1.0 200 OK' with no data, so that it can be
362tested from a tool which supports HTTP health-checks. To enable it, simply
willy tarreaueedaa9f2005-12-17 14:08:03 +0100363specify 'health' as the working mode :
364
365Example :
366---------
willy tarreau197e8ec2005-12-17 14:10:59 +0100367 # simple response : 'OK'
willy tarreaueedaa9f2005-12-17 14:08:03 +0100368 listen health_check 0.0.0.0:60000
369 mode health
370
willy tarreau197e8ec2005-12-17 14:10:59 +0100371 # HTTP response : 'HTTP/1.0 200 OK'
372 listen http_health_check 0.0.0.0:60001
373 mode health
374 option httpchk
375
willy tarreaueedaa9f2005-12-17 14:08:03 +0100376
3772.3) Limiting the number of simultaneous connections
378----------------------------------------------------
379The 'maxconn' parameter allows a proxy to refuse connections above a certain
380amount of simultaneous ones. When the limit is reached, it simply stops
381listening, but the system may still be accepting them because of the back log
382queue. These connections will be processed further when other ones have freed
383some slots. This provides a serialization effect which helps very fragile
384servers resist to high loads. Se further for system limitations.
385
386Example :
387---------
388 listen tiny_server 0.0.0.0:80
389 maxconn 10
390
391
3922.4) Soft stop
393--------------
394It is possible to stop services without breaking existing connections by the
395sending of the SIG_USR1 signal to the process. All services are then put into
396soft-stop state, which means that they will refuse to accept new connections,
397except for those which have a non-zero value in the 'grace' parameter, in which
398case they will still accept connections for the specified amount of time, in
399milliseconds. This allows to tell a load-balancer that the service is failing,
400while still doing the job during the time it needs to detect it.
401
402Note: active connections are never killed. In the worst case, the user will have
403to wait for all of them to close or to time-out, or simply kill the process
404normally (SIG_TERM). The default 'grace' value is '0'.
405
406Example :
407---------
408 # enter soft stop after 'killall -USR1 haproxy'
409 # the service will still run 10 seconds after the signal
410 listen http_proxy 0.0.0.0:80
411 mode http
412 grace 10000
413
414 # this port is dedicated to a load-balancer, and must fail immediately
415 listen health_check 0.0.0.0:60000
416 mode health
417 grace 0
418
419
4202.5) Connections expiration time
421--------------------------------
422It is possible (and recommended) to configure several time-outs on TCP
423connections. Three independant timers are adjustable with values specified
424in milliseconds. A session will be terminated if either one of these timers
425expire.
426
427 - the time we accept to wait for data from the client, or for the client to
428 accept data : 'clitimeout' :
429
430 # client time-out set to 2mn30.
431 clitimeout 150000
432
433 - the time we accept to wait for data from the server, or for the server to
434 accept data : 'srvtimeout' :
435
436 # server time-out set to 30s.
437 srvtimeout 30000
438
439 - the time we accept to wait for a connection to establish on a server :
440 'contimeout' :
441
442 # we give up if the connection does not complete within 4 seconds
443 contimeout 4000
444
445Notes :
446-------
447 - 'contimeout' and 'srvtimeout' have no sense on 'health' mode servers ;
448 - under high loads, or with a saturated or defective network, it's possible
449 that some packets get lost. Since the first TCP retransmit only happens
450 after 3 seconds, a time-out equal to, or lower than 3 seconds cannot
451 compensate for a packet loss. A 4 seconds time-out seems a reasonable
452 minimum which will considerably reduce connection failures.
453
4542.6) Attempts to reconnect
455--------------------------
456After a connection failure to a server, it is possible to retry, potentially
457on another server. This is useful if health-checks are too rare and you don't
458want the clients to see the failures. The number of attempts to reconnect is
459set by the 'retries' paramter.
460
461Example :
462---------
463 # we can retry 3 times max after a failure
464 retries 3
465
466
4672.7) Address of the dispatch server (deprecated)
468------------------------------------------------
469The server which will be sent all new connections is defined by the 'dispatch'
470parameter, in the form <address>:<port>. It generally is dedicated to unknown
471connections and will assign them a cookie, in case of HTTP persistence mode,
472or simply is a single server in case of generic TCP proxy. This old mode is only
473provided for backwards compatibility, but doesn't allow to check remote servers
474state, and has a rather limited usage. All new setups should switch to 'balance'
475mode. The principle of the dispatcher is to be able to perform the load
476balancing itself, but work only on new clients so that the server doesn't need
477to be a big machine.
478
479Example :
480---------
481 # all new connections go there
482 dispatch 192.168.1.2:80
483
484Note :
485------
486This parameter has no sense for 'health' servers, and is incompatible with
487'balance' mode.
488
489
4902.8) Outgoing source address
491----------------------------
492It is often necessary to bind to a particular address when connecting to some
493remote hosts. This is done via the 'source' parameter which is a per-proxy
494parameter. A newer version may allow to fix different sources to reach different
495servers. The syntax is 'source <address>[:<port>]', where <address> is a valid
496local address (or '0.0.0.0' or '*' or empty to let the system choose), and
497<port> is an optional parameter allowing the user to force the source port for
498very specific needs. If the port is not specified or is '0', the system will
499choose a free port. Note that as of version 1.1.18, the servers health checks
500are also performed from the same source.
501
502Examples :
503----------
504 listen http_proxy *:80
505 # all connections take 192.168.1.200 as source address
506 source 192.168.1.200:0
507
508 listen rlogin_proxy *:513
509 # use address 192.168.1.200 and the reserved port 900 (needs to be root)
510 source 192.168.1.200:900
511
512
5132.9) Setting the cookie name
514----------------------------
515In HTTP mode, it is possible to look for a particular cookie which will contain
516a server identifier which should handle the connection. The cookie name is set
517via the 'cookie' parameter.
518
519Example :
520---------
521 listen http_proxy :80
522 mode http
523 cookie SERVERID
524
525It is possible to change the cookie behaviour to get a smarter persistence,
526depending on applications. It is notably possible to delete or modify a cookie
527emitted by a server, insert a cookie identifying the server in an HTTP response
528and even add a header to tell upstream caches not to cache this response.
529
530Examples :
531----------
532
533To remove the cookie for direct accesses (ie when the server matches the one
534which was specified in the client cookie) :
535
536 cookie SERVERID indirect
537
538To replace the cookie value with the one assigned to the server if any (no
539cookie will be created if the server does not provide one, nor if the
540configuration does not provide one). This lets the application put the cookie
541exactly on certain pages (eg: successful authentication) :
542
543 cookie SERVERID rewrite
544
545To create a new cookie and assign the server identifier to it (in this case, all
546servers should be associated with a valid cookie, since no cookie will simply
547delete the cookie from the client's browser) :
548
549 cookie SERVERID insert
550
551To insert a cookie and ensure that no upstream cache will store it, add the
552'nocache' option :
553
554 cookie SERVERID insert nocache
555
556To insert a cookie only after a POST request, add 'postonly' after 'insert'.
557This has the advantage that there's no risk of caching, and that all pages
558seen before the POST one can still be cached :
559
560 cookie SERVERID insert postonly
561
562Notes :
563-----------
564- it is possible to combine 'insert' with 'indirect' or 'rewrite' to adapt to
565 applications which already generate the cookie with an invalid content.
566
567- in the case where 'insert' and 'indirect' are both specified, the cookie is
568 never transmitted to the server, since it wouldn't understand it. This is
569 the most application-transparent mode.
570
571- it is particularly recommended to use 'nocache' in 'insert' mode if any
572 upstream HTTP/1.0 cache is susceptible to cache the result, because this may
573 lead to many clients going to the same server, or even worse, some clients
574 having their server changed while retrieving a page from the cache.
575
576- when the application is well known and controlled, the best method is to
577 only add the persistence cookie on a POST form because it's up to the
578 application to select which page it wants the upstream servers to cache.
579 In this case, you would use 'insert postonly indirect'.
580
5812.10) Associating a cookie value with a server
582----------------------------------------------
583In HTTP mode, it's possible to associate a cookie value to each server. This
584was initially used in combination with 'dispatch' mode to handle direct accesses
585but it is now the standard way of doing the load balancing. The syntax is :
586
587 server <identifier> <address>:<port> cookie <value>
588
589- <identifier> is any name which can be used to identify the server in the logs.
590- <address>:<port> specifies where the server is bound.
591- <value> is the value to put in or to read from the cookie.
592
593Example : the 'SERVERID' cookie can be either 'server01' or 'server02'
594---------
595 listen http_proxy :80
596 mode http
597 cookie SERVERID
598 dispatch 192.168.1.100:80
599 server web1 192.168.1.1:80 cookie server01
600 server web2 192.168.1.2:80 cookie server02
601
602Warning : the syntax has changed since version 1.0 !
603---------
604
6053) Autonomous load balancer
606===========================
607
608The proxy can perform the load-balancing itself, both in TCP and in HTTP modes.
609This is the most interesting mode which obsoletes the old 'dispatch' mode
610described above. It has advantages such as server health monitoring, multiple
611port binding and port mapping. To use this mode, the 'balance' keyword is used,
612followed by the selected algorithm. As of version 1.1.23, only 'roundrobin' is
613available, which is also the default value if unspecified. In this mode, there
614will be no dispatch address, but the proxy needs at least one server.
615
616Example : same as the last one, with internal load balancer
617---------
618
619 listen http_proxy :80
620 mode http
621 cookie SERVERID
622 balance roundrobin
623 server web1 192.168.1.1:80 cookie server01
624 server web2 192.168.1.2:80 cookie server02
625
626
627Since version 1.1.22, it is possible to automatically determine on which port
628the server will get the connection, depending on the port the client connected
629to. Indeed, there now are 4 possible combinations for the server's <port> field:
630
631 - unspecified or '0' :
632 the connection will be sent to the same port as the one on which the proxy
633 received the client connection itself.
634
635 - numerical value (the only one supported in versions earlier than 1.1.22) :
636 the connection will always be sent to the specified port.
637
638 - '+' followed by a numerical value :
639 the connection will be sent to the same port as the one on which the proxy
640 received the connection, plus this value.
641
642 - '-' followed by a numerical value :
643 the connection will be sent to the same port as the one on which the proxy
644 received the connection, minus this value.
645
646Examples :
647----------
648
649# same as previous example
650
651 listen http_proxy :80
652 mode http
653 cookie SERVERID
654 balance roundrobin
655 server web1 192.168.1.1 cookie server01
656 server web2 192.168.1.2 cookie server02
657
658# simultaneous relaying of ports 80, 81 and 8080-8089
659
660 listen http_proxy :80,:81,:8080-8089
661 mode http
662 cookie SERVERID
663 balance roundrobin
664 server web1 192.168.1.1 cookie server01
665 server web2 192.168.1.2 cookie server02
666
667# relaying of TCP ports 25, 389 and 663 to ports 1025, 1389 and 1663
668
669 listen http_proxy :25,:389,:663
670 mode tcp
671 balance roundrobin
672 server srv1 192.168.1.1:+1000
673 server srv2 192.168.1.2:+1000
674
675
willy tarreau197e8ec2005-12-17 14:10:59 +01006763.1) Server monitoring
677----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +0100678
679It is possible to check the servers status by trying to establish TCP
680connections or even sending HTTP requests to them. A server which fails to
681reply to health checks as expected will not be used by the load balancing
682algorithms. To enable monitoring, add the 'check' keyword on a server line.
683It is possible to specify the interval between tests (in milliseconds) with
684the 'inter' parameter, the number of failures supported before declaring that
685the server has fallen down with the 'fall' parameter, and the number of valid
686checks needed for the server to fully get up with the 'rise' parameter. Since
687version 1.1.22, it is also possible to send checks to a different port
688(mandatory when none is specified) with the 'port' parameter. The default
689values are the following ones :
690
691 - inter : 2000
692 - rise : 2
693 - fall : 3
694 - port : default server port
695
696The default mode consists in establishing TCP connections only. But in certain
697types of application failures, it is often that the server continues to accept
698connections because the system does it itself while the application is running
699an endless loop, or is completely stuck. So in version 1.1.16 were introduced
700HTTP health checks which only performed simple lightweight requests and analysed
701the response. Now, as of version 1.1.23, it is possible to change the HTTP
702method, the URI, and the HTTP version string (which even allows to send headers
703with a dirty trick). To enable HTTP health-checks, use 'option httpchk'.
704
705By default, requests use the 'OPTIONS' method because it's very light and easy
706to filter from logs, and does it on '/'. Only HTTP responses 2xx and 3xx are
707considered valid ones, and only if they come before the time to send a new
708request is reached ('inter' parameter). If some servers block this type of
709request, 3 other forms help to forge a request :
710
711 - option httpchk -> OPTIONS / HTTP/1.0
712 - option httpchk URI -> OPTIONS <URI> HTTP/1.0
713 - option httpchk METH URI -> <METH> <URI> HTTP/1.0
714 - option httpchk METH URI VER -> <METH> <URI> <VER>
715
716See examples below.
717
718Since version 1.1.17, it is possible to specify backup servers. These servers
719are only sollicited when no other server is available. This may only be useful
720to serve a maintenance page, or define one active and one backup server (seldom
721used in TCP mode). To make a server a backup one, simply add the 'backup' option
722on its line. These servers also support cookies, so if a cookie is specified for
723a backup server, clients assigned to this server will stick to it even when the
724other ones come back. Conversely, if no cookie is assigned to such a server,
725the clients will get their cookies removed (empty cookie = removal), and will
726be balanced against other servers once they come back. Please note that there
727is no load-balancing among backup servers. If there are several backup servers,
728the second one will only be used when the first one dies, and so on.
729
730Since version 1.1.17, it is also possible to visually check the status of all
731servers at once. For this, you just have to send a SIGHUP signal to the proxy.
732The servers status will be dumped into the logs at the 'notice' level, as well
733as on <stderr> if not closed. For this reason, it's always a good idea to have
734one local log server at the 'notice' level.
735
736Examples :
737----------
738# same setup as in paragraph 3) with TCP monitoring
739 listen http_proxy 0.0.0.0:80
740 mode http
741 cookie SERVERID
742 balance roundrobin
743 server web1 192.168.1.1:80 cookie server01 check
744 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
745
746# same with HTTP monitoring via 'OPTIONS / HTTP/1.0'
747 listen http_proxy 0.0.0.0:80
748 mode http
749 cookie SERVERID
750 balance roundrobin
751 option httpchk
752 server web1 192.168.1.1:80 cookie server01 check
753 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
754
755# same with HTTP monitoring via 'OPTIONS /index.html HTTP/1.0'
756 listen http_proxy 0.0.0.0:80
757 mode http
758 cookie SERVERID
759 balance roundrobin
760 option httpchk /index.html
761 server web1 192.168.1.1:80 cookie server01 check
762 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
763
764# same with HTTP monitoring via 'HEAD /index.jsp? HTTP/1.1\r\nHost: www'
765 listen http_proxy 0.0.0.0:80
766 mode http
767 cookie SERVERID
768 balance roundrobin
769 option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www
770 server web1 192.168.1.1:80 cookie server01 check
771 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
772
773# automatic insertion of a cookie in the server's response, and automatic
774# deletion of the cookie in the client request, while asking upstream caches
775# not to cache replies.
776 listen web_appl 0.0.0.0:80
777 mode http
778 cookie SERVERID insert nocache indirect
779 balance roundrobin
780 server web1 192.168.1.1:80 cookie server01 check
781 server web2 192.168.1.2:80 cookie server02 check
782
783# same with off-site application backup and local error pages server
784 listen web_appl 0.0.0.0:80
785 mode http
786 cookie SERVERID insert nocache indirect
787 balance roundrobin
788 server web1 192.168.1.1:80 cookie server01 check
789 server web2 192.168.1.2:80 cookie server02 check
790 server web-backup 192.168.2.1:80 cookie server03 check backup
791 server web-excuse 192.168.3.1:80 check backup
792
793# SMTP+TLS relaying with heakth-checks and backup servers
794
795 listen http_proxy :25,:587
796 mode tcp
797 balance roundrobin
798 server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2
799 server srv2 192.168.1.2 backup
800
801
8023.2) Redistribute connections in case of failure
803------------------------------------------------
804In HTTP mode, if a server designated by a cookie does not respond, the clients
805may definitely stick to it because they cannot flush the cookie, so they will
806not be able to access the service anymore. Specifying 'redispatch' will allow
807the proxy to break their persistence and redistribute them to working servers.
808
809Example :
810---------
811 listen http_proxy 0.0.0.0:80
812 mode http
813 cookie SERVERID
814 dispatch 192.168.1.100:80
815 server web1 192.168.1.1:80 cookie server01
816 server web2 192.168.1.2:80 cookie server02
817 redispatch # send back to dispatch in case of connection failure
818
819Up to, and including version 1.1.16, this parameter only applied to connection
820failures. Since version 1.1.17, it also applies to servers which have been
821detected as failed by the health check mechanism. Indeed, a server may be broken
822but still accepting connections, which would not solve every case. But it is
823possible to conserve the old behaviour, that is, make a client insist on trying
824to connect to a server even if it is said to be down, by setting the 'persist'
825option :
826
827 listen http_proxy 0.0.0.0:80
828 mode http
829 option persist
830 cookie SERVERID
831 dispatch 192.168.1.100:80
832 server web1 192.168.1.1:80 cookie server01
833 server web2 192.168.1.2:80 cookie server02
834 redispatch # send back to dispatch in case of connection failure
835
836
8374) Additionnal features
838=======================
839
840Other features are available. They are transparent mode, event logging and
841header rewriting/filtering.
842
8434.1) Transparent mode
844---------------------
845In HTTP mode, the 'transparent' keyword allows to intercept sessions which are
846routed through the system hosting the proxy. This mode was implemented as a
847replacement for the 'dispatch' mode, since connections without cookie will be
848sent to the original address while known cookies will be sent to the servers.
849This mode implies that the system can redirect sessions to a local port.
850
851Example :
852---------
853 listen http_proxy 0.0.0.0:65000
854 mode http
855 transparent
856 cookie SERVERID
857 server server01 192.168.1.1:80
858 server server02 192.168.1.2:80
859
860 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
861 --dport 80 -j REDIRECT --to-ports 65000
862
863Note :
864------
865If the port is left unspecified on the server, the port the client connected to
866will be used. This allows to relay a full port range without using transparent
867mode nor thousands of file descriptors, provided that the system can redirect
868sessions to local ports.
869
870Example :
871---------
872 # redirect all ports to local port 65000, then forward to the server on the
873 # original port.
874 listen http_proxy 0.0.0.0:65000
875 mode tcp
876 server server01 192.168.1.1 check port 60000
877 server server02 192.168.1.2 check port 60000
878
879 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
880 -j REDIRECT --to-ports 65000
881
882
8834.2) Event logging
884------------------
willy tarreauc1cae632005-12-17 14:12:23 +01008854.2.1) Log levels
886-----------------
willy tarreau197e8ec2005-12-17 14:10:59 +0100887TCP and HTTP connections can be logged with informations such as date, time,
888source IP address, destination address, connection duration, response times,
889HTTP request, the HTTP return code, number of bytes transmitted, the conditions
890in which the session ended, and even exchanged cookies values, to track a
891particular user's problems for example. All messages are sent to up to two
892syslog servers. Consult section 1.1 for more info about log facilities. The
893syntax follows :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100894
willy tarreau197e8ec2005-12-17 14:10:59 +0100895 log <address_1> <facility_1> [max_level_1]
896 log <address_2> <facility_2> [max_level_2]
897or
willy tarreaueedaa9f2005-12-17 14:08:03 +0100898 log global
899
willy tarreau197e8ec2005-12-17 14:10:59 +0100900Note :
901------
902The particular syntax 'log global' means that the same log configuration as the
903'global' section will be used.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100904
willy tarreau197e8ec2005-12-17 14:10:59 +0100905Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100906---------
907 listen http_proxy 0.0.0.0:80
908 mode http
909 log 192.168.2.200 local3
910 log 192.168.2.201 local4
911
willy tarreauc1cae632005-12-17 14:12:23 +01009124.2.2) Log format
913-----------------
914By default, connections are logged at the TCP level, as soon as the session
915establishes between the client and the proxy. By enabling the 'tcplog' option,
916the proxy will wait until the session ends to generate an enhanced log
917containing more information such as session duration and its state during the
918disconnection.
919
920Another option, 'httplog', provides more detailed information about HTTP
921contents, such as the request and some cookies. In the event where an external
922component would establish frequent connections to check the service, logs may be
923full of useless lines. So it is possible not to log any session which didn't
924transfer any data, by the setting of the 'dontlognull' option. This only has
925effect on sessions which are established then closed.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100926
willy tarreau197e8ec2005-12-17 14:10:59 +0100927Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100928---------
929 listen http_proxy 0.0.0.0:80
930 mode http
931 option httplog
932 option dontlognull
933 log 192.168.2.200 local3
934
willy tarreauc1cae632005-12-17 14:12:23 +01009354.2.3) Timing events
936--------------------
937Timers provide a great help in trouble shooting network problems. All values
938are reported in milliseconds (ms). In HTTP mode, four control points are
939reported under the form 'Tq/Tc/Tr/Tt' :
940
941 - Tq: total time to get the client request.
942 It's the time elapsed between the moment the client connection was accepted
943 and the moment the proxy received the last HTTP header. The value '-1'
944 indicates that the end of headers (empty line) has never been seen.
945
946 - Tc: total time to establish the TCP connection to the server.
947 It's the time elapsed between the moment the proxy sent the connection
948 request, and the moment it was acknowledged, or between the TCP SYN packet
949 and the matching SYN/ACK in return. The value '-1' means that the
950 connection never established.
951
952 - Tr: server response time. It's the time elapsed between the moment the
953 TCP connection was established to the server and the moment it send its
954 complete response header. It purely shows its request processing time,
955 without the network overhead due to the data transmission. The value '-1'
956 means that the last the response header (empty line) was never seen.
957
958 - Tt: total session duration time, between the moment the proxy accepted it
959 and the moment both ends were closed. From this one, we can deduce Td,
960 the data transmission time, by substracting other timers when valid :
961
962 Td = Tt - (Tq + Tc + Tr)
963
964 Timers with '-1' values have to be excluded from this equation.
965
966In TCP mode ('option tcplog'), only Tc and Tt are reported.
967
968These timers provide precious indications on trouble causes. Since the TCP
969protocol defines retransmit delays of 3, 6, 12... seconds, we know for sure
970that timers close to multiples of 3s are nearly always related to packets lost
971due to network problems (wires or negociation). Moreover, if <Tt> is close to
972a timeout value specified in the configuration, it often means that a session
973has been aborted on time-out.
974
975Most common cases :
976
977 - If Tq is close to 3000, a packet has probably been lost between the client
978 and the proxy.
979 - If Tc is close to 3000, a packet has probably been lost between the server
980 and the proxy during the server connection phase. This one should always be
981 very low (less than a few tens).
982 - If Tr is nearly always lower than 3000 except some rare values which seem to
983 be the average majored by 3000, there are probably some packets lost between
984 the proxy and the server.
985 - If Tt is often slightly higher than a time-out, it's often because the
986 client and the server use HTTP keep-alive and the session is maintained
987 after the response ends. Se further for how to disable HTTP keep-alive.
988
989Other cases ('xx' means any value to be ignored) :
990 -1/xx/xx/Tt : the client was not able to send its complete request in time,
991 or that it aborted it too early.
992 Tq/-1/xx/Tt : the connection could not establish on the server. Either it
993 refused it or it timed out after Tt-Tq ms.
994 Tq/Tc/-1/Tt : the server has accepted the connection but did not return a
995 complete response in time, or it closed its connexion
996 unexpectedly, after Tt-(Tq+Tc) ms.
997
9984.2.4) Session state at disconnection
999-------------------------------------
1000TCP and HTTP logs provide a session completion indicator. It's a 4-characters
1001(2 in TCP) field preceeding the HTTP request, and indicating :
willy tarreau197e8ec2005-12-17 14:10:59 +01001002 - On the first character, a code reporting the first event which caused the
1003 session to terminate :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001004
willy tarreau197e8ec2005-12-17 14:10:59 +01001005 C : the TCP session was aborted by the client.
1006 S : the TCP session was aborted by the server, or the server refused it.
1007 P : the session was abordted prematurely by the proxy, either because of
1008 an internal error, or because a DENY filter was matched.
1009 c : the client time-out expired first.
1010 s : the server time-out expired first.
1011 - : normal session completion.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001012
willy tarreau197e8ec2005-12-17 14:10:59 +01001013 - on the second character, the HTTP session state when it was closed :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001014
willy tarreau197e8ec2005-12-17 14:10:59 +01001015 R : waiting for complete REQUEST from the client
1016 C : waiting for CONNECTION to establish on the server
1017 H : waiting for complete HEADERS from the server
1018 D : the session was in the DATA phase
1019 L : the proxy was still transmitting LAST data to the client while the
1020 server had already finished.
1021 - : normal session completion after end of data transfer.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001022
willy tarreau197e8ec2005-12-17 14:10:59 +01001023 - the third character tells whether the persistence cookie was provided by
willy tarreauc1cae632005-12-17 14:12:23 +01001024 the client (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001025
willy tarreau197e8ec2005-12-17 14:10:59 +01001026 N : the client provided NO cookie.
1027 I : the client provided an INVALID cookie matching no known server.
1028 D : the client provided a cookie designating a server which was DOWN,
1029 so either the 'persist' option was used and the client was sent to
1030 this server, or it was not set and the client was redispatched to
1031 another server.
1032 V : the client provided a valid cookie, and was sent to the associated
1033 server.
1034 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001035
willy tarreau197e8ec2005-12-17 14:10:59 +01001036 - the last character reports what operations were performed on the persistence
willy tarreauc1cae632005-12-17 14:12:23 +01001037 cookie returned by the server (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001038
willy tarreau197e8ec2005-12-17 14:10:59 +01001039 N : NO cookie was provided by the server.
1040 P : a cookie was PROVIDED by the server and transmitted as-is.
1041 I : no cookie was provided by the server, and one was INSERTED by the
willy tarreaueedaa9f2005-12-17 14:08:03 +01001042 proxy.
willy tarreau197e8ec2005-12-17 14:10:59 +01001043 D : the cookie provided by the server was DELETED by the proxy.
1044 R : the cookie provided by the server was REWRITTEN by the proxy.
1045 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001046
willy tarreau197e8ec2005-12-17 14:10:59 +01001047The 'capture' keyword allows to capture and log informations exchanged between
1048clients and servers. As of version 1.1.23, only cookies can be captured, which
1049makes it easy to track a complete user session. The syntax is :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001050
willy tarreau197e8ec2005-12-17 14:10:59 +01001051 capture cookie <cookie_prefix> len <capture_length>
willy tarreaueedaa9f2005-12-17 14:08:03 +01001052
willy tarreau197e8ec2005-12-17 14:10:59 +01001053The FIRST cookie whose name starts with <cookie_prefix> will be captured, and
1054logged as 'NAME=value', without exceeding <capture_length> characters (64 max).
1055When the cookie name is fixed and known, it's preferable to suffix '=' to it to
1056ensure that no other cookie will be logged.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001057
willy tarreau197e8ec2005-12-17 14:10:59 +01001058Examples :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001059----------
willy tarreau197e8ec2005-12-17 14:10:59 +01001060 # capture the first cookie whose name starts with "ASPSESSION"
willy tarreaueedaa9f2005-12-17 14:08:03 +01001061 capture cookie ASPSESSION len 32
1062
willy tarreau197e8ec2005-12-17 14:10:59 +01001063 # capture the first cookie whose name is exactly "vgnvisitor"
willy tarreaueedaa9f2005-12-17 14:08:03 +01001064 capture cookie vgnvisitor= len 32
1065
willy tarreau197e8ec2005-12-17 14:10:59 +01001066In the logs, the field preceeding the completion indicator contains the cookie
1067value as sent by the server, preceeded by the cookie value as sent by the
1068client. Each of these field is replaced with '-' when no cookie was seen.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001069
willy tarreauc1cae632005-12-17 14:12:23 +010010704.2.5) Examples of logs
1071-----------------------
1072- 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"
1073 => long request (6.5s) entered by hand through 'telnet'. The server replied
1074 in 147 ms, and the session ended normally ('----')
willy tarreaueedaa9f2005-12-17 14:08:03 +01001075
willy tarreauc1cae632005-12-17 14:12:23 +01001076- haproxy[18113]: 127.0.0.1:34548 [15/Oct/2003:15:18:55] relais-http <NOSRV> -1/-1/-1/8490 -1 0 - - CR-- ""
1077 => the client never completed its request and aborted itself ('C---') after
1078 8.5s, while the proxy was waiting for the request headers ('-R--').
1079 Nothing was sent to the server.
1080
1081- haproxy[18113]: 127.0.0.1:34549 [15/Oct/2003:15:19:06] relais-http <NOSRV> -1/-1/-1/50001 408 0 - - cR-- ""
1082 => The client never completed its request, which was aborted by the time-out
1083 ('c---') after 50s, while the proxy was waiting for the request headers ('-R--').
1084 Nothing was sent to the server, but the proxy could send a 408 return code
1085 to the client.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001086
willy tarreauc1cae632005-12-17 14:12:23 +01001087- haproxy[18989]: 127.0.0.1:34550 [15/Oct/2003:15:24:28] relais-tcp Srv1 0/5007 0 cD
1088 => This is a 'tcplog' entry. Client-side time-out ('c----') occured after 5s.
1089
1090- 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"
1091 => The request took 3s to complete (probably a network problem), and the
1092 connection to the server failed ('SC--') after 4 attemps of 2 seconds
1093 (config says 'retries 3'), then a 503 error code was sent to the client.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001094
willy tarreau197e8ec2005-12-17 14:10:59 +010010954.3) HTTP header manipulation
1096-----------------------------
1097In HTTP mode, it is possible to rewrite, add or delete some of the request and
1098response headers based on regular expressions. It is also possible to block a
1099request or a response if a particular header matches a regular expression,
1100which is enough to stops most elementary protocol attacks, and to protect
1101against information leak from the internal network. But there is a limitation
1102to this : since haproxy's HTTP engine knows nothing about keep-alive, only
1103headers passed during the first request of a TCP session will be seen. All
1104subsequent headers will be considered data only and not analyzed. Furthermore,
1105haproxy doesn't touch data contents, it stops at the end of headers.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001106
willy tarreau197e8ec2005-12-17 14:10:59 +01001107The syntax is :
1108 reqadd <string> to add a header to the request
1109 reqrep <search> <replace> to modify the request
1110 reqirep <search> <replace> same, but ignoring the case
1111 reqdel <search> to delete a header in the request
1112 reqidel <search> same, but ignoring the case
1113 reqallow <search> definitely allow a request if a header matches <search>
1114 reqiallow <search> same, but ignoring the case
1115 reqdeny <search> denies a request if a header matches <search>
1116 reqideny <search> same, but ignoring the case
1117 reqpass <search> ignore a header matching <search>
1118 reqipass <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01001119
willy tarreau197e8ec2005-12-17 14:10:59 +01001120 rspadd <string> to add a header to the response
1121 rsprep <search> <replace> to modify the response
1122 rspirep <search> <replace> same, but ignoring the case
1123 rspdel <search> to delete the response
1124 rspidel <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01001125
1126
willy tarreau197e8ec2005-12-17 14:10:59 +01001127<search> is a POSIX regular expression (regex) which supports grouping through
1128parenthesis (without the backslash). Spaces and other delimiters must be
1129prefixed with a backslash ('\') to avoid confusion with a field delimiter.
1130Other characters may be prefixed with a backslash to change their meaning :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001131
willy tarreau197e8ec2005-12-17 14:10:59 +01001132 \t for a tab
1133 \r for a carriage return (CR)
1134 \n for a new line (LF)
1135 \ to mark a space and differentiate it from a delimiter
1136 \# to mark a sharp and differentiate it from a comment
1137 \\ to use a backslash in a regex
1138 \\\\ to use a backslash in the text (*2 for regex, *2 for haproxy)
1139 \xXX to write the ASCII hex code XX as in the C language
willy tarreaueedaa9f2005-12-17 14:08:03 +01001140
1141
willy tarreau197e8ec2005-12-17 14:10:59 +01001142<replace> containst the string to be used to replace the largest portion of text
1143matching the regex. It can make use of the special characters above, and can
1144reference a substring delimited by parenthesis in the regex, by the group
1145numerical order from 1 to 9. In this case, you would write a backslah ('\')
1146immediately followed by one digit indicating the group position.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001147
willy tarreau197e8ec2005-12-17 14:10:59 +01001148<string> represents the string which will systematically be added after the last
1149header line. It can also use special characters above.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001150
willy tarreau197e8ec2005-12-17 14:10:59 +01001151Notes :
1152-------
1153 - the first line is considered as a header, which makes it possible to rewrite
1154 or filter HTTP requests URIs or response codes.
1155 - 'reqrep' is the equivalent of 'cliexp' in version 1.0, and 'rsprep' is the
1156 equivalent of 'srvexp' in 1.0. Those names are still supported but
1157 deprecated.
1158 - for performances reasons, the number of characters added to a request or to
1159 a response is limited to 4096 since version 1.1.5 (it was 256 before). This
1160 value is easy to modify in the code if needed (#define). If it is too short
1161 on occasional uses, it is possible to gain some space by removing some
1162 useless headers before adding new ones.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001163
willy tarreau197e8ec2005-12-17 14:10:59 +01001164Examples :
1165----------
1166 ###### a few examples ######
1167
1168 # rewrite 'online.fr' instead of 'free.fr' for GET and POST requests
1169 reqrep ^(GET\ .*)(.free.fr)(.*) \1.online.fr\3
1170 reqrep ^(POST\ .*)(.free.fr)(.*) \1.online.fr\3
1171
1172 # force proxy connections to close
willy tarreaueedaa9f2005-12-17 14:08:03 +01001173 reqirep ^Proxy-Connection:.* Proxy-Connection:\ close
willy tarreau197e8ec2005-12-17 14:10:59 +01001174 # rewrite locations
willy tarreaueedaa9f2005-12-17 14:08:03 +01001175 rspirep ^(Location:\ )([^:]*://[^/]*)(.*) \1\3
willy tarreaueedaa9f2005-12-17 14:08:03 +01001176
willy tarreau197e8ec2005-12-17 14:10:59 +01001177 ###### A full configuration being used on production ######
willy tarreaueedaa9f2005-12-17 14:08:03 +01001178
willy tarreau197e8ec2005-12-17 14:10:59 +01001179 # Every header should end with a colon followed by one space.
1180 reqideny ^[^:\ ]*[\ ]*$
willy tarreaueedaa9f2005-12-17 14:08:03 +01001181
willy tarreau197e8ec2005-12-17 14:10:59 +01001182 # block Apache chunk exploit
1183 reqideny ^Transfer-Encoding:[\ ]*chunked
1184 reqideny ^Host:\ apache-
willy tarreaueedaa9f2005-12-17 14:08:03 +01001185
willy tarreau197e8ec2005-12-17 14:10:59 +01001186 # block annoying worms that fill the logs...
1187 reqideny ^[^:\ ]*\ .*(\.|%2e)(\.|%2e)(%2f|%5c|/|\\\\)
1188 reqideny ^[^:\ ]*\ ([^\ ]*\ [^\ ]*\ |.*%00)
1189 reqideny ^[^:\ ]*\ .*<script
1190 reqideny ^[^:\ ]*\ .*/(root\.exe\?|cmd\.exe\?|default\.ida\?)
1191
1192 # allow other syntactically valid requests, and block any other method
1193 reqipass ^(GET|POST|HEAD|OPTIONS)\ /.*\ HTTP/1\.[01]$
1194 reqipass ^OPTIONS\ \\*\ HTTP/1\.[01]$
1195 reqideny ^[^:\ ]*\
1196
1197 # force connection:close, thus disabling HTTP keep-alive
1198 reqidel ^Connection:
1199 rspidel ^Connection:
1200 reqadd Connection:\ close
1201 rspadd Connection:\ close
1202
1203 # change the server name
1204 rspidel ^Server:\
1205 rspadd Server:\ Formilux/0.1.8
1206
1207
willy tarreauc1cae632005-12-17 14:12:23 +01001208Last, the 'forwardfor' option creates an HTTP 'X-Forwarded-For' header which
1209contains the client's IP address. This is useful to let the final web server
1210know what the client address was (eg for statistics on domains).
1211
1212Example :
1213---------
1214 listen http_proxy 0.0.0.0:80
1215 mode http
1216 log global
1217 option httplog
1218 option dontlognull
1219 option forwardfor
willy tarreau197e8ec2005-12-17 14:10:59 +01001220
12214.4) Load balancing with persistence
1222------------------------------------
1223
1224Combining cookie insertion with internal load balancing allows to transparently
1225bring persistence to applications. The principle is quite simple :
1226 - assign a cookie value to each server
1227 - enable the load balancing between servers
1228 - insert a cookie into responses resulting from the balancing algorithm
1229 (indirect accesses), end ensure that no upstream proxy will cache it.
1230 - remove the cookie in the request headers so that the application never sees
1231 it.
1232
1233Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001234---------
1235 listen application 0.0.0.0:80
1236 mode http
1237 cookie SERVERID insert nocache indirect
1238 balance roundrobin
1239 server 192.168.1.1:80 cookie server01 check
1240 server 192.168.1.2:80 cookie server02 check
1241
willy tarreau197e8ec2005-12-17 14:10:59 +010012424.5) Customizing errors
1243-----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +01001244
willy tarreau197e8ec2005-12-17 14:10:59 +01001245Some situations can make haproxy return an HTTP error code to the client :
1246 - invalid or too long request => HTTP 400
1247 - request not completely sent in time => HTTP 408
1248 - forbidden request (matches a deny filter) => HTTP 403
1249 - internal error in haproxy => HTTP 500
1250 - the server returned an invalid or incomplete response => HTTP 502
1251 - no server was available to handle the request => HTTP 503
1252 - the server failed to reply in time => HTTP 504
willy tarreaueedaa9f2005-12-17 14:08:03 +01001253
willy tarreau197e8ec2005-12-17 14:10:59 +01001254A succint error message taken from the RFC accompanies these return codes.
1255But depending on the clients knowledge, it may be better to return custom, user
1256friendly, error pages. This is made possible through the use of the 'errorloc'
1257command :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001258
willy tarreau197e8ec2005-12-17 14:10:59 +01001259 errorloc <HTTP_code> <location>
willy tarreaueedaa9f2005-12-17 14:08:03 +01001260
willy tarreau197e8ec2005-12-17 14:10:59 +01001261Instead of generating an HTTP error <HTTP_code> among those above, the proxy
1262will return a temporary redirection code (HTTP 302) towards the address
1263specified in <location>. This address may be either relative to the site or
1264absolute. Since this request will be handled by the client's browser, it's
1265mandatory that the returned address be reachable from the outside.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001266
willy tarreau197e8ec2005-12-17 14:10:59 +01001267Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001268---------
1269 listen application 0.0.0.0:80
1270 errorloc 400 /badrequest.html
1271 errorloc 403 /forbidden.html
1272 errorloc 408 /toolong.html
1273 errorloc 500 http://haproxy.domain.net/bugreport.html
1274 errorloc 502 http://192.168.114.58/error50x.html
1275 errorloc 503 http://192.168.114.58/error50x.html
1276 errorloc 504 http://192.168.114.58/error50x.html
1277
willy tarreau197e8ec2005-12-17 14:10:59 +010012784.6) Modifying default values
1279-----------------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +01001280
willy tarreau197e8ec2005-12-17 14:10:59 +01001281Version 1.1.22 introduced the notion of default values, which eliminates the
1282pain of often repeating common parameters between many instances, such as
1283logs, timeouts, modes, etc...
willy tarreaueedaa9f2005-12-17 14:08:03 +01001284
willy tarreau197e8ec2005-12-17 14:10:59 +01001285Default values are set in a 'defaults' section. Each of these section clears
1286all previously set default parameters, so there may be as many default
1287parameters as needed. Only the last one before a 'listen' section will be
1288used for this section. The 'defaults' section uses the same syntax as the
1289'listen' section, for the supported parameters. The 'defaults' keyword ignores
1290everything on its command line, so that fake instance names can be specified
1291there for better clarity.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001292
willy tarreau197e8ec2005-12-17 14:10:59 +01001293In version 1.1.23, only those parameters can be preset in the 'default'
1294section :
1295 - log (the first and second one)
willy tarreaueedaa9f2005-12-17 14:08:03 +01001296 - mode { tcp, http, health }
1297 - balance { roundrobin }
willy tarreau197e8ec2005-12-17 14:10:59 +01001298 - disabled (to disable every further instances)
1299 - enabled (to enable every further instances, this is the default)
willy tarreaueedaa9f2005-12-17 14:08:03 +01001300 - contimeout, clitimeout, srvtimeout, grace, retries, maxconn
1301 - option { redispatch, transparent, keepalive, forwardfor, httplog,
1302 dontlognull, persist, httpchk }
1303 - redispatch, redisp, transparent, source { addr:port }
1304 - cookie, capture
1305 - errorloc
1306
willy tarreau197e8ec2005-12-17 14:10:59 +01001307As of 1.1.24, it is not possible to put certain parameters in a 'defaults'
1308section, mainly regular expressions and server configurations :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001309 - dispatch, server,
willy tarreau197e8ec2005-12-17 14:10:59 +01001310 - req*, rsp*
willy tarreaueedaa9f2005-12-17 14:08:03 +01001311
willy tarreau197e8ec2005-12-17 14:10:59 +01001312Last, there's no way yet to change a boolean option from its assigned default
1313value. So if an 'option' statement is set in a 'defaults' section, the only
1314way to flush it is to redefine a new 'defaults' section without this 'option'.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001315
willy tarreau197e8ec2005-12-17 14:10:59 +01001316Examples :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001317----------
1318 defaults applications TCP
1319 log global
1320 mode tcp
1321 balance roundrobin
1322 clitimeout 180000
1323 srvtimeout 180000
1324 contimeout 4000
1325 retries 3
1326 redispatch
1327
1328 listen app_tcp1 10.0.0.1:6000-6063
1329 server srv1 192.168.1.1 check port 6000 inter 10000
1330 server srv2 192.168.1.2 backup
1331
1332 listen app_tcp2 10.0.0.2:6000-6063
1333 server srv1 192.168.2.1 check port 6000 inter 10000
1334 server srv2 192.168.2.2 backup
1335
1336 defaults applications HTTP
1337 log global
1338 mode http
1339 option httplog
1340 option forwardfor
1341 option dontlognull
1342 balance roundrobin
1343 clitimeout 20000
1344 srvtimeout 20000
1345 contimeout 4000
1346 retries 3
1347
1348 listen app_http1 10.0.0.1:80-81
1349 cookie SERVERID postonly insert indirect
1350 capture cookie userid= len 10
1351 server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000
1352 server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000
1353
1354 defaults
willy tarreau197e8ec2005-12-17 14:10:59 +01001355 # this empty section voids all default parameters
willy tarreaueedaa9f2005-12-17 14:08:03 +01001356
willy tarreau197e8ec2005-12-17 14:10:59 +01001357=========================
1358| System-specific setup |
1359=========================
willy tarreaueedaa9f2005-12-17 14:08:03 +01001360
willy tarreau197e8ec2005-12-17 14:10:59 +01001361Linux 2.4
1362=========
willy tarreaueedaa9f2005-12-17 14:08:03 +01001363
1364-- cut here --
1365#!/bin/sh
1366# set this to about 256/4M (16384 for 256M machine)
1367MAXFILES=16384
1368echo $MAXFILES > /proc/sys/fs/file-max
1369ulimit -n $MAXFILES
1370
1371if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then
1372 echo 65536 > /proc/sys/net/ipv4/ip_conntrack_max
1373fi
1374
1375if [ -e /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait ]; then
1376 # 30 seconds for fin, 15 for time wait
1377 echo 3000 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait
1378 echo 1500 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_time_wait
1379 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_invalid_scale
1380 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_out_of_window
1381fi
1382
1383echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range
1384echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
1385echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog
1386echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets
1387echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans
1388echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
1389echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
1390echo 0 > /proc/sys/net/ipv4/tcp_timestamps
1391echo 0 > /proc/sys/net/ipv4/tcp_ecn
1392echo 0 > /proc/sys/net/ipv4/tcp_sack
1393echo 0 > /proc/sys/net/ipv4/tcp_dsack
1394
1395# auto-tuned on 2.4
1396#echo 262143 > /proc/sys/net/core/rmem_max
1397#echo 262143 > /proc/sys/net/core/rmem_default
1398
1399echo 16384 65536 524288 > /proc/sys/net/ipv4/tcp_rmem
1400echo 16384 349520 699040 > /proc/sys/net/ipv4/tcp_wmem
1401
1402-- cut here --
1403
willy tarreau197e8ec2005-12-17 14:10:59 +01001404
1405FreeBSD
1406=======
1407
1408A FreeBSD port of HA-Proxy is now available and maintained, thanks to
1409Clement Laforet <sheepkiller@cultdeadsheep.org>.
1410
1411For more information :
1412http://www.freebsd.org/cgi/url.cgi?ports/net/haproxy/pkg-descr
1413http://www.freebsd.org/cgi/cvsweb.cgi/ports/net/haproxy/
1414http://www.freshports.org/net/haproxy
1415
1416
1417-- end --