blob: 7d678d2e1b47e2b22dff2ae243d397dd035904dc [file] [log] [blame]
willy tarreaueedaa9f2005-12-17 14:08:03 +01001
2 H A - P r o x y
3 ---------------
4 version 1.1.23
5 willy tarreau
6 2003/09/20
7
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
38 -s shows statistics (only if compiled in)
39 -l shows even more statistics (implies '-s')
40
41
42The maximal number of connections per proxy is used as the default parameter for
43each instance for which the 'maxconn' paramter is not set in the 'listen' section.
44
45The maximal number of total connections limits the number of connections used by
46the whole process if the 'maxconn' parameter is not set in the 'global' section.
47
48The debugging mode has the same effect as the 'debug' option in the 'global'
49section. When the proxy runs in this mode, it dumps every connections,
50disconnections, timestamps, and HTTP headers to stdout. This should NEVER
51be used in an init script since it will prevent the system from starting up.
52
53Statistics are only available if compiled in with the 'STATTIME' option. It's
54only used during code optimization phases.
55
56======================
57| Configuration file |
58======================
59
60Structure
61=========
62
63The configuration file parser ignores empty lines, spaces, tabs. Anything
64between a sharp ('#') not following a backslash ('\'), and the end of a line
65constitutes a comment and is ignored too.
66
67The configuration file is segmented in sections. A section begins whenever
68one of these 3 keywords are encountered :
69
70 - 'global'
71 - 'listen'
72 - 'defaults'
73
74Every parameter refer to the section beginning at the last one of these 3
75keywords.
76
77
781) Global parameters
79====================
80
81Global parameters affect the whole process behaviour. They are all set in the
82'global' section. There may be several 'global' sections if needed, but their
83parameters will only be merged. Allowed parameters in 'global' section include
84the following ones :
85
86 - log <address> <facility> [max_level]
87 - maxconn <number>
88 - uid <user id>
89 - gid <group id>
90 - chroot <directory>
91 - nbproc <number>
92 - daemon
93 - debug
94 - quiet
95
961.1) Event logging
97------------------
98Most events are logged : start, stop, servers going up and down, connections and
99errors. Each event generates a syslog message which can be sent to up to 2
100servers. The syntax is :
101
102 log <ip_address> <facility> [max_level]
103
104Connections are logged at level "info". Services initialization and servers
105going up are logged at level "notice", termination signals are logged at
106"warning", and definitive service termination, as well as loss of servers are
107logged at level "alert". The optional parameter <max_level> specifies above
108what level messages should be sent. Level can take one of these 8 values :
109
110 emerg, alert, crit, err, warning, notice, info, debug
111
112For backwards compatibility with versions 1.1.16 and earlier, the default level
113value is "debug" if not specified.
114
115Permitted facilities are :
116 kern, user, mail, daemon, auth, syslog, lpr, news,
117 uucp, cron, auth2, ftp, ntp, audit, alert, cron2,
118 local0, local1, local2, local3, local4, local5, local6, local7
119
120According to RFC3164, messages are truncated to 1024 bytes before being emitted.
121
122Example :
123---------
124 global
125 log 192.168.2.200 local3
126 log 127.0.0.1 local4 notice
127
1281.2) limiting the number of connections
129---------------------------------------
130It is possible and recommended to limit the global number of per-process
131connections. Since one connection includes both a client and a server, it
132means that the max number of TCP sessions will be about the double of this
133number. It's important to understand this when trying to find best values
134for 'ulimit -n' before starting the proxy. To anticipate the number of
135sockets needed, all these parameters must be counted :
136
137 - 1 socket per incoming connection
138 - 1 socket per outgoing connection
139 - 1 socket per address/port/proxy tuple.
140 - 1 socket per server being health-checked
141 - 1 socket for all logs
142
143In simple configurations where each proxy only listens one one address/port,
144set the limit of file descriptors (ulimit -n) to
145(2 * maxconn + nbproxies + nbservers + 1). In a future release, haproxy may
146be able to set this value itself.
147
1481.3) Drop of priviledges
149------------------------
150In order to reduce the risk and consequences of attacks, in the event where a
151yet non-identified vulnerability would be successfully exploited, it's possible
152to lower the process priviledges and even isolate it in a riskless directory.
153
154In the 'global' section, the 'uid' parameter sets a numerical user identifier
155which the process will switch to after binding its listening sockets. The value
156'0', which normally represents the super-user, here indicates that the UID must
157not change during startup. It's the default behaviour. The 'gid' parameter does
158the same for the group identifier. It's particularly advised against use of
159generic accounts such as 'nobody' because it has the same consequences as using
160'root' if other services use them.
161
162The 'chroot' parameter makes the process isolate itself in an empty directory
163just before switching its UID. This type of isolation (chroot) can sometimes
164be worked around on certain OS (Linux, Solaris), provided that the attacker
165has gained 'root' priviledges and has the ability to use or create a directory.
166For this reason, it's capital to use a dedicated directory and not to share one
167between several services of different nature. To make isolation more resistant,
168it's recommended to use an empty directory without any right, and to change the
169UID of the process so that it cannot do anything there.
170
171Note: in the event where such a vulnerability would be exploited, it's most
172likely that first attempts would kill the process due to 'Segmentation Fault',
173'Bus Error' or 'Illegal Instruction' signals. Eventhough it's true that
174isolating the server reduces the risks of intrusion, it's sometimes useful to
175find why a process dies, via the analysis of a 'core' file, although very rare
176(the last bug of this sort was fixed in 1.1.9). For security reasons, most
177systems disable the generation of core file when a process changes its UID. So
178the two workarounds are either to start the process from a restricted user
179account, which will not be able to chroot itself, or start it as root and not
180change the UID. In both cases the core will be either in the start or the chroot
181directories. Do not forget to allow core dumps prior to start the process :
182
183# ulimit -c unlimited
184
185Example :
186---------
187
188 global
189 uid 30000
190 gid 30000
191 chroot /var/chroot/haproxy
192
1931.4) Startup modes
194------------------
195The service can start in several different :
196 - foreground / background
197 - quiet / normal / debug
198
199The default mode is normal, foreground, which means that the program doesn't
200return once started. NEVER EVER use this mode in a system startup script, or
201the system won't boot. It needs to be started in background, so that it
202returns immediately after forking. That's accomplished by the 'daemon' option
203in the 'global' section, which is the equivalent of the '-D' command line
204argument.
205
206Moreover, certain alert messages are still sent to the standard output even
207in 'daemon' mode. To make them disappear, simply add the 'quiet' option in the
208'global' section. This option has no command-line equivalent.
209
210Last, the 'debug' mode, enabled with the 'debug' option in the 'global' section,
211and which is equivalent of the '-d' option, allows deep TCP/HTTP analysis, with
212timestamped display of each connection, disconnection, and HTTP headers for both
213ways. This mode is incompatible with 'daemon' and 'quiet' modes for obvious
214reasons.
215
2161.5) Increasing the overall processing power
217--------------------------------------------
218On multi-processor systems, it may seem to be a shame to use only one processor,
219eventhough the load needed to saturate a recent processor are far above common
220usage. Anyway, for very specific needs, the proxy can start several processes
221between which the operating system will spread the incoming connections. The
222number of processes is controlled by the 'nbproc' parameter in the 'global'
223section. It defaults to 1, and obviously works only in 'daemon' mode.
224
225Example :
226---------
227
228 global
229 daemon
230 quiet
231 nbproc 2
232
233
2342) Declaration of a listening service
235=====================================
236
237Service sections start with the 'listen' keyword :
238
239 listen <instance_name> [ <IP_address>:<port_range>[,...] ]
240
241- <instance_name> is the name of the instance. This name will be reported in
242 logs, so it is good to have it reflect the proxied service. No unicity test
243 is done on this name, and it's not mandatory for it to be unique, but highly
244 recommended.
245
246- <IP_address> is the IP address the proxy binds to. Empty address, '*' and
247 '0.0.0.0' all mean that the proxy listens to all valid addresses on the
248 system.
249
250- <port_range> is either a unique port, or a port range for which the proxy will
251 accept connections for the IP address specified above. This range can be :
252 - a numerical port (ex: '80')
253 - a dash-delimited ports range explicitly stating the lower and upper bounds
254 (ex: '2000-2100') which are included in the range.
255
256 Particular care must be taken against port ranges, because every <addr:port>
257 couple consumes one socket (=a file descriptor), so it's easy to eat lots of
258 descriptors with a simple range. The <addr:port> couple must be used only once
259 among all instances running on a same system. Please note that attaching to
260 ports lower than 1024 need particular priviledges to start the program, which
261 are independant of the 'uid' parameter.
262
263- the <IP_address>:<port_range> couple may be repeated indefinitely to require
264 the proxy to listen to other addresses and/or ports. To achieve this, simply
265 separate them with a coma.
266
267Examples :
268---------
269 listen http_proxy :80
270 listen x11_proxy 127.0.0.1:6000-6009
271 listen smtp_proxy 127.0.0.1:25,127.0.0.1:587
272 listen ldap_proxy :389,:663
273
274In the event that all addresses do not fit line width, it's preferable to
275detach secondary addresses on other lines with the 'bind' keyword. If this
276keyword is used, it's not even necessary to specify the first address on the
277'listen' line, which sometimes makes multiple configuration handling easier :
278
279 bind [ <IP_address>:<port_range>[,...] ]
280
281Examples :
282----------
283 listen http_proxy
284 bind :80,:443
285 bind 10.0.0.1:10080,10.0.0.1:10443
286
2872.1) Inhibiting a service
288-------------------------
289A service may be disabled for maintenance reasons, without needing to comment
290out the whole section, simply by specifying the 'disabled' keyword in the
291section to be disabled :
292
293 listen smtp_proxy 0.0.0.0:25
294 disabled
295
296Note: the 'enabled' keyword allows to enable a service which has been disabled
297 previously by a default configuration.
298
2992.2) Modes of operation
300-----------------------
301A service can work in 3 different distinct modes :
302 - TCP
303 - HTTP
304 - monitoring
305
306TCP mode
307--------
308In this mode, the service relays TCP connections as soon as they're established,
309towards one or several servers. No processing is done on the stream. It's only
310an association of source(addr:port) -> destination(addr:port). To use this mode,
311you must specify 'mode tcp' in the 'listen' section. This is the default mode.
312
313Example :
314---------
315 listen smtp_proxy 0.0.0.0:25
316 mode tcp
317
318HTTP mode
319---------
320In this mode, the service relays TCP connections towards one or several servers,
321when it has enough informations to decide, which normally means that all HTTP
322headers have been read. Some of them may be scanned for a cookie or a pattern
323matching a regex. To use this mode, specify 'mode http' in the 'listen' section.
324
325Example :
326---------
327 listen http_proxy 0.0.0.0:80
328 mode http
329
330Health-checking mode
331--------------------
332This mode provides a way for external components to check the proxy's health.
333It is meant to be used with intelligent load-balancers which can use send/expect
334scripts to check for all of their servers' availability. This one simply accepts
335the connection, returns the word 'OK' and closes it. To enable it, simply
336specify 'health' as the working mode :
337
338Example :
339---------
340 listen health_check 0.0.0.0:60000
341 mode health
342
343
3442.3) Limiting the number of simultaneous connections
345----------------------------------------------------
346The 'maxconn' parameter allows a proxy to refuse connections above a certain
347amount of simultaneous ones. When the limit is reached, it simply stops
348listening, but the system may still be accepting them because of the back log
349queue. These connections will be processed further when other ones have freed
350some slots. This provides a serialization effect which helps very fragile
351servers resist to high loads. Se further for system limitations.
352
353Example :
354---------
355 listen tiny_server 0.0.0.0:80
356 maxconn 10
357
358
3592.4) Soft stop
360--------------
361It is possible to stop services without breaking existing connections by the
362sending of the SIG_USR1 signal to the process. All services are then put into
363soft-stop state, which means that they will refuse to accept new connections,
364except for those which have a non-zero value in the 'grace' parameter, in which
365case they will still accept connections for the specified amount of time, in
366milliseconds. This allows to tell a load-balancer that the service is failing,
367while still doing the job during the time it needs to detect it.
368
369Note: active connections are never killed. In the worst case, the user will have
370to wait for all of them to close or to time-out, or simply kill the process
371normally (SIG_TERM). The default 'grace' value is '0'.
372
373Example :
374---------
375 # enter soft stop after 'killall -USR1 haproxy'
376 # the service will still run 10 seconds after the signal
377 listen http_proxy 0.0.0.0:80
378 mode http
379 grace 10000
380
381 # this port is dedicated to a load-balancer, and must fail immediately
382 listen health_check 0.0.0.0:60000
383 mode health
384 grace 0
385
386
3872.5) Connections expiration time
388--------------------------------
389It is possible (and recommended) to configure several time-outs on TCP
390connections. Three independant timers are adjustable with values specified
391in milliseconds. A session will be terminated if either one of these timers
392expire.
393
394 - the time we accept to wait for data from the client, or for the client to
395 accept data : 'clitimeout' :
396
397 # client time-out set to 2mn30.
398 clitimeout 150000
399
400 - the time we accept to wait for data from the server, or for the server to
401 accept data : 'srvtimeout' :
402
403 # server time-out set to 30s.
404 srvtimeout 30000
405
406 - the time we accept to wait for a connection to establish on a server :
407 'contimeout' :
408
409 # we give up if the connection does not complete within 4 seconds
410 contimeout 4000
411
412Notes :
413-------
414 - 'contimeout' and 'srvtimeout' have no sense on 'health' mode servers ;
415 - under high loads, or with a saturated or defective network, it's possible
416 that some packets get lost. Since the first TCP retransmit only happens
417 after 3 seconds, a time-out equal to, or lower than 3 seconds cannot
418 compensate for a packet loss. A 4 seconds time-out seems a reasonable
419 minimum which will considerably reduce connection failures.
420
4212.6) Attempts to reconnect
422--------------------------
423After a connection failure to a server, it is possible to retry, potentially
424on another server. This is useful if health-checks are too rare and you don't
425want the clients to see the failures. The number of attempts to reconnect is
426set by the 'retries' paramter.
427
428Example :
429---------
430 # we can retry 3 times max after a failure
431 retries 3
432
433
4342.7) Address of the dispatch server (deprecated)
435------------------------------------------------
436The server which will be sent all new connections is defined by the 'dispatch'
437parameter, in the form <address>:<port>. It generally is dedicated to unknown
438connections and will assign them a cookie, in case of HTTP persistence mode,
439or simply is a single server in case of generic TCP proxy. This old mode is only
440provided for backwards compatibility, but doesn't allow to check remote servers
441state, and has a rather limited usage. All new setups should switch to 'balance'
442mode. The principle of the dispatcher is to be able to perform the load
443balancing itself, but work only on new clients so that the server doesn't need
444to be a big machine.
445
446Example :
447---------
448 # all new connections go there
449 dispatch 192.168.1.2:80
450
451Note :
452------
453This parameter has no sense for 'health' servers, and is incompatible with
454'balance' mode.
455
456
4572.8) Outgoing source address
458----------------------------
459It is often necessary to bind to a particular address when connecting to some
460remote hosts. This is done via the 'source' parameter which is a per-proxy
461parameter. A newer version may allow to fix different sources to reach different
462servers. The syntax is 'source <address>[:<port>]', where <address> is a valid
463local address (or '0.0.0.0' or '*' or empty to let the system choose), and
464<port> is an optional parameter allowing the user to force the source port for
465very specific needs. If the port is not specified or is '0', the system will
466choose a free port. Note that as of version 1.1.18, the servers health checks
467are also performed from the same source.
468
469Examples :
470----------
471 listen http_proxy *:80
472 # all connections take 192.168.1.200 as source address
473 source 192.168.1.200:0
474
475 listen rlogin_proxy *:513
476 # use address 192.168.1.200 and the reserved port 900 (needs to be root)
477 source 192.168.1.200:900
478
479
4802.9) Setting the cookie name
481----------------------------
482In HTTP mode, it is possible to look for a particular cookie which will contain
483a server identifier which should handle the connection. The cookie name is set
484via the 'cookie' parameter.
485
486Example :
487---------
488 listen http_proxy :80
489 mode http
490 cookie SERVERID
491
492It is possible to change the cookie behaviour to get a smarter persistence,
493depending on applications. It is notably possible to delete or modify a cookie
494emitted by a server, insert a cookie identifying the server in an HTTP response
495and even add a header to tell upstream caches not to cache this response.
496
497Examples :
498----------
499
500To remove the cookie for direct accesses (ie when the server matches the one
501which was specified in the client cookie) :
502
503 cookie SERVERID indirect
504
505To replace the cookie value with the one assigned to the server if any (no
506cookie will be created if the server does not provide one, nor if the
507configuration does not provide one). This lets the application put the cookie
508exactly on certain pages (eg: successful authentication) :
509
510 cookie SERVERID rewrite
511
512To create a new cookie and assign the server identifier to it (in this case, all
513servers should be associated with a valid cookie, since no cookie will simply
514delete the cookie from the client's browser) :
515
516 cookie SERVERID insert
517
518To insert a cookie and ensure that no upstream cache will store it, add the
519'nocache' option :
520
521 cookie SERVERID insert nocache
522
523To insert a cookie only after a POST request, add 'postonly' after 'insert'.
524This has the advantage that there's no risk of caching, and that all pages
525seen before the POST one can still be cached :
526
527 cookie SERVERID insert postonly
528
529Notes :
530-----------
531- it is possible to combine 'insert' with 'indirect' or 'rewrite' to adapt to
532 applications which already generate the cookie with an invalid content.
533
534- in the case where 'insert' and 'indirect' are both specified, the cookie is
535 never transmitted to the server, since it wouldn't understand it. This is
536 the most application-transparent mode.
537
538- it is particularly recommended to use 'nocache' in 'insert' mode if any
539 upstream HTTP/1.0 cache is susceptible to cache the result, because this may
540 lead to many clients going to the same server, or even worse, some clients
541 having their server changed while retrieving a page from the cache.
542
543- when the application is well known and controlled, the best method is to
544 only add the persistence cookie on a POST form because it's up to the
545 application to select which page it wants the upstream servers to cache.
546 In this case, you would use 'insert postonly indirect'.
547
5482.10) Associating a cookie value with a server
549----------------------------------------------
550In HTTP mode, it's possible to associate a cookie value to each server. This
551was initially used in combination with 'dispatch' mode to handle direct accesses
552but it is now the standard way of doing the load balancing. The syntax is :
553
554 server <identifier> <address>:<port> cookie <value>
555
556- <identifier> is any name which can be used to identify the server in the logs.
557- <address>:<port> specifies where the server is bound.
558- <value> is the value to put in or to read from the cookie.
559
560Example : the 'SERVERID' cookie can be either 'server01' or 'server02'
561---------
562 listen http_proxy :80
563 mode http
564 cookie SERVERID
565 dispatch 192.168.1.100:80
566 server web1 192.168.1.1:80 cookie server01
567 server web2 192.168.1.2:80 cookie server02
568
569Warning : the syntax has changed since version 1.0 !
570---------
571
5723) Autonomous load balancer
573===========================
574
575The proxy can perform the load-balancing itself, both in TCP and in HTTP modes.
576This is the most interesting mode which obsoletes the old 'dispatch' mode
577described above. It has advantages such as server health monitoring, multiple
578port binding and port mapping. To use this mode, the 'balance' keyword is used,
579followed by the selected algorithm. As of version 1.1.23, only 'roundrobin' is
580available, which is also the default value if unspecified. In this mode, there
581will be no dispatch address, but the proxy needs at least one server.
582
583Example : same as the last one, with internal load balancer
584---------
585
586 listen http_proxy :80
587 mode http
588 cookie SERVERID
589 balance roundrobin
590 server web1 192.168.1.1:80 cookie server01
591 server web2 192.168.1.2:80 cookie server02
592
593
594Since version 1.1.22, it is possible to automatically determine on which port
595the server will get the connection, depending on the port the client connected
596to. Indeed, there now are 4 possible combinations for the server's <port> field:
597
598 - unspecified or '0' :
599 the connection will be sent to the same port as the one on which the proxy
600 received the client connection itself.
601
602 - numerical value (the only one supported in versions earlier than 1.1.22) :
603 the connection will always be sent to the specified port.
604
605 - '+' followed by a numerical value :
606 the connection will be sent to the same port as the one on which the proxy
607 received the connection, plus this value.
608
609 - '-' followed by a numerical value :
610 the connection will be sent to the same port as the one on which the proxy
611 received the connection, minus this value.
612
613Examples :
614----------
615
616# same as previous example
617
618 listen http_proxy :80
619 mode http
620 cookie SERVERID
621 balance roundrobin
622 server web1 192.168.1.1 cookie server01
623 server web2 192.168.1.2 cookie server02
624
625# simultaneous relaying of ports 80, 81 and 8080-8089
626
627 listen http_proxy :80,:81,:8080-8089
628 mode http
629 cookie SERVERID
630 balance roundrobin
631 server web1 192.168.1.1 cookie server01
632 server web2 192.168.1.2 cookie server02
633
634# relaying of TCP ports 25, 389 and 663 to ports 1025, 1389 and 1663
635
636 listen http_proxy :25,:389,:663
637 mode tcp
638 balance roundrobin
639 server srv1 192.168.1.1:+1000
640 server srv2 192.168.1.2:+1000
641
642
6433.1) Servers monitoring
644-----------------------
645
646It is possible to check the servers status by trying to establish TCP
647connections or even sending HTTP requests to them. A server which fails to
648reply to health checks as expected will not be used by the load balancing
649algorithms. To enable monitoring, add the 'check' keyword on a server line.
650It is possible to specify the interval between tests (in milliseconds) with
651the 'inter' parameter, the number of failures supported before declaring that
652the server has fallen down with the 'fall' parameter, and the number of valid
653checks needed for the server to fully get up with the 'rise' parameter. Since
654version 1.1.22, it is also possible to send checks to a different port
655(mandatory when none is specified) with the 'port' parameter. The default
656values are the following ones :
657
658 - inter : 2000
659 - rise : 2
660 - fall : 3
661 - port : default server port
662
663The default mode consists in establishing TCP connections only. But in certain
664types of application failures, it is often that the server continues to accept
665connections because the system does it itself while the application is running
666an endless loop, or is completely stuck. So in version 1.1.16 were introduced
667HTTP health checks which only performed simple lightweight requests and analysed
668the response. Now, as of version 1.1.23, it is possible to change the HTTP
669method, the URI, and the HTTP version string (which even allows to send headers
670with a dirty trick). To enable HTTP health-checks, use 'option httpchk'.
671
672By default, requests use the 'OPTIONS' method because it's very light and easy
673to filter from logs, and does it on '/'. Only HTTP responses 2xx and 3xx are
674considered valid ones, and only if they come before the time to send a new
675request is reached ('inter' parameter). If some servers block this type of
676request, 3 other forms help to forge a request :
677
678 - option httpchk -> OPTIONS / HTTP/1.0
679 - option httpchk URI -> OPTIONS <URI> HTTP/1.0
680 - option httpchk METH URI -> <METH> <URI> HTTP/1.0
681 - option httpchk METH URI VER -> <METH> <URI> <VER>
682
683See examples below.
684
685Since version 1.1.17, it is possible to specify backup servers. These servers
686are only sollicited when no other server is available. This may only be useful
687to serve a maintenance page, or define one active and one backup server (seldom
688used in TCP mode). To make a server a backup one, simply add the 'backup' option
689on its line. These servers also support cookies, so if a cookie is specified for
690a backup server, clients assigned to this server will stick to it even when the
691other ones come back. Conversely, if no cookie is assigned to such a server,
692the clients will get their cookies removed (empty cookie = removal), and will
693be balanced against other servers once they come back. Please note that there
694is no load-balancing among backup servers. If there are several backup servers,
695the second one will only be used when the first one dies, and so on.
696
697Since version 1.1.17, it is also possible to visually check the status of all
698servers at once. For this, you just have to send a SIGHUP signal to the proxy.
699The servers status will be dumped into the logs at the 'notice' level, as well
700as on <stderr> if not closed. For this reason, it's always a good idea to have
701one local log server at the 'notice' level.
702
703Examples :
704----------
705# same setup as in paragraph 3) with TCP monitoring
706 listen http_proxy 0.0.0.0:80
707 mode http
708 cookie SERVERID
709 balance roundrobin
710 server web1 192.168.1.1:80 cookie server01 check
711 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
712
713# same with HTTP monitoring via 'OPTIONS / HTTP/1.0'
714 listen http_proxy 0.0.0.0:80
715 mode http
716 cookie SERVERID
717 balance roundrobin
718 option httpchk
719 server web1 192.168.1.1:80 cookie server01 check
720 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
721
722# same with HTTP monitoring via 'OPTIONS /index.html HTTP/1.0'
723 listen http_proxy 0.0.0.0:80
724 mode http
725 cookie SERVERID
726 balance roundrobin
727 option httpchk /index.html
728 server web1 192.168.1.1:80 cookie server01 check
729 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
730
731# same with HTTP monitoring via 'HEAD /index.jsp? HTTP/1.1\r\nHost: www'
732 listen http_proxy 0.0.0.0:80
733 mode http
734 cookie SERVERID
735 balance roundrobin
736 option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www
737 server web1 192.168.1.1:80 cookie server01 check
738 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
739
740# automatic insertion of a cookie in the server's response, and automatic
741# deletion of the cookie in the client request, while asking upstream caches
742# not to cache replies.
743 listen web_appl 0.0.0.0:80
744 mode http
745 cookie SERVERID insert nocache indirect
746 balance roundrobin
747 server web1 192.168.1.1:80 cookie server01 check
748 server web2 192.168.1.2:80 cookie server02 check
749
750# same with off-site application backup and local error pages server
751 listen web_appl 0.0.0.0:80
752 mode http
753 cookie SERVERID insert nocache indirect
754 balance roundrobin
755 server web1 192.168.1.1:80 cookie server01 check
756 server web2 192.168.1.2:80 cookie server02 check
757 server web-backup 192.168.2.1:80 cookie server03 check backup
758 server web-excuse 192.168.3.1:80 check backup
759
760# SMTP+TLS relaying with heakth-checks and backup servers
761
762 listen http_proxy :25,:587
763 mode tcp
764 balance roundrobin
765 server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2
766 server srv2 192.168.1.2 backup
767
768
7693.2) Redistribute connections in case of failure
770------------------------------------------------
771In HTTP mode, if a server designated by a cookie does not respond, the clients
772may definitely stick to it because they cannot flush the cookie, so they will
773not be able to access the service anymore. Specifying 'redispatch' will allow
774the proxy to break their persistence and redistribute them to working servers.
775
776Example :
777---------
778 listen http_proxy 0.0.0.0:80
779 mode http
780 cookie SERVERID
781 dispatch 192.168.1.100:80
782 server web1 192.168.1.1:80 cookie server01
783 server web2 192.168.1.2:80 cookie server02
784 redispatch # send back to dispatch in case of connection failure
785
786Up to, and including version 1.1.16, this parameter only applied to connection
787failures. Since version 1.1.17, it also applies to servers which have been
788detected as failed by the health check mechanism. Indeed, a server may be broken
789but still accepting connections, which would not solve every case. But it is
790possible to conserve the old behaviour, that is, make a client insist on trying
791to connect to a server even if it is said to be down, by setting the 'persist'
792option :
793
794 listen http_proxy 0.0.0.0:80
795 mode http
796 option persist
797 cookie SERVERID
798 dispatch 192.168.1.100:80
799 server web1 192.168.1.1:80 cookie server01
800 server web2 192.168.1.2:80 cookie server02
801 redispatch # send back to dispatch in case of connection failure
802
803
8044) Additionnal features
805=======================
806
807Other features are available. They are transparent mode, event logging and
808header rewriting/filtering.
809
8104.1) Transparent mode
811---------------------
812In HTTP mode, the 'transparent' keyword allows to intercept sessions which are
813routed through the system hosting the proxy. This mode was implemented as a
814replacement for the 'dispatch' mode, since connections without cookie will be
815sent to the original address while known cookies will be sent to the servers.
816This mode implies that the system can redirect sessions to a local port.
817
818Example :
819---------
820 listen http_proxy 0.0.0.0:65000
821 mode http
822 transparent
823 cookie SERVERID
824 server server01 192.168.1.1:80
825 server server02 192.168.1.2:80
826
827 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
828 --dport 80 -j REDIRECT --to-ports 65000
829
830Note :
831------
832If the port is left unspecified on the server, the port the client connected to
833will be used. This allows to relay a full port range without using transparent
834mode nor thousands of file descriptors, provided that the system can redirect
835sessions to local ports.
836
837Example :
838---------
839 # redirect all ports to local port 65000, then forward to the server on the
840 # original port.
841 listen http_proxy 0.0.0.0:65000
842 mode tcp
843 server server01 192.168.1.1 check port 60000
844 server server02 192.168.1.2 check port 60000
845
846 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
847 -j REDIRECT --to-ports 65000
848
849
8504.2) Event logging
851------------------
852
853
854- 8< - - - 8< - - - 8< - - - 8< - - - 8< - - - 8< - - -
855
856
857
858Les connexions TCP et HTTP peuvent donner lieu à une journalisation sommaire ou
859détaillée indiquant, pour chaque connexion, la date, l'heure, l'adresse IP
860source, le serveur destination, la durée de la connexion, les temps de réponse,
861la requête HTTP, le code de retour, la quantité de données transmises, et même
862dans certains cas, la valeur d'un cookie permettant de suivre les sessions.
863Tous les messages sont envoyés en syslog vers un ou deux serveurs. Se référer à
864la section 1.1 pour plus d'information sur les catégories de logs. La syntaxe
865est la suivante :
866
867 log <adresse_ip_1> <catégorie_1> [niveau_max_1]
868 log <adresse_ip_2> <catégorie_2> [niveau_max_2]
869ou
870 log global
871
872Remarque :
873----------
874La syntaxe spécifique 'log global' indique que l'on souhaite utiliser les
875paramètres de journalisation définis dans la section 'global'.
876
877Exemple :
878---------
879 listen http_proxy 0.0.0.0:80
880 mode http
881 log 192.168.2.200 local3
882 log 192.168.2.201 local4
883
884Par défaut, les informations contenues dans les logs se situent au niveau TCP
885uniquement. Il faut préciser l'option 'httplog' pour obtenir les détails du
886protocole HTTP. Dans les cas où un mécanisme de surveillance effectuant des
887connexions et déconnexions fréquentes, polluerait les logs, il suffit d'ajouter
888l'option 'dontlognull', pour ne plus obtenir une ligne de log pour les sessions
889n'ayant pas donné lieu à un échange de données (requête ou réponse).
890
891Exemple :
892---------
893 listen http_proxy 0.0.0.0:80
894 mode http
895 option httplog
896 option dontlognull
897 log 192.168.2.200 local3
898
899Depuis la version 1.1.18, un indicateur de complétude de la session a été ajouté
900dans les logs HTTP. C'est un champ de 4 caractères précédant la requête HTTP,
901indiquant :
902 - sur le premier caractère, un code précisant le premier événement qui a causé
903 la terminaison de la session :
904
905 C : fermeture de la session TCP de la part du client
906 S : fermeture de la session TCP de la part du serveur, ou refus de connexion
907 P : terminaison prématurée des sessions par le proxy, pour cas d'erreur
908 interne ou de configuration (ex: filtre d'URL)
909 c : expiration du délai d'attente côté client : clitimeout
910 s : expiration du délai d'attente côté serveur: srvtimeout et contimeout
911 - : terminaison normale.
912
913 - sur le second caractère, l'état d'avancement de la session HTTP lors de la
914 fermeture :
915
916 R : terminaison en attendant la réception totale de la requête du client
917 C : terminaison en attendant la connexion vers le serveur
918 H : terminaison en attendant la réception totale des entêtes du serveur
919 D : terminaison durant le transfert des données du serveur vers le client
920 L : terminaison durant le transfert des dernières données du proxy vers
921 le client, alors que le serveur a déjà fini.
922 - : terminaison normale, après fin de transfert des données
923
924 - le troisième caractère indique l'éventuelle identification d'un cookie de
925 persistence :
926
927 N : aucun cookie de persistence n'a été présenté.
928 I : le client a présenté un cookie ne correspondant à aucun serveur
929 connu.
930 D : le client a présenté un cookie correspondant à un serveur hors
931 d'usage. Suivant l'option 'persist', il a été renvoyé vers un
932 autre serveur ou a tout de même tenté de se connecter sur celui
933 correspondant au cookie.
934 V : le client a présenté un cookie valide et a pu se connecter au
935 serveur correspondant.
936 - : non appliquable
937
938 - le dernier caractère indique l'éventuel traitement effectué sur un cookie de
939 persistence retrourné par le serveur :
940
941 N : aucun cookie de persistence n'a été fourni par le serveur.
942 P : un cookie cookie de persistence n'a été fourni par le serveur.
943 I : aucun cookie n'a été fourni par le serveur, il a été inséré par le
944 proxy.
945 D : le cookie présenté par le serveur a été supprimé par le proxy pour
946 ne pas être retourné au client.
947 R : le cookie retourné par le serveur a été modifié par le proxy.
948 - : non appliquable
949
950Le mot clé "capture" permet d'ajouter dans des logs HTTP des informations
951capturées dans les échanges. La version 1.1.17 supporte uniquement une capture
952de cookies client et serveur, ce qui permet dans bien des cas, de reconstituer
953la session d'un utilisateur. La syntaxe est la suivante :
954
955 capture cookie <préfixe_cookie> len <longueur_capture>
956
957Le premier cookie dont le nom commencera par <préfixe_cookie> sera capturé, et
958transmis sous la forme "NOM=valeur", sans toutefois, excéder <longueur_capture>
959caractères (64 au maximum). Lorsque le nom du cookie est fixe et connu, on peut
960le suffixer du signe "=" pour s'assurer qu'aucun autre cookie ne prendra sa
961place dans les logs.
962
963Exemples :
964----------
965 # capture du premier cookie dont le nom commence par "ASPSESSION"
966 capture cookie ASPSESSION len 32
967
968 # capture du premier cookie dont le nom est exactement "vgnvisitor"
969 capture cookie vgnvisitor= len 32
970
971Dans les logs, le champ précédant l'indicateur de complétude contient le cookie
972positionné par le serveur, précédé du cookie positionné par le client. Chacun de
973ces champs est remplacé par le signe "-" lorsqu'aucun cookie n'est fourni par le
974client ou le serveur.
975
976Enfin, l'option 'forwardfor' ajoute l'adresse IP du client dans un champ
977'X-Forwarded-For' de la requête, ce qui permet à un serveur web final de
978connaître l'adresse IP du client initial.
979
980Exemple :
981---------
982 listen http_proxy 0.0.0.0:80
983 mode http
984 log global
985 option httplog
986 option dontlognull
987 option forwardfor
988 capture cookie userid= len 20
989
990
9914.3) Modification des entêtes HTTP
992----------------------------------
993En mode HTTP uniquement, il est possible de remplacer certains en-têtes dans la
994requête et/ou la réponse à partir d'expressions régulières. Il est également
995possible de bloquer certaines requêtes en fonction du contenu des en-têtes ou de
996la requête. Une limitation cependant : les en-têtes fournis au milieu de
997connexions persistentes (keep-alive) ne sont pas vus car ils sont considérés
998comme faisant partie des échanges de données consécutifs à la première requête.
999Les données ne sont pas affectées, ceci ne s'applique qu'aux en-têtes.
1000
1001La syntaxe est :
1002 reqadd <string> pour ajouter un en-tête dans la requête
1003 reqrep <search> <replace> pour modifier la requête
1004 reqirep <search> <replace> idem sans distinction majuscules/minuscules
1005 reqdel <search> pour supprimer un en-tête dans la requête
1006 reqidel <search> idem sans distinction majuscules/minuscules
1007 reqallow <search> autoriser la requête si un entête valide <search>
1008 reqiallow <search> idem sans distinction majuscules/minuscules
1009 reqdeny <search> interdire la requête si un entête valide <search>
1010 reqideny <search> idem sans distinction majuscules/minuscules
1011 reqpass <search> inhibe ces actions sur les entêtes validant <search>
1012 reqipass <search> idem sans distinction majuscules/minuscules
1013
1014 rspadd <string> pour ajouter un en-tête dans la réponse
1015 rsprep <search> <replace> pour modifier la réponse
1016 rspirep <search> <replace> idem sans distinction majuscules/minuscules
1017 rspdel <search> pour supprimer un en-tête dans la réponse
1018 rspidel <search> idem sans distinction majuscules/minuscules
1019
1020
1021<search> est une expression régulière compatible POSIX regexp supportant le
1022groupage par parenthèses (sans les '\'). Les espaces et autres séparateurs
1023doivent êtres précédés d'un '\' pour ne pas être confondus avec la fin de la
1024chaîne. De plus, certains caractères spéciaux peuvent être précédés d'un
1025backslach ('\') :
1026
1027 \t pour une tabulation
1028 \r pour un retour charriot
1029 \n pour un saut de ligne
1030 \ pour différencier un espace d'un séparateur
1031 \# pour différencier un dièse d'un commentaire
1032 \\ pour utiliser un backslash dans la regex
1033 \\\\ pour utiliser un backslash dans le texte
1034 \xXX pour un caractère spécifique XX (comme en C)
1035
1036
1037<replace> contient la chaîne remplaçant la portion vérifiée par l'expression.
1038Elle peut inclure les caractères spéciaux ci-dessus, faire référence à un
1039groupe délimité par des parenthèses dans l'expression régulière, par sa
1040position numérale. Les positions vont de 1 à 9, et sont codées par un '\'
1041suivi du chiffre désiré. Il est également possible d'insérer un caractère non
1042imprimable (utile pour le saut de ligne) inscrivant '\x' suivi du code
1043hexadécimal de ce caractère (comme en C).
1044
1045<string> représente une chaîne qui sera ajoutée systématiquement après la
1046dernière ligne d'en-tête.
1047
1048Remarques :
1049---------
1050 - la première ligne de la requête et celle de la réponse sont traitées comme
1051 des en-têtes, ce qui permet de réécrire des URL et des codes d'erreur.
1052 - 'reqrep' est l'équivalent de 'cliexp' en version 1.0, et 'rsprep' celui de
1053 'srvexp'. Ces noms sont toujours supportés mais déconseillés.
1054 - pour des raisons de performances, le nombre total de caractères ajoutés sur
1055 une requête ou une réponse est limité à 4096 depuis la version 1.1.5 (cette
1056 limite était à 256 auparavant). Cette valeur est modifiable dans le code.
1057 Pour un usage temporaire, on peut gagner de la place en supprimant quelques
1058 entêtes inutiles avant les ajouts.
1059
1060Exemples :
1061--------
1062 reqrep ^(GET.*)(.free.fr)(.*) \1.online.fr\3
1063 reqrep ^(POST.*)(.free.fr)(.*) \1.online.fr\3
1064 reqirep ^Proxy-Connection:.* Proxy-Connection:\ close
1065 rspirep ^Server:.* Server:\ Tux-2.0
1066 rspirep ^(Location:\ )([^:]*://[^/]*)(.*) \1\3
1067 rspidel ^Connection:
1068 rspadd Connection:\ close
1069
1070
10714.4) Répartition avec persistence
1072---------------------------------
1073
1074La combinaison de l'insertion de cookie avec la répartition de charge interne
1075permet d'assurer une persistence dans les sessions HTTP d'une manière
1076pratiquement transparente pour les applications. Le principe est simple :
1077 - attribuer une valeur d'un cookie à chaque serveur
1078 - effectuer une répartition interne
1079 - insérer un cookie dans les réponses issues d'une répartition uniquement,
1080 et faire en sorte que des caches ne mémorisent pas ce cookie.
1081 - cacher ce cookie à l'application lors des requêtes ultérieures.
1082
1083Exemple :
1084---------
1085 listen application 0.0.0.0:80
1086 mode http
1087 cookie SERVERID insert nocache indirect
1088 balance roundrobin
1089 server 192.168.1.1:80 cookie server01 check
1090 server 192.168.1.2:80 cookie server02 check
1091
10924.5) Personalisation des erreurs
1093--------------------------------
1094
1095Certaines situations conduisent à retourner une erreur HTTP au client :
1096 - requête invalide ou trop longue => code HTTP 400
1097 - requête mettant trop de temps à venir => code HTTP 408
1098 - requête interdite (bloquée par un reqideny) => code HTTP 403
1099 - erreur interne du proxy => code HTTP 500
1100 - le serveur a retourné une réponse incomplète ou invalide => code HTTP 502
1101 - aucun serveur disponible pour cette requête => code HTTP 503
1102 - le serveur n'a pas répondu dans le temps imparti => code HTTP 504
1103
1104Un message d'erreur succint tiré de la RFC accompagne ces codes de retour.
1105Cependant, en fonction du type de clientèle, on peut préférer retourner des
1106pages personnalisées. Ceci est possible par le biais de la commande "errorloc" :
1107
1108 errorloc <code_HTTP> <location>
1109
1110Au lieu de générer une erreur HTTP <code_HTTP> parmi les codes cités ci-dessus,
1111le proxy génèrera un code de redirection temporaire (HTTP 302) vers l'adresse
1112d'une page précisée dans <location>. Cette adresse peut être relative au site,
1113ou absolue. Comme cette réponse est traîtée par le navigateur du client
1114lui-même, il est indispensable que l'adresse fournie lui soit accessible.
1115
1116Exemple :
1117---------
1118 listen application 0.0.0.0:80
1119 errorloc 400 /badrequest.html
1120 errorloc 403 /forbidden.html
1121 errorloc 408 /toolong.html
1122 errorloc 500 http://haproxy.domain.net/bugreport.html
1123 errorloc 502 http://192.168.114.58/error50x.html
1124 errorloc 503 http://192.168.114.58/error50x.html
1125 errorloc 504 http://192.168.114.58/error50x.html
1126
11274.6) Changement des valeurs par défaut
1128--------------------------------------
1129
1130Dans la version 1.1.22 est apparue la notion de valeurs par défaut, ce qui évite
1131de répéter des paramètres communs à toutes les instances, tels que les timeouts,
1132adresses de log, modes de fonctionnement, etc.
1133
1134Les valeurs par défaut sont positionnées dans la dernière section 'defaults'
1135précédent l'instance qui les utilisera. On peut donc mettre autant de sections
1136'defaults' que l'on veut. Il faut juste se rappeler que la présence d'une telle
1137section implique une annulation de tous les paramètres par défaut positionnés
1138précédemment, dans le but de les remplacer.
1139
1140La section 'defaults' utilise la même syntaxe que la section 'listen', aux
1141paramètres près qui ne sont pas supportés. Le mot clé 'defaults' peut accepter
1142un commentaire en guise paramètre.
1143
1144Dans la version 1.1.22, seuls les paramètres suivants peuvent être positionnés
1145dans une section 'defaults' :
1146 - log (le premier et le second)
1147 - mode { tcp, http, health }
1148 - balance { roundrobin }
1149 - disabled (pour désactiver toutes les instances qui suivent)
1150 - enabled (pour faire l'opération inverse, mais c'est le cas par défaut)
1151 - contimeout, clitimeout, srvtimeout, grace, retries, maxconn
1152 - option { redispatch, transparent, keepalive, forwardfor, httplog,
1153 dontlognull, persist, httpchk }
1154 - redispatch, redisp, transparent, source { addr:port }
1155 - cookie, capture
1156 - errorloc
1157
1158Ne sont pas supportés dans cette version, les adresses de dispatch et les
1159configurations de serveurs, ainsi que tous les filtres basés sur les
1160expressions régulières :
1161 - dispatch, server,
1162 - req*, rsp*,
1163
1164Enfin, il n'y a pas le moyen, pour le moment, d'invalider un paramètre booléen
1165positionné par défaut. Donc si une option est spécifiée dans les paramètres par
1166défaut, le seul moyen de la désactiver pour une instance, c'est de changer les
1167paramètres par défaut avant la déclaration de l'instance.
1168
1169Exemples :
1170----------
1171 defaults applications TCP
1172 log global
1173 mode tcp
1174 balance roundrobin
1175 clitimeout 180000
1176 srvtimeout 180000
1177 contimeout 4000
1178 retries 3
1179 redispatch
1180
1181 listen app_tcp1 10.0.0.1:6000-6063
1182 server srv1 192.168.1.1 check port 6000 inter 10000
1183 server srv2 192.168.1.2 backup
1184
1185 listen app_tcp2 10.0.0.2:6000-6063
1186 server srv1 192.168.2.1 check port 6000 inter 10000
1187 server srv2 192.168.2.2 backup
1188
1189 defaults applications HTTP
1190 log global
1191 mode http
1192 option httplog
1193 option forwardfor
1194 option dontlognull
1195 balance roundrobin
1196 clitimeout 20000
1197 srvtimeout 20000
1198 contimeout 4000
1199 retries 3
1200
1201 listen app_http1 10.0.0.1:80-81
1202 cookie SERVERID postonly insert indirect
1203 capture cookie userid= len 10
1204 server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000
1205 server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000
1206
1207 defaults
1208 # section vide qui annule tous les paramètes par défaut.
1209
1210=======================
1211| Paramétrage système |
1212=======================
1213
1214Sous Linux 2.4
1215==============
1216
1217-- cut here --
1218#!/bin/sh
1219# set this to about 256/4M (16384 for 256M machine)
1220MAXFILES=16384
1221echo $MAXFILES > /proc/sys/fs/file-max
1222ulimit -n $MAXFILES
1223
1224if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then
1225 echo 65536 > /proc/sys/net/ipv4/ip_conntrack_max
1226fi
1227
1228if [ -e /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait ]; then
1229 # 30 seconds for fin, 15 for time wait
1230 echo 3000 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait
1231 echo 1500 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_time_wait
1232 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_invalid_scale
1233 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_out_of_window
1234fi
1235
1236echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range
1237echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
1238echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog
1239echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets
1240echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans
1241echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
1242echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
1243echo 0 > /proc/sys/net/ipv4/tcp_timestamps
1244echo 0 > /proc/sys/net/ipv4/tcp_ecn
1245echo 0 > /proc/sys/net/ipv4/tcp_sack
1246echo 0 > /proc/sys/net/ipv4/tcp_dsack
1247
1248# auto-tuned on 2.4
1249#echo 262143 > /proc/sys/net/core/rmem_max
1250#echo 262143 > /proc/sys/net/core/rmem_default
1251
1252echo 16384 65536 524288 > /proc/sys/net/ipv4/tcp_rmem
1253echo 16384 349520 699040 > /proc/sys/net/ipv4/tcp_wmem
1254
1255-- cut here --
1256
1257-- fin --