blob: a4bbf01ca02ed3d9405cb0fb8ba9f43c00bf4c7a [file] [log] [blame]
willy tarreaueedaa9f2005-12-17 14:08:03 +01001
2 H A - P r o x y
3 ---------------
willy tarreauc1cae632005-12-17 14:12:23 +01004 version 1.1.25
willy tarreaueedaa9f2005-12-17 14:08:03 +01005 willy tarreau
willy tarreauc1cae632005-12-17 14:12:23 +01006 2003/10/15
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
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
willy tarreau197e8ec2005-12-17 14:10:59 +0100335the connection, returns the word 'OK' and closes it. If the 'option httpchk' is
336set, then the reply will be 'HTTP/1.0 200 OK' with no data, so that it can be
337tested from a tool which supports HTTP health-checks. To enable it, simply
willy tarreaueedaa9f2005-12-17 14:08:03 +0100338specify 'health' as the working mode :
339
340Example :
341---------
willy tarreau197e8ec2005-12-17 14:10:59 +0100342 # simple response : 'OK'
willy tarreaueedaa9f2005-12-17 14:08:03 +0100343 listen health_check 0.0.0.0:60000
344 mode health
345
willy tarreau197e8ec2005-12-17 14:10:59 +0100346 # HTTP response : 'HTTP/1.0 200 OK'
347 listen http_health_check 0.0.0.0:60001
348 mode health
349 option httpchk
350
willy tarreaueedaa9f2005-12-17 14:08:03 +0100351
3522.3) Limiting the number of simultaneous connections
353----------------------------------------------------
354The 'maxconn' parameter allows a proxy to refuse connections above a certain
355amount of simultaneous ones. When the limit is reached, it simply stops
356listening, but the system may still be accepting them because of the back log
357queue. These connections will be processed further when other ones have freed
358some slots. This provides a serialization effect which helps very fragile
359servers resist to high loads. Se further for system limitations.
360
361Example :
362---------
363 listen tiny_server 0.0.0.0:80
364 maxconn 10
365
366
3672.4) Soft stop
368--------------
369It is possible to stop services without breaking existing connections by the
370sending of the SIG_USR1 signal to the process. All services are then put into
371soft-stop state, which means that they will refuse to accept new connections,
372except for those which have a non-zero value in the 'grace' parameter, in which
373case they will still accept connections for the specified amount of time, in
374milliseconds. This allows to tell a load-balancer that the service is failing,
375while still doing the job during the time it needs to detect it.
376
377Note: active connections are never killed. In the worst case, the user will have
378to wait for all of them to close or to time-out, or simply kill the process
379normally (SIG_TERM). The default 'grace' value is '0'.
380
381Example :
382---------
383 # enter soft stop after 'killall -USR1 haproxy'
384 # the service will still run 10 seconds after the signal
385 listen http_proxy 0.0.0.0:80
386 mode http
387 grace 10000
388
389 # this port is dedicated to a load-balancer, and must fail immediately
390 listen health_check 0.0.0.0:60000
391 mode health
392 grace 0
393
394
3952.5) Connections expiration time
396--------------------------------
397It is possible (and recommended) to configure several time-outs on TCP
398connections. Three independant timers are adjustable with values specified
399in milliseconds. A session will be terminated if either one of these timers
400expire.
401
402 - the time we accept to wait for data from the client, or for the client to
403 accept data : 'clitimeout' :
404
405 # client time-out set to 2mn30.
406 clitimeout 150000
407
408 - the time we accept to wait for data from the server, or for the server to
409 accept data : 'srvtimeout' :
410
411 # server time-out set to 30s.
412 srvtimeout 30000
413
414 - the time we accept to wait for a connection to establish on a server :
415 'contimeout' :
416
417 # we give up if the connection does not complete within 4 seconds
418 contimeout 4000
419
420Notes :
421-------
422 - 'contimeout' and 'srvtimeout' have no sense on 'health' mode servers ;
423 - under high loads, or with a saturated or defective network, it's possible
424 that some packets get lost. Since the first TCP retransmit only happens
425 after 3 seconds, a time-out equal to, or lower than 3 seconds cannot
426 compensate for a packet loss. A 4 seconds time-out seems a reasonable
427 minimum which will considerably reduce connection failures.
428
4292.6) Attempts to reconnect
430--------------------------
431After a connection failure to a server, it is possible to retry, potentially
432on another server. This is useful if health-checks are too rare and you don't
433want the clients to see the failures. The number of attempts to reconnect is
434set by the 'retries' paramter.
435
436Example :
437---------
438 # we can retry 3 times max after a failure
439 retries 3
440
441
4422.7) Address of the dispatch server (deprecated)
443------------------------------------------------
444The server which will be sent all new connections is defined by the 'dispatch'
445parameter, in the form <address>:<port>. It generally is dedicated to unknown
446connections and will assign them a cookie, in case of HTTP persistence mode,
447or simply is a single server in case of generic TCP proxy. This old mode is only
448provided for backwards compatibility, but doesn't allow to check remote servers
449state, and has a rather limited usage. All new setups should switch to 'balance'
450mode. The principle of the dispatcher is to be able to perform the load
451balancing itself, but work only on new clients so that the server doesn't need
452to be a big machine.
453
454Example :
455---------
456 # all new connections go there
457 dispatch 192.168.1.2:80
458
459Note :
460------
461This parameter has no sense for 'health' servers, and is incompatible with
462'balance' mode.
463
464
4652.8) Outgoing source address
466----------------------------
467It is often necessary to bind to a particular address when connecting to some
468remote hosts. This is done via the 'source' parameter which is a per-proxy
469parameter. A newer version may allow to fix different sources to reach different
470servers. The syntax is 'source <address>[:<port>]', where <address> is a valid
471local address (or '0.0.0.0' or '*' or empty to let the system choose), and
472<port> is an optional parameter allowing the user to force the source port for
473very specific needs. If the port is not specified or is '0', the system will
474choose a free port. Note that as of version 1.1.18, the servers health checks
475are also performed from the same source.
476
477Examples :
478----------
479 listen http_proxy *:80
480 # all connections take 192.168.1.200 as source address
481 source 192.168.1.200:0
482
483 listen rlogin_proxy *:513
484 # use address 192.168.1.200 and the reserved port 900 (needs to be root)
485 source 192.168.1.200:900
486
487
4882.9) Setting the cookie name
489----------------------------
490In HTTP mode, it is possible to look for a particular cookie which will contain
491a server identifier which should handle the connection. The cookie name is set
492via the 'cookie' parameter.
493
494Example :
495---------
496 listen http_proxy :80
497 mode http
498 cookie SERVERID
499
500It is possible to change the cookie behaviour to get a smarter persistence,
501depending on applications. It is notably possible to delete or modify a cookie
502emitted by a server, insert a cookie identifying the server in an HTTP response
503and even add a header to tell upstream caches not to cache this response.
504
505Examples :
506----------
507
508To remove the cookie for direct accesses (ie when the server matches the one
509which was specified in the client cookie) :
510
511 cookie SERVERID indirect
512
513To replace the cookie value with the one assigned to the server if any (no
514cookie will be created if the server does not provide one, nor if the
515configuration does not provide one). This lets the application put the cookie
516exactly on certain pages (eg: successful authentication) :
517
518 cookie SERVERID rewrite
519
520To create a new cookie and assign the server identifier to it (in this case, all
521servers should be associated with a valid cookie, since no cookie will simply
522delete the cookie from the client's browser) :
523
524 cookie SERVERID insert
525
526To insert a cookie and ensure that no upstream cache will store it, add the
527'nocache' option :
528
529 cookie SERVERID insert nocache
530
531To insert a cookie only after a POST request, add 'postonly' after 'insert'.
532This has the advantage that there's no risk of caching, and that all pages
533seen before the POST one can still be cached :
534
535 cookie SERVERID insert postonly
536
537Notes :
538-----------
539- it is possible to combine 'insert' with 'indirect' or 'rewrite' to adapt to
540 applications which already generate the cookie with an invalid content.
541
542- in the case where 'insert' and 'indirect' are both specified, the cookie is
543 never transmitted to the server, since it wouldn't understand it. This is
544 the most application-transparent mode.
545
546- it is particularly recommended to use 'nocache' in 'insert' mode if any
547 upstream HTTP/1.0 cache is susceptible to cache the result, because this may
548 lead to many clients going to the same server, or even worse, some clients
549 having their server changed while retrieving a page from the cache.
550
551- when the application is well known and controlled, the best method is to
552 only add the persistence cookie on a POST form because it's up to the
553 application to select which page it wants the upstream servers to cache.
554 In this case, you would use 'insert postonly indirect'.
555
5562.10) Associating a cookie value with a server
557----------------------------------------------
558In HTTP mode, it's possible to associate a cookie value to each server. This
559was initially used in combination with 'dispatch' mode to handle direct accesses
560but it is now the standard way of doing the load balancing. The syntax is :
561
562 server <identifier> <address>:<port> cookie <value>
563
564- <identifier> is any name which can be used to identify the server in the logs.
565- <address>:<port> specifies where the server is bound.
566- <value> is the value to put in or to read from the cookie.
567
568Example : the 'SERVERID' cookie can be either 'server01' or 'server02'
569---------
570 listen http_proxy :80
571 mode http
572 cookie SERVERID
573 dispatch 192.168.1.100:80
574 server web1 192.168.1.1:80 cookie server01
575 server web2 192.168.1.2:80 cookie server02
576
577Warning : the syntax has changed since version 1.0 !
578---------
579
5803) Autonomous load balancer
581===========================
582
583The proxy can perform the load-balancing itself, both in TCP and in HTTP modes.
584This is the most interesting mode which obsoletes the old 'dispatch' mode
585described above. It has advantages such as server health monitoring, multiple
586port binding and port mapping. To use this mode, the 'balance' keyword is used,
587followed by the selected algorithm. As of version 1.1.23, only 'roundrobin' is
588available, which is also the default value if unspecified. In this mode, there
589will be no dispatch address, but the proxy needs at least one server.
590
591Example : same as the last one, with internal load balancer
592---------
593
594 listen http_proxy :80
595 mode http
596 cookie SERVERID
597 balance roundrobin
598 server web1 192.168.1.1:80 cookie server01
599 server web2 192.168.1.2:80 cookie server02
600
601
602Since version 1.1.22, it is possible to automatically determine on which port
603the server will get the connection, depending on the port the client connected
604to. Indeed, there now are 4 possible combinations for the server's <port> field:
605
606 - unspecified or '0' :
607 the connection will be sent to the same port as the one on which the proxy
608 received the client connection itself.
609
610 - numerical value (the only one supported in versions earlier than 1.1.22) :
611 the connection will always be sent to the specified port.
612
613 - '+' followed by a numerical value :
614 the connection will be sent to the same port as the one on which the proxy
615 received the connection, plus this value.
616
617 - '-' followed by a numerical value :
618 the connection will be sent to the same port as the one on which the proxy
619 received the connection, minus this value.
620
621Examples :
622----------
623
624# same as previous example
625
626 listen http_proxy :80
627 mode http
628 cookie SERVERID
629 balance roundrobin
630 server web1 192.168.1.1 cookie server01
631 server web2 192.168.1.2 cookie server02
632
633# simultaneous relaying of ports 80, 81 and 8080-8089
634
635 listen http_proxy :80,:81,:8080-8089
636 mode http
637 cookie SERVERID
638 balance roundrobin
639 server web1 192.168.1.1 cookie server01
640 server web2 192.168.1.2 cookie server02
641
642# relaying of TCP ports 25, 389 and 663 to ports 1025, 1389 and 1663
643
644 listen http_proxy :25,:389,:663
645 mode tcp
646 balance roundrobin
647 server srv1 192.168.1.1:+1000
648 server srv2 192.168.1.2:+1000
649
650
willy tarreau197e8ec2005-12-17 14:10:59 +01006513.1) Server monitoring
652----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +0100653
654It is possible to check the servers status by trying to establish TCP
655connections or even sending HTTP requests to them. A server which fails to
656reply to health checks as expected will not be used by the load balancing
657algorithms. To enable monitoring, add the 'check' keyword on a server line.
658It is possible to specify the interval between tests (in milliseconds) with
659the 'inter' parameter, the number of failures supported before declaring that
660the server has fallen down with the 'fall' parameter, and the number of valid
661checks needed for the server to fully get up with the 'rise' parameter. Since
662version 1.1.22, it is also possible to send checks to a different port
663(mandatory when none is specified) with the 'port' parameter. The default
664values are the following ones :
665
666 - inter : 2000
667 - rise : 2
668 - fall : 3
669 - port : default server port
670
671The default mode consists in establishing TCP connections only. But in certain
672types of application failures, it is often that the server continues to accept
673connections because the system does it itself while the application is running
674an endless loop, or is completely stuck. So in version 1.1.16 were introduced
675HTTP health checks which only performed simple lightweight requests and analysed
676the response. Now, as of version 1.1.23, it is possible to change the HTTP
677method, the URI, and the HTTP version string (which even allows to send headers
678with a dirty trick). To enable HTTP health-checks, use 'option httpchk'.
679
680By default, requests use the 'OPTIONS' method because it's very light and easy
681to filter from logs, and does it on '/'. Only HTTP responses 2xx and 3xx are
682considered valid ones, and only if they come before the time to send a new
683request is reached ('inter' parameter). If some servers block this type of
684request, 3 other forms help to forge a request :
685
686 - option httpchk -> OPTIONS / HTTP/1.0
687 - option httpchk URI -> OPTIONS <URI> HTTP/1.0
688 - option httpchk METH URI -> <METH> <URI> HTTP/1.0
689 - option httpchk METH URI VER -> <METH> <URI> <VER>
690
691See examples below.
692
693Since version 1.1.17, it is possible to specify backup servers. These servers
694are only sollicited when no other server is available. This may only be useful
695to serve a maintenance page, or define one active and one backup server (seldom
696used in TCP mode). To make a server a backup one, simply add the 'backup' option
697on its line. These servers also support cookies, so if a cookie is specified for
698a backup server, clients assigned to this server will stick to it even when the
699other ones come back. Conversely, if no cookie is assigned to such a server,
700the clients will get their cookies removed (empty cookie = removal), and will
701be balanced against other servers once they come back. Please note that there
702is no load-balancing among backup servers. If there are several backup servers,
703the second one will only be used when the first one dies, and so on.
704
705Since version 1.1.17, it is also possible to visually check the status of all
706servers at once. For this, you just have to send a SIGHUP signal to the proxy.
707The servers status will be dumped into the logs at the 'notice' level, as well
708as on <stderr> if not closed. For this reason, it's always a good idea to have
709one local log server at the 'notice' level.
710
711Examples :
712----------
713# same setup as in paragraph 3) with TCP monitoring
714 listen http_proxy 0.0.0.0:80
715 mode http
716 cookie SERVERID
717 balance roundrobin
718 server web1 192.168.1.1:80 cookie server01 check
719 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
720
721# same with HTTP monitoring via 'OPTIONS / HTTP/1.0'
722 listen http_proxy 0.0.0.0:80
723 mode http
724 cookie SERVERID
725 balance roundrobin
726 option httpchk
727 server web1 192.168.1.1:80 cookie server01 check
728 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
729
730# same with HTTP monitoring via 'OPTIONS /index.html HTTP/1.0'
731 listen http_proxy 0.0.0.0:80
732 mode http
733 cookie SERVERID
734 balance roundrobin
735 option httpchk /index.html
736 server web1 192.168.1.1:80 cookie server01 check
737 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
738
739# same with HTTP monitoring via 'HEAD /index.jsp? HTTP/1.1\r\nHost: www'
740 listen http_proxy 0.0.0.0:80
741 mode http
742 cookie SERVERID
743 balance roundrobin
744 option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www
745 server web1 192.168.1.1:80 cookie server01 check
746 server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2
747
748# automatic insertion of a cookie in the server's response, and automatic
749# deletion of the cookie in the client request, while asking upstream caches
750# not to cache replies.
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
758# same with off-site application backup and local error pages server
759 listen web_appl 0.0.0.0:80
760 mode http
761 cookie SERVERID insert nocache indirect
762 balance roundrobin
763 server web1 192.168.1.1:80 cookie server01 check
764 server web2 192.168.1.2:80 cookie server02 check
765 server web-backup 192.168.2.1:80 cookie server03 check backup
766 server web-excuse 192.168.3.1:80 check backup
767
768# SMTP+TLS relaying with heakth-checks and backup servers
769
770 listen http_proxy :25,:587
771 mode tcp
772 balance roundrobin
773 server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2
774 server srv2 192.168.1.2 backup
775
776
7773.2) Redistribute connections in case of failure
778------------------------------------------------
779In HTTP mode, if a server designated by a cookie does not respond, the clients
780may definitely stick to it because they cannot flush the cookie, so they will
781not be able to access the service anymore. Specifying 'redispatch' will allow
782the proxy to break their persistence and redistribute them to working servers.
783
784Example :
785---------
786 listen http_proxy 0.0.0.0:80
787 mode http
788 cookie SERVERID
789 dispatch 192.168.1.100:80
790 server web1 192.168.1.1:80 cookie server01
791 server web2 192.168.1.2:80 cookie server02
792 redispatch # send back to dispatch in case of connection failure
793
794Up to, and including version 1.1.16, this parameter only applied to connection
795failures. Since version 1.1.17, it also applies to servers which have been
796detected as failed by the health check mechanism. Indeed, a server may be broken
797but still accepting connections, which would not solve every case. But it is
798possible to conserve the old behaviour, that is, make a client insist on trying
799to connect to a server even if it is said to be down, by setting the 'persist'
800option :
801
802 listen http_proxy 0.0.0.0:80
803 mode http
804 option persist
805 cookie SERVERID
806 dispatch 192.168.1.100:80
807 server web1 192.168.1.1:80 cookie server01
808 server web2 192.168.1.2:80 cookie server02
809 redispatch # send back to dispatch in case of connection failure
810
811
8124) Additionnal features
813=======================
814
815Other features are available. They are transparent mode, event logging and
816header rewriting/filtering.
817
8184.1) Transparent mode
819---------------------
820In HTTP mode, the 'transparent' keyword allows to intercept sessions which are
821routed through the system hosting the proxy. This mode was implemented as a
822replacement for the 'dispatch' mode, since connections without cookie will be
823sent to the original address while known cookies will be sent to the servers.
824This mode implies that the system can redirect sessions to a local port.
825
826Example :
827---------
828 listen http_proxy 0.0.0.0:65000
829 mode http
830 transparent
831 cookie SERVERID
832 server server01 192.168.1.1:80
833 server server02 192.168.1.2:80
834
835 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
836 --dport 80 -j REDIRECT --to-ports 65000
837
838Note :
839------
840If the port is left unspecified on the server, the port the client connected to
841will be used. This allows to relay a full port range without using transparent
842mode nor thousands of file descriptors, provided that the system can redirect
843sessions to local ports.
844
845Example :
846---------
847 # redirect all ports to local port 65000, then forward to the server on the
848 # original port.
849 listen http_proxy 0.0.0.0:65000
850 mode tcp
851 server server01 192.168.1.1 check port 60000
852 server server02 192.168.1.2 check port 60000
853
854 # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
855 -j REDIRECT --to-ports 65000
856
857
8584.2) Event logging
859------------------
willy tarreauc1cae632005-12-17 14:12:23 +01008604.2.1) Log levels
861-----------------
willy tarreau197e8ec2005-12-17 14:10:59 +0100862TCP and HTTP connections can be logged with informations such as date, time,
863source IP address, destination address, connection duration, response times,
864HTTP request, the HTTP return code, number of bytes transmitted, the conditions
865in which the session ended, and even exchanged cookies values, to track a
866particular user's problems for example. All messages are sent to up to two
867syslog servers. Consult section 1.1 for more info about log facilities. The
868syntax follows :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100869
willy tarreau197e8ec2005-12-17 14:10:59 +0100870 log <address_1> <facility_1> [max_level_1]
871 log <address_2> <facility_2> [max_level_2]
872or
willy tarreaueedaa9f2005-12-17 14:08:03 +0100873 log global
874
willy tarreau197e8ec2005-12-17 14:10:59 +0100875Note :
876------
877The particular syntax 'log global' means that the same log configuration as the
878'global' section will be used.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100879
willy tarreau197e8ec2005-12-17 14:10:59 +0100880Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100881---------
882 listen http_proxy 0.0.0.0:80
883 mode http
884 log 192.168.2.200 local3
885 log 192.168.2.201 local4
886
willy tarreauc1cae632005-12-17 14:12:23 +01008874.2.2) Log format
888-----------------
889By default, connections are logged at the TCP level, as soon as the session
890establishes between the client and the proxy. By enabling the 'tcplog' option,
891the proxy will wait until the session ends to generate an enhanced log
892containing more information such as session duration and its state during the
893disconnection.
894
895Another option, 'httplog', provides more detailed information about HTTP
896contents, such as the request and some cookies. In the event where an external
897component would establish frequent connections to check the service, logs may be
898full of useless lines. So it is possible not to log any session which didn't
899transfer any data, by the setting of the 'dontlognull' option. This only has
900effect on sessions which are established then closed.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100901
willy tarreau197e8ec2005-12-17 14:10:59 +0100902Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100903---------
904 listen http_proxy 0.0.0.0:80
905 mode http
906 option httplog
907 option dontlognull
908 log 192.168.2.200 local3
909
willy tarreauc1cae632005-12-17 14:12:23 +01009104.2.3) Timing events
911--------------------
912Timers provide a great help in trouble shooting network problems. All values
913are reported in milliseconds (ms). In HTTP mode, four control points are
914reported under the form 'Tq/Tc/Tr/Tt' :
915
916 - Tq: total time to get the client request.
917 It's the time elapsed between the moment the client connection was accepted
918 and the moment the proxy received the last HTTP header. The value '-1'
919 indicates that the end of headers (empty line) has never been seen.
920
921 - Tc: total time to establish the TCP connection to the server.
922 It's the time elapsed between the moment the proxy sent the connection
923 request, and the moment it was acknowledged, or between the TCP SYN packet
924 and the matching SYN/ACK in return. The value '-1' means that the
925 connection never established.
926
927 - Tr: server response time. It's the time elapsed between the moment the
928 TCP connection was established to the server and the moment it send its
929 complete response header. It purely shows its request processing time,
930 without the network overhead due to the data transmission. The value '-1'
931 means that the last the response header (empty line) was never seen.
932
933 - Tt: total session duration time, between the moment the proxy accepted it
934 and the moment both ends were closed. From this one, we can deduce Td,
935 the data transmission time, by substracting other timers when valid :
936
937 Td = Tt - (Tq + Tc + Tr)
938
939 Timers with '-1' values have to be excluded from this equation.
940
941In TCP mode ('option tcplog'), only Tc and Tt are reported.
942
943These timers provide precious indications on trouble causes. Since the TCP
944protocol defines retransmit delays of 3, 6, 12... seconds, we know for sure
945that timers close to multiples of 3s are nearly always related to packets lost
946due to network problems (wires or negociation). Moreover, if <Tt> is close to
947a timeout value specified in the configuration, it often means that a session
948has been aborted on time-out.
949
950Most common cases :
951
952 - If Tq is close to 3000, a packet has probably been lost between the client
953 and the proxy.
954 - If Tc is close to 3000, a packet has probably been lost between the server
955 and the proxy during the server connection phase. This one should always be
956 very low (less than a few tens).
957 - If Tr is nearly always lower than 3000 except some rare values which seem to
958 be the average majored by 3000, there are probably some packets lost between
959 the proxy and the server.
960 - If Tt is often slightly higher than a time-out, it's often because the
961 client and the server use HTTP keep-alive and the session is maintained
962 after the response ends. Se further for how to disable HTTP keep-alive.
963
964Other cases ('xx' means any value to be ignored) :
965 -1/xx/xx/Tt : the client was not able to send its complete request in time,
966 or that it aborted it too early.
967 Tq/-1/xx/Tt : the connection could not establish on the server. Either it
968 refused it or it timed out after Tt-Tq ms.
969 Tq/Tc/-1/Tt : the server has accepted the connection but did not return a
970 complete response in time, or it closed its connexion
971 unexpectedly, after Tt-(Tq+Tc) ms.
972
9734.2.4) Session state at disconnection
974-------------------------------------
975TCP and HTTP logs provide a session completion indicator. It's a 4-characters
976(2 in TCP) field preceeding the HTTP request, and indicating :
willy tarreau197e8ec2005-12-17 14:10:59 +0100977 - On the first character, a code reporting the first event which caused the
978 session to terminate :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100979
willy tarreau197e8ec2005-12-17 14:10:59 +0100980 C : the TCP session was aborted by the client.
981 S : the TCP session was aborted by the server, or the server refused it.
982 P : the session was abordted prematurely by the proxy, either because of
983 an internal error, or because a DENY filter was matched.
984 c : the client time-out expired first.
985 s : the server time-out expired first.
986 - : normal session completion.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100987
willy tarreau197e8ec2005-12-17 14:10:59 +0100988 - on the second character, the HTTP session state when it was closed :
willy tarreaueedaa9f2005-12-17 14:08:03 +0100989
willy tarreau197e8ec2005-12-17 14:10:59 +0100990 R : waiting for complete REQUEST from the client
991 C : waiting for CONNECTION to establish on the server
992 H : waiting for complete HEADERS from the server
993 D : the session was in the DATA phase
994 L : the proxy was still transmitting LAST data to the client while the
995 server had already finished.
996 - : normal session completion after end of data transfer.
willy tarreaueedaa9f2005-12-17 14:08:03 +0100997
willy tarreau197e8ec2005-12-17 14:10:59 +0100998 - the third character tells whether the persistence cookie was provided by
willy tarreauc1cae632005-12-17 14:12:23 +0100999 the client (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001000
willy tarreau197e8ec2005-12-17 14:10:59 +01001001 N : the client provided NO cookie.
1002 I : the client provided an INVALID cookie matching no known server.
1003 D : the client provided a cookie designating a server which was DOWN,
1004 so either the 'persist' option was used and the client was sent to
1005 this server, or it was not set and the client was redispatched to
1006 another server.
1007 V : the client provided a valid cookie, and was sent to the associated
1008 server.
1009 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001010
willy tarreau197e8ec2005-12-17 14:10:59 +01001011 - the last character reports what operations were performed on the persistence
willy tarreauc1cae632005-12-17 14:12:23 +01001012 cookie returned by the server (only in HTTP mode) :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001013
willy tarreau197e8ec2005-12-17 14:10:59 +01001014 N : NO cookie was provided by the server.
1015 P : a cookie was PROVIDED by the server and transmitted as-is.
1016 I : no cookie was provided by the server, and one was INSERTED by the
willy tarreaueedaa9f2005-12-17 14:08:03 +01001017 proxy.
willy tarreau197e8ec2005-12-17 14:10:59 +01001018 D : the cookie provided by the server was DELETED by the proxy.
1019 R : the cookie provided by the server was REWRITTEN by the proxy.
1020 - : does not apply (no cookie set in configuration).
willy tarreaueedaa9f2005-12-17 14:08:03 +01001021
willy tarreau197e8ec2005-12-17 14:10:59 +01001022The 'capture' keyword allows to capture and log informations exchanged between
1023clients and servers. As of version 1.1.23, only cookies can be captured, which
1024makes it easy to track a complete user session. The syntax is :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001025
willy tarreau197e8ec2005-12-17 14:10:59 +01001026 capture cookie <cookie_prefix> len <capture_length>
willy tarreaueedaa9f2005-12-17 14:08:03 +01001027
willy tarreau197e8ec2005-12-17 14:10:59 +01001028The FIRST cookie whose name starts with <cookie_prefix> will be captured, and
1029logged as 'NAME=value', without exceeding <capture_length> characters (64 max).
1030When the cookie name is fixed and known, it's preferable to suffix '=' to it to
1031ensure that no other cookie will be logged.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001032
willy tarreau197e8ec2005-12-17 14:10:59 +01001033Examples :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001034----------
willy tarreau197e8ec2005-12-17 14:10:59 +01001035 # capture the first cookie whose name starts with "ASPSESSION"
willy tarreaueedaa9f2005-12-17 14:08:03 +01001036 capture cookie ASPSESSION len 32
1037
willy tarreau197e8ec2005-12-17 14:10:59 +01001038 # capture the first cookie whose name is exactly "vgnvisitor"
willy tarreaueedaa9f2005-12-17 14:08:03 +01001039 capture cookie vgnvisitor= len 32
1040
willy tarreau197e8ec2005-12-17 14:10:59 +01001041In the logs, the field preceeding the completion indicator contains the cookie
1042value as sent by the server, preceeded by the cookie value as sent by the
1043client. Each of these field is replaced with '-' when no cookie was seen.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001044
willy tarreauc1cae632005-12-17 14:12:23 +010010454.2.5) Examples of logs
1046-----------------------
1047- 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"
1048 => long request (6.5s) entered by hand through 'telnet'. The server replied
1049 in 147 ms, and the session ended normally ('----')
willy tarreaueedaa9f2005-12-17 14:08:03 +01001050
willy tarreauc1cae632005-12-17 14:12:23 +01001051- haproxy[18113]: 127.0.0.1:34548 [15/Oct/2003:15:18:55] relais-http <NOSRV> -1/-1/-1/8490 -1 0 - - CR-- ""
1052 => the client never completed its request and aborted itself ('C---') after
1053 8.5s, while the proxy was waiting for the request headers ('-R--').
1054 Nothing was sent to the server.
1055
1056- haproxy[18113]: 127.0.0.1:34549 [15/Oct/2003:15:19:06] relais-http <NOSRV> -1/-1/-1/50001 408 0 - - cR-- ""
1057 => The client never completed its request, which was aborted by the time-out
1058 ('c---') after 50s, while the proxy was waiting for the request headers ('-R--').
1059 Nothing was sent to the server, but the proxy could send a 408 return code
1060 to the client.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001061
willy tarreauc1cae632005-12-17 14:12:23 +01001062- haproxy[18989]: 127.0.0.1:34550 [15/Oct/2003:15:24:28] relais-tcp Srv1 0/5007 0 cD
1063 => This is a 'tcplog' entry. Client-side time-out ('c----') occured after 5s.
1064
1065- 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"
1066 => The request took 3s to complete (probably a network problem), and the
1067 connection to the server failed ('SC--') after 4 attemps of 2 seconds
1068 (config says 'retries 3'), then a 503 error code was sent to the client.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001069
willy tarreau197e8ec2005-12-17 14:10:59 +010010704.3) HTTP header manipulation
1071-----------------------------
1072In HTTP mode, it is possible to rewrite, add or delete some of the request and
1073response headers based on regular expressions. It is also possible to block a
1074request or a response if a particular header matches a regular expression,
1075which is enough to stops most elementary protocol attacks, and to protect
1076against information leak from the internal network. But there is a limitation
1077to this : since haproxy's HTTP engine knows nothing about keep-alive, only
1078headers passed during the first request of a TCP session will be seen. All
1079subsequent headers will be considered data only and not analyzed. Furthermore,
1080haproxy doesn't touch data contents, it stops at the end of headers.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001081
willy tarreau197e8ec2005-12-17 14:10:59 +01001082The syntax is :
1083 reqadd <string> to add a header to the request
1084 reqrep <search> <replace> to modify the request
1085 reqirep <search> <replace> same, but ignoring the case
1086 reqdel <search> to delete a header in the request
1087 reqidel <search> same, but ignoring the case
1088 reqallow <search> definitely allow a request if a header matches <search>
1089 reqiallow <search> same, but ignoring the case
1090 reqdeny <search> denies a request if a header matches <search>
1091 reqideny <search> same, but ignoring the case
1092 reqpass <search> ignore a header matching <search>
1093 reqipass <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01001094
willy tarreau197e8ec2005-12-17 14:10:59 +01001095 rspadd <string> to add a header to the response
1096 rsprep <search> <replace> to modify the response
1097 rspirep <search> <replace> same, but ignoring the case
1098 rspdel <search> to delete the response
1099 rspidel <search> same, but ignoring the case
willy tarreaueedaa9f2005-12-17 14:08:03 +01001100
1101
willy tarreau197e8ec2005-12-17 14:10:59 +01001102<search> is a POSIX regular expression (regex) which supports grouping through
1103parenthesis (without the backslash). Spaces and other delimiters must be
1104prefixed with a backslash ('\') to avoid confusion with a field delimiter.
1105Other characters may be prefixed with a backslash to change their meaning :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001106
willy tarreau197e8ec2005-12-17 14:10:59 +01001107 \t for a tab
1108 \r for a carriage return (CR)
1109 \n for a new line (LF)
1110 \ to mark a space and differentiate it from a delimiter
1111 \# to mark a sharp and differentiate it from a comment
1112 \\ to use a backslash in a regex
1113 \\\\ to use a backslash in the text (*2 for regex, *2 for haproxy)
1114 \xXX to write the ASCII hex code XX as in the C language
willy tarreaueedaa9f2005-12-17 14:08:03 +01001115
1116
willy tarreau197e8ec2005-12-17 14:10:59 +01001117<replace> containst the string to be used to replace the largest portion of text
1118matching the regex. It can make use of the special characters above, and can
1119reference a substring delimited by parenthesis in the regex, by the group
1120numerical order from 1 to 9. In this case, you would write a backslah ('\')
1121immediately followed by one digit indicating the group position.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001122
willy tarreau197e8ec2005-12-17 14:10:59 +01001123<string> represents the string which will systematically be added after the last
1124header line. It can also use special characters above.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001125
willy tarreau197e8ec2005-12-17 14:10:59 +01001126Notes :
1127-------
1128 - the first line is considered as a header, which makes it possible to rewrite
1129 or filter HTTP requests URIs or response codes.
1130 - 'reqrep' is the equivalent of 'cliexp' in version 1.0, and 'rsprep' is the
1131 equivalent of 'srvexp' in 1.0. Those names are still supported but
1132 deprecated.
1133 - for performances reasons, the number of characters added to a request or to
1134 a response is limited to 4096 since version 1.1.5 (it was 256 before). This
1135 value is easy to modify in the code if needed (#define). If it is too short
1136 on occasional uses, it is possible to gain some space by removing some
1137 useless headers before adding new ones.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001138
willy tarreau197e8ec2005-12-17 14:10:59 +01001139Examples :
1140----------
1141 ###### a few examples ######
1142
1143 # rewrite 'online.fr' instead of 'free.fr' for GET and POST requests
1144 reqrep ^(GET\ .*)(.free.fr)(.*) \1.online.fr\3
1145 reqrep ^(POST\ .*)(.free.fr)(.*) \1.online.fr\3
1146
1147 # force proxy connections to close
willy tarreaueedaa9f2005-12-17 14:08:03 +01001148 reqirep ^Proxy-Connection:.* Proxy-Connection:\ close
willy tarreau197e8ec2005-12-17 14:10:59 +01001149 # rewrite locations
willy tarreaueedaa9f2005-12-17 14:08:03 +01001150 rspirep ^(Location:\ )([^:]*://[^/]*)(.*) \1\3
willy tarreaueedaa9f2005-12-17 14:08:03 +01001151
willy tarreau197e8ec2005-12-17 14:10:59 +01001152 ###### A full configuration being used on production ######
willy tarreaueedaa9f2005-12-17 14:08:03 +01001153
willy tarreau197e8ec2005-12-17 14:10:59 +01001154 # Every header should end with a colon followed by one space.
1155 reqideny ^[^:\ ]*[\ ]*$
willy tarreaueedaa9f2005-12-17 14:08:03 +01001156
willy tarreau197e8ec2005-12-17 14:10:59 +01001157 # block Apache chunk exploit
1158 reqideny ^Transfer-Encoding:[\ ]*chunked
1159 reqideny ^Host:\ apache-
willy tarreaueedaa9f2005-12-17 14:08:03 +01001160
willy tarreau197e8ec2005-12-17 14:10:59 +01001161 # block annoying worms that fill the logs...
1162 reqideny ^[^:\ ]*\ .*(\.|%2e)(\.|%2e)(%2f|%5c|/|\\\\)
1163 reqideny ^[^:\ ]*\ ([^\ ]*\ [^\ ]*\ |.*%00)
1164 reqideny ^[^:\ ]*\ .*<script
1165 reqideny ^[^:\ ]*\ .*/(root\.exe\?|cmd\.exe\?|default\.ida\?)
1166
1167 # allow other syntactically valid requests, and block any other method
1168 reqipass ^(GET|POST|HEAD|OPTIONS)\ /.*\ HTTP/1\.[01]$
1169 reqipass ^OPTIONS\ \\*\ HTTP/1\.[01]$
1170 reqideny ^[^:\ ]*\
1171
1172 # force connection:close, thus disabling HTTP keep-alive
1173 reqidel ^Connection:
1174 rspidel ^Connection:
1175 reqadd Connection:\ close
1176 rspadd Connection:\ close
1177
1178 # change the server name
1179 rspidel ^Server:\
1180 rspadd Server:\ Formilux/0.1.8
1181
1182
willy tarreauc1cae632005-12-17 14:12:23 +01001183Last, the 'forwardfor' option creates an HTTP 'X-Forwarded-For' header which
1184contains the client's IP address. This is useful to let the final web server
1185know what the client address was (eg for statistics on domains).
1186
1187Example :
1188---------
1189 listen http_proxy 0.0.0.0:80
1190 mode http
1191 log global
1192 option httplog
1193 option dontlognull
1194 option forwardfor
willy tarreau197e8ec2005-12-17 14:10:59 +01001195
11964.4) Load balancing with persistence
1197------------------------------------
1198
1199Combining cookie insertion with internal load balancing allows to transparently
1200bring persistence to applications. The principle is quite simple :
1201 - assign a cookie value to each server
1202 - enable the load balancing between servers
1203 - insert a cookie into responses resulting from the balancing algorithm
1204 (indirect accesses), end ensure that no upstream proxy will cache it.
1205 - remove the cookie in the request headers so that the application never sees
1206 it.
1207
1208Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001209---------
1210 listen application 0.0.0.0:80
1211 mode http
1212 cookie SERVERID insert nocache indirect
1213 balance roundrobin
1214 server 192.168.1.1:80 cookie server01 check
1215 server 192.168.1.2:80 cookie server02 check
1216
willy tarreau197e8ec2005-12-17 14:10:59 +010012174.5) Customizing errors
1218-----------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +01001219
willy tarreau197e8ec2005-12-17 14:10:59 +01001220Some situations can make haproxy return an HTTP error code to the client :
1221 - invalid or too long request => HTTP 400
1222 - request not completely sent in time => HTTP 408
1223 - forbidden request (matches a deny filter) => HTTP 403
1224 - internal error in haproxy => HTTP 500
1225 - the server returned an invalid or incomplete response => HTTP 502
1226 - no server was available to handle the request => HTTP 503
1227 - the server failed to reply in time => HTTP 504
willy tarreaueedaa9f2005-12-17 14:08:03 +01001228
willy tarreau197e8ec2005-12-17 14:10:59 +01001229A succint error message taken from the RFC accompanies these return codes.
1230But depending on the clients knowledge, it may be better to return custom, user
1231friendly, error pages. This is made possible through the use of the 'errorloc'
1232command :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001233
willy tarreau197e8ec2005-12-17 14:10:59 +01001234 errorloc <HTTP_code> <location>
willy tarreaueedaa9f2005-12-17 14:08:03 +01001235
willy tarreau197e8ec2005-12-17 14:10:59 +01001236Instead of generating an HTTP error <HTTP_code> among those above, the proxy
1237will return a temporary redirection code (HTTP 302) towards the address
1238specified in <location>. This address may be either relative to the site or
1239absolute. Since this request will be handled by the client's browser, it's
1240mandatory that the returned address be reachable from the outside.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001241
willy tarreau197e8ec2005-12-17 14:10:59 +01001242Example :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001243---------
1244 listen application 0.0.0.0:80
1245 errorloc 400 /badrequest.html
1246 errorloc 403 /forbidden.html
1247 errorloc 408 /toolong.html
1248 errorloc 500 http://haproxy.domain.net/bugreport.html
1249 errorloc 502 http://192.168.114.58/error50x.html
1250 errorloc 503 http://192.168.114.58/error50x.html
1251 errorloc 504 http://192.168.114.58/error50x.html
1252
willy tarreau197e8ec2005-12-17 14:10:59 +010012534.6) Modifying default values
1254-----------------------------
willy tarreaueedaa9f2005-12-17 14:08:03 +01001255
willy tarreau197e8ec2005-12-17 14:10:59 +01001256Version 1.1.22 introduced the notion of default values, which eliminates the
1257pain of often repeating common parameters between many instances, such as
1258logs, timeouts, modes, etc...
willy tarreaueedaa9f2005-12-17 14:08:03 +01001259
willy tarreau197e8ec2005-12-17 14:10:59 +01001260Default values are set in a 'defaults' section. Each of these section clears
1261all previously set default parameters, so there may be as many default
1262parameters as needed. Only the last one before a 'listen' section will be
1263used for this section. The 'defaults' section uses the same syntax as the
1264'listen' section, for the supported parameters. The 'defaults' keyword ignores
1265everything on its command line, so that fake instance names can be specified
1266there for better clarity.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001267
willy tarreau197e8ec2005-12-17 14:10:59 +01001268In version 1.1.23, only those parameters can be preset in the 'default'
1269section :
1270 - log (the first and second one)
willy tarreaueedaa9f2005-12-17 14:08:03 +01001271 - mode { tcp, http, health }
1272 - balance { roundrobin }
willy tarreau197e8ec2005-12-17 14:10:59 +01001273 - disabled (to disable every further instances)
1274 - enabled (to enable every further instances, this is the default)
willy tarreaueedaa9f2005-12-17 14:08:03 +01001275 - contimeout, clitimeout, srvtimeout, grace, retries, maxconn
1276 - option { redispatch, transparent, keepalive, forwardfor, httplog,
1277 dontlognull, persist, httpchk }
1278 - redispatch, redisp, transparent, source { addr:port }
1279 - cookie, capture
1280 - errorloc
1281
willy tarreau197e8ec2005-12-17 14:10:59 +01001282As of 1.1.24, it is not possible to put certain parameters in a 'defaults'
1283section, mainly regular expressions and server configurations :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001284 - dispatch, server,
willy tarreau197e8ec2005-12-17 14:10:59 +01001285 - req*, rsp*
willy tarreaueedaa9f2005-12-17 14:08:03 +01001286
willy tarreau197e8ec2005-12-17 14:10:59 +01001287Last, there's no way yet to change a boolean option from its assigned default
1288value. So if an 'option' statement is set in a 'defaults' section, the only
1289way to flush it is to redefine a new 'defaults' section without this 'option'.
willy tarreaueedaa9f2005-12-17 14:08:03 +01001290
willy tarreau197e8ec2005-12-17 14:10:59 +01001291Examples :
willy tarreaueedaa9f2005-12-17 14:08:03 +01001292----------
1293 defaults applications TCP
1294 log global
1295 mode tcp
1296 balance roundrobin
1297 clitimeout 180000
1298 srvtimeout 180000
1299 contimeout 4000
1300 retries 3
1301 redispatch
1302
1303 listen app_tcp1 10.0.0.1:6000-6063
1304 server srv1 192.168.1.1 check port 6000 inter 10000
1305 server srv2 192.168.1.2 backup
1306
1307 listen app_tcp2 10.0.0.2:6000-6063
1308 server srv1 192.168.2.1 check port 6000 inter 10000
1309 server srv2 192.168.2.2 backup
1310
1311 defaults applications HTTP
1312 log global
1313 mode http
1314 option httplog
1315 option forwardfor
1316 option dontlognull
1317 balance roundrobin
1318 clitimeout 20000
1319 srvtimeout 20000
1320 contimeout 4000
1321 retries 3
1322
1323 listen app_http1 10.0.0.1:80-81
1324 cookie SERVERID postonly insert indirect
1325 capture cookie userid= len 10
1326 server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000
1327 server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000
1328
1329 defaults
willy tarreau197e8ec2005-12-17 14:10:59 +01001330 # this empty section voids all default parameters
willy tarreaueedaa9f2005-12-17 14:08:03 +01001331
willy tarreau197e8ec2005-12-17 14:10:59 +01001332=========================
1333| System-specific setup |
1334=========================
willy tarreaueedaa9f2005-12-17 14:08:03 +01001335
willy tarreau197e8ec2005-12-17 14:10:59 +01001336Linux 2.4
1337=========
willy tarreaueedaa9f2005-12-17 14:08:03 +01001338
1339-- cut here --
1340#!/bin/sh
1341# set this to about 256/4M (16384 for 256M machine)
1342MAXFILES=16384
1343echo $MAXFILES > /proc/sys/fs/file-max
1344ulimit -n $MAXFILES
1345
1346if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then
1347 echo 65536 > /proc/sys/net/ipv4/ip_conntrack_max
1348fi
1349
1350if [ -e /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait ]; then
1351 # 30 seconds for fin, 15 for time wait
1352 echo 3000 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait
1353 echo 1500 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_time_wait
1354 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_invalid_scale
1355 echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_out_of_window
1356fi
1357
1358echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range
1359echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
1360echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog
1361echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets
1362echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans
1363echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
1364echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
1365echo 0 > /proc/sys/net/ipv4/tcp_timestamps
1366echo 0 > /proc/sys/net/ipv4/tcp_ecn
1367echo 0 > /proc/sys/net/ipv4/tcp_sack
1368echo 0 > /proc/sys/net/ipv4/tcp_dsack
1369
1370# auto-tuned on 2.4
1371#echo 262143 > /proc/sys/net/core/rmem_max
1372#echo 262143 > /proc/sys/net/core/rmem_default
1373
1374echo 16384 65536 524288 > /proc/sys/net/ipv4/tcp_rmem
1375echo 16384 349520 699040 > /proc/sys/net/ipv4/tcp_wmem
1376
1377-- cut here --
1378
willy tarreau197e8ec2005-12-17 14:10:59 +01001379
1380FreeBSD
1381=======
1382
1383A FreeBSD port of HA-Proxy is now available and maintained, thanks to
1384Clement Laforet <sheepkiller@cultdeadsheep.org>.
1385
1386For more information :
1387http://www.freebsd.org/cgi/url.cgi?ports/net/haproxy/pkg-descr
1388http://www.freebsd.org/cgi/cvsweb.cgi/ports/net/haproxy/
1389http://www.freshports.org/net/haproxy
1390
1391
1392-- end --