blob: 12a7eb76dc131c9cbbc9bfc36c836cb7008fcb37 [file] [log] [blame]
Willy Tarreau6a06a402007-07-15 20:15:28 +02001 ----------------------
2 HAProxy
3 Configuration Manual
4 ----------------------
Willy Tarreaubbd42122007-07-25 07:26:38 +02005 version 1.3.12.1
Willy Tarreau6a06a402007-07-15 20:15:28 +02006 willy tarreau
Willy Tarreaubbd42122007-07-25 07:26:38 +02007 2007/07/25
Willy Tarreau6a06a402007-07-15 20:15:28 +02008
9
10This document covers the configuration language as implemented in the version
11specified above. It does not provide any hint, example or advice. For such
12docuemntation, please refer to the Reference Manual or the Architecture Manual.
13
14
15HAProxy's configuration process involves 3 major sources of parameters :
16
17 - the arguments from the command-line, which always take precedence
18 - the "global" section, which sets process-wide parameters
19 - the proxies sections which can take form of "defaults", "listen",
20 "frontend" and "backend".
21
221. Global parameters
23--------------------
24
25Parameters in the "global" section are process-wide and often OS-specific. They
26are generally set once for all and do not need being changed once correct. Some
27of them have command-line equivalents.
28
29The following keywords are supported in the "global" section :
30
31 * Process management and security
32 - chroot
33 - daemon
34 - gid
35 - group
36 - log
37 - nbproc
38 - pidfile
39 - uid
40 - ulimit-n
41 - user
42
43 * Performance tuning
44 - maxconn
45 - noepoll
46 - nokqueue
47 - nopoll
48 - nosepoll
49 - tune.maxpollevents
50
51 * Debugging
52 - debug
53 - quiet
54 - stats
55
56
571.1) Process management and security
58------------------------------------
59
60chroot <jail dir>
61 Changes current directory to <jail dir> and performs a chroot() there before
62 dropping privileges. This increases the security level in case an unknown
63 vulnerability would be exploited, since it would make it very hard for the
64 attacker to exploit the system. This only works when the process is started
65 with superuser privileges. It is important to ensure that <jail_dir> is both
66 empty and unwritable to anyone.
67
68daemon
69 Makes the process fork into background. This is the recommended mode of
70 operation. It is equivalent to the command line "-D" argument. It can be
71 disabled by the command line "-db" argument.
72
73gid <number>
74 Changes the process' group ID to <number>. It is recommended that the group
75 ID is dedicated to HAProxy or to a small set of similar daemons. HAProxy must
76 be started with a user belonging to this group, or with superuser privileges.
77 See also "group" and "uid".
78
79group <group name>
80 Similar to "gid" but uses the GID of group name <group name> from /etc/group.
81 See also "gid" and "user".
82
83log <address> <facility> [max level]
84 Adds a global syslog server. Up to two global servers can be defined. They
85 will receive logs for startups and exits, as well as all logs from proxies
86 configured with "log global". <address> is an IPv4 address optionally
87 followed by a colon and an UDP port. If no port is specified, 514 is used
88 by default (the standard syslog port). <facility> must be one of the 24
89 standard syslog facilities :
90
91 kern user mail daemon auth syslog lpr news
92 uucp cron auth2 ftp ntp audit alert cron2
93 local0 local1 local2 local3 local4 local5 local6 local7
94
95 An optional level can be specified to filter outgoing messages. By default,
96 all messages are sent. If a level is specified, only messages with a severity
97 at least as important as this level will be sent. 8 levels are known :
98
99 emerg alert crit err warning notice info debug
100
101nbproc <number>
102 Creates <number> processes when going daemon. This requires the "daemon"
103 mode. By default, only one process is created, which is the recommended mode
104 of operation. For systems limited to small sets of file descriptors per
105 process, it may be needed to fork multiple daemons. USING MULTIPLE PROCESSES
106 IS HARDER TO DEBUG AND IS REALLY DISCOURAGED. See also "daemon".
107
108pidfile <pidfile>
109 Writes pids of all daemons into file <pidfile>. This option is equivalent to
110 the "-p" command line argument. The file must be accessible to the user
111 starting the process. See also "daemon".
112
113uid <number>
114 Changes the process' user ID to <number>. It is recommended that the user ID
115 is dedicated to HAProxy or to a small set of similar daemons. HAProxy must
116 be started with superuser privileges in order to be able to switch to another
117 one. See also "gid" and "user".
118
119ulimit-n <number>
120 Sets the maximum number of per-process file-descriptors to <number>. By
121 default, it is automatically computed, so it is recommended not to use this
122 option.
123
124user <user name>
125 Similar to "uid" but uses the UID of user name <user name> from /etc/passwd.
126 See also "uid" and "group".
127
128
1291.2) Performance tuning
130-----------------------
131
132maxconn <number>
133 Sets the maximum per-process number of concurrent connections to <number>. It
134 is equivalent to the command-line argument "-n". Proxies will stop accepting
135 connections when this limit is reached. The "ulimit-n" parameter is
136 automatically adjusted according to this value. See also "ulimit-n".
137
138noepoll
139 Disables the use of the "epoll" event polling system on Linux. It is
140 equivalent to the command-line argument "-de". The next polling system
141 used will generally be "poll". See also "nosepoll", and "nopoll".
142
143nokqueue
144 Disables the use of the "kqueue" event polling system on BSD. It is
145 equivalent to the command-line argument "-dk". The next polling system
146 used will generally be "poll". See also "nopoll".
147
148nopoll
149 Disables the use of the "poll" event polling system. It is equivalent to the
150 command-line argument "-dp". The next polling system used will be "select".
151 It should never be needed to didsable "poll" since it's available on all
152 platforms supported by HAProxy. See also "nosepoll", and "nopoll" and
153 "nokqueue".
154
155nosepoll
156 Disables the use of the "speculative epoll" event polling system on Linux. It
157 is equivalent to the command-line argument "-ds". The next polling system
158 used will generally be "epoll". See also "nosepoll", and "nopoll".
159
160tune.maxpollevents <number>
161 Sets the maximum amount of events that can be processed at once in a call to
162 the polling system. The default value is adapted to the operating system. It
163 has been noticed that reducing it below 200 tends to slightly decrease
164 latency at the expense of network bandwidth, and increasing it above 200
165 tends to trade latency for slightly increased bandwidth.
166
167
1681.3) Debugging
169---------------
170
171debug
172 Enables debug mode which dumps to stdout all exchanges, and disables forking
173 into background. It is the equivalent of the command-line argument "-d". It
174 should never be used in a production configuration since it may prevent full
175 system startup.
176
177quiet
178 Do not display any message during startup. It is equivalent to the command-
179 line argument "-q".
180
181stats
182 Dump internal statistics to stdout at regular interval. It is available for
183 development purposes only and should never be set.
184
185
1862) Proxies
187----------
188Proxy configuration can be located in a set of sections :
189 - defaults <name>
190 - frontend <name>
191 - backend <name>
192 - listen <name>
193
194A "defaults" section sets default parameters for all other sections following
195its declaration. Those default parameters are reset by the next "defaults"
196section. See below for the list of parameters which can be set in a "defaults"
197section.
198
199A "frontend" section describes a set of listening sockets accepting client
200connections.
201
202A "backend" section describes a set of servers to which the proxy will connect
203to forward incoming connections.
204
205A "listen" section defines a complete proxy with its frontend and backend
206parts combined in one section. It is generally useful for TCP-only traffic.
207
208The following list of keywords is supported. Most of them may only be used in a
209limited set of section types.
210
211keyword defaults frontend listen backend
212----------------------+----------+----------+---------+---------
213acl - X X X
214appsession - - X X
215balance - - X X
216bind - X X -
217block - X X X
218capture cookie X X X X
219capture request header X X X X
220capture response header X X X X
221clitimeout X X X -
222contimeout X X X X
223cookie X - X X
224default_backend - X X -
225disabled - X X X
226dispatch - - X X
227enabled - X X X
228errorfile X X X X
229errorloc X X X X
230errorloc302 X X X X
231errorloc303 X X X X
232fullconn X - X X
233grace - X X X
234log X X X X
235maxconn X X X -
236mode X X X X
237monitor-net X X X -
238monitor-uri X X X -
239option abortonclose X - X X
240option allbackups X - X X
241option checkcache X - X X
242option clitcpka X X X -
243option dontlognull X X X -
244option forceclose X - X X
245option forwardfor X X X X
246option httpchk X - X X
247option httpclose X X X X
248option httplog X X X X
249option logasap X X X -
250option persist X - X X
251option redispatch X - X X
252option smtpchk X - X X
253option srvtcpka X - X X
254option ssl-hello-chk X - X X
255option tcpka X X X X
256option tcplog X X X X
257option tcpsplice X X X X
258option transparent X X X -
259redisp X - X X
260redispatch X - X X
261reqadd - X X X
262reqallow - X X X
263reqdel - X X X
264reqdeny - X X X
265reqiallow - X X X
266reqidel - X X X
267reqideny - X X X
268reqipass - X X X
269reqirep - X X X
270reqisetbe - X X X
271reqitarpit - X X X
272reqpass - X X X
273reqrep - X X X
274reqsetbe - X X X
275reqtarpit - X X X
276retries X - X X
277rspadd - X X X
278rspdel - X X X
279rspdeny - X X X
280rspidel - X X X
281rspideny - X X X
282rspirep - X X X
283rsprep - X X X
284server - - X X
285source X - X X
286srvtimeout X - X X
Willy Tarreau24e779b2007-07-24 23:43:37 +0200287stats auth X - X X
288stats enable X - X X
289stats realm X - X X
Willy Tarreaubbd42122007-07-25 07:26:38 +0200290stats refresh X - X X
Willy Tarreau24e779b2007-07-24 23:43:37 +0200291stats scope X - X X
292stats uri X - X X
Willy Tarreau6a06a402007-07-15 20:15:28 +0200293transparent X X X -
294use_backend - X X -
295usesrc X - X X
296----------------------+----------+----------+---------+---------
297keyword defaults frontend listen backend
298
299
3002.1) using ACLs
301---------------
302
303The use of Access Control Lists (ACL) provides a flexible solution to perform
304content switching. The principle is simple :
305
306 - define test criteria with sets of values
307 - perform actions only if a set of tests is valid
308
309The actions generally consist in blocking the request, or selecting a backend.
310
311In order to define a test, the "acl" keyword is used. The syntax is :
312
313 acl <aclname> <criterion> [flags] [operator] <value> ...
314
315This creates an ACL <aclname> or completes an existing one with new
316tests. Those tests apply to the portion of request specified in <criterion>
317and may be adjusted with optional flags [flags]. Some criteria also support
318an operator which may be specified before the set of values. The values are
319of the type supported by the criterion, and are separated by spaces.
320
321There is no limit to the number of ACLs. The unused ones do not affect
322performance, they just consume a small amount of memory.
323
324The current flags are currently supported :
325
326 -i : ignore case during matching.
327 -- : force end of flags. Useful when a string looks like one of the flags.
328
329Supported types of values are :
330 - integers or integer ranges
331 - strings
332 - regular expressions
333 - IP addresses and networks
334
335
3362.1.1) Matching integers
337------------------------
338
339Matching integers is special in that ranges and operators are permitted. Note
340that integer matching only applies to positive values. A range is a value
341expressed with a lower and an upper bound separated with a colon, both of which
342may be omitted.
343
344For instance, "1024:65535" is a valid range to represent a range of
345unprivileged ports, and "1024:" would also work. "0:1023" is a valid
346representation of privileged ports, and ":1023" would also work.
347
348For an easier usage, comparison operators are also supported. Note that using
349operators with ranges does not make much sense and is discouraged. Also, it
350does not make much sense to perform order comparisons with a set of values.
351
352Available operators are :
353
354 eq : true if the tested value equals at least one value
355 ge : true if the tested value is greater than or equal to at least one value
356 gt : true if the tested value is greater than at least one value
357 le : true if the tested value is less than or equal to at least one value
358 lt : true if the tested value is less than at least one value
359
360For instance, the following ACL matches negative Content-Length headers :
361
362 acl negative-length hdr_val(content-length) lt 0
363
364
3652.1.2) Matching strings
366-----------------------
367
368String matching applies to verbatim strings as they are passed, with the
369exception of the backslash ("\") which makes it possible to escape some
370characters such as the space. If the "-i" flag is passed before the first
371string, then the matching will be performed ignoring the case. In order
372to match the string "-i", either set it second, or pass the "--" flag
373before the first string.
374
375
3762.1.3) Matching regular expressions (regexes)
377---------------------------------------------
378
379Just like with string matching, regex matching applies to verbatim strings as
380they are passed, with the exception of the backslash ("\") which makes it
381possible to escape some characters such as the space. If the "-i" flag is
382passed before the first regex, then the matching will be performed ignoring
383the case. In order to match the string "-i", either set it second, or pass
384the "--" flag before the first string.
385
386
3872.1.4) Matching IPv4 addresses
388----------------------------
389
390IPv4 addresses values can be specified either as plain addresses or with a
391netmask appended, in which case the IPv4 address matches whenever it is
392within the network. Plain addresses may also be replaced with a resolvable
393host name, but this practise is generally discouraged as it makes it more
394difficult to read configurations.
395
396
3972.1.5) Available matching criteria
398----------------------------------
399
400always_false
401 This one never matches. All values and flags are ignored. It may be used as
402 a temporary replacement for another one when adjusting configurations.
403
404always_true
405 This one always matches. All values and flags are ignored. It may be used as
406 a temporary replacement for another one when adjusting configurations.
407
408src <ip_address>
409 Applies to the client's IP address. It is usually used to limit access to
410 certain resources such as statistics. Note that it is the TCP-level source
411 address which is used, and not the address of a client behind a proxy.
412
413src_port <integer>
414 Applies to the client's TCP source port. This has a very limited usage.
415
416dst <ip_address>
417 Applies to the local IP address the client connected to. It can be used to
418 switch to a different backend for some alternative addresses.
419
420dst_port <integer>
421 Applies to the local port the client connected to. It can be used to switch
422 to a different backend for some alternative ports.
423
424dst_conn <integer>
425 Applies to the number of currently established connections on the frontend,
426 including the one being evaluated. It can be used to either return a sorry
427 page before hard-blocking, or to use a specific backend to drain the requests
428 when the farm is considered saturated.
429
430method <string>
431 Applies to the method in the HTTP request, eg: "GET". Some predefined ACL
432 already check for most common methods.
433
434req_ver <string>
435 Applies to the version string in the HTTP request, eg: "1.0". Some predefined
436 ACL already check for versions 1.0 and 1.1.
437
438path <string>
439 Returns true when the path part of the request, which starts at the first
440 slash and ends before the question mark, equals one of the strings. It may be
441 used to match known files, such as /favicon.ico.
442
443path_beg <string>
444 Returns true when the path begins with one of the strings. This can be used to
445 send certain directory names to alternative backends.
446
447path_end <string>
448 Returns true when the path ends with one of the strings. This may be used to
449 control file name extension.
450
451path_sub <string>
452 Returns true when the path contains one of the strings. It can be used to
453 detect particular patterns in paths, such as "../" for example. See also
454 "path_dir".
455
456path_dir <string>
457 Returns true when one of the strings is found isolated or delimited with
458 slashes in the path. This is used to perform filename or directory name
459 matching without the risk of wrong match due to colliding prefixes. See also
460 "url_dir" and "path_sub".
461
462path_dom <string>
463 Returns true when one of the strings is found isolated or delimited with dots
464 in the path. This may be used to perform domain name matching in proxy
465 requests. See also "path_sub" and "url_dom".
466
467path_reg <regex>
468 Returns true when the path matches one of the regular expressions. It can be
469 used any time, but it is important to remember that regex matching is slower
470 than other methods. See also "url_reg" and all "path_" criteria.
471
472url <string>
473 Applies to the whole URL passed in the request. The only real use is to match
474 "*", for which there already is a predefined ACL.
475
476url_beg <string>
477 Returns true when the URL begins with one of the strings. This can be used to
478 check whether a URL begins with a slash or with a protocol scheme.
479
480url_end <string>
481 Returns true when the URL ends with one of the strings. It has very limited
482 use. "path_end" should be used instead for filename matching.
483
484url_sub <string>
485 Returns true when the URL contains one of the strings. It can be used to
486 detect particular patterns in query strings for example. See also "path_sub".
487
488url_dir <string>
489 Returns true when one of the strings is found isolated or delimited with
490 slashes in the URL. This is used to perform filename or directory name
491 matching without the risk of wrong match due to colliding prefixes. See also
492 "path_dir" and "url_sub".
493
494url_dom <string>
495 Returns true when one of the strings is found isolated or delimited with dots
496 in the URL. This is used to perform domain name matching without the risk of
497 wrong match due to colliding prefixes. See also "url_sub".
498
499url_reg <regex>
500 Returns true when the URL matches one of the regular expressions. It can be
501 used any time, but it is important to remember that regex matching is slower
502 than other methods. See also "path_reg" and all "url_" criteria.
503
504hdr <string>
505hdr(header) <string>
506 Note: all the "hdr*" matching criteria either apply to all headers, or to a
507 particular header whose name is passed between parenthesis and without any
508 space. The header matching complies with RFC2616, and treats as separate
509 headers all values delimited by comas.
510
511 The "hdr" criteria returns true if any of the headers matching the criteria
512 match any of the strings. This can be used to check exact values. For
513 instance, checking that "connection: close" is set :
514
515 hdr(Connection) -i close
516
517hdr_beg <string>
518hdr_beg(header) <string>
519 Returns true when one of the headers begins with one of the strings. See
520 "hdr" for more information on header matching.
521
522hdr_end <string>
523hdr_end(header) <string>
524 Returns true when one of the headers ends with one of the strings. See "hdr"
525 for more information on header matching.
526
527hdr_sub <string>
528hdr_sub(header) <string>
529 Returns true when one of the headers contains one of the strings. See "hdr"
530 for more information on header matching.
531
532hdr_dir <string>
533hdr_dir(header) <string>
534 Returns true when one of the headers contains one of the strings either
535 isolated or delimited by slashes. This is used to perform filename or
536 directory name matching, and may be used with Referer. See "hdr" for more
537 information on header matching.
538
539hdr_dom <string>
540hdr_dom(header) <string>
541 Returns true when one of the headers contains one of the strings either
542 isolated or delimited by dots. This is used to perform domain name matching,
543 and may be used with the Host header. See "hdr" for more information on
544 header matching.
545
546hdr_reg <regex>
547hdr_reg(header) <regex>
548 Returns true when one of the headers matches of the regular expressions. It
549 can be used at any time, but it is important to remember that regex matching
550 is slower than other methods. See also other "hdr_" criteria, as well as
551 "hdr" for more information on header matching.
552
553hdr_val <integer>
554hdr_val(header) <integer>
555 Returns true when one of the headers starts with a number which matches the
556 values or ranges specified. This may be used to limit content-length to
557 acceptable values for example. See "hdr" for more information on header
558 matching.
559
560hdr_cnt <integer>
561hdr_cnt(header) <integer>
562 Returns true when the count of the headers which matches the values or ranges
563 specified. This is used to detect presence or absence of a specific header,
564 as well as to block request smugling attacks by rejecting requests which
565 contain more than one of certain headers. See "hdr" for more information on
566 header matching.
567
568
5692.1.6) Pre-defined ACLs
570-----------------------
571
572Some predefined ACLs are hard-coded so that they do not have to be declared in
573every frontend which needs them. They all have their names in upper case in
574order to avoid confusion. Their equivalence is provided below :
575
576ACL name Equivalent to Usage
577---------------+-----------------------------+---------------------------------
578TRUE always_true 1 always match
579FALSE always_false 0 never match
580LOCALHOST src 127.0.0.1/8 match connection from local host
581HTTP_1.0 req_ver 1.0 match HTTP version 1.0
582HTTP_1.1 req_ver 1.1 match HTTP version 1.1
583METH_CONNECT method CONNECT match HTTP CONNECT method
584METH_GET method GET HEAD match HTTP GET or HEAD method
585METH_HEAD method HEAD match HTTP HEAD method
586METH_OPTIONS method OPTIONS match HTTP OPTIONS method
587METH_POST method POST match HTTP POST method
588METH_TRACE method TRACE match HTTP TRACE method
589HTTP_URL_ABS url_reg ^[^/:]*:// match absolute URL with scheme
590HTTP_URL_SLASH url_beg / match URL begining with "/"
591HTTP_URL_STAR url * match URL equal to "*"
592HTTP_CONTENT hdr_val(content-length) gt 0 match an existing content-length
593---------------+-----------------------------+---------------------------------
594
595
5962.1.7) Using ACLs to form conditions
597------------------------------------
598
599Some actions are only performed upon a valid condition. A condition is a
600combination of ACLs with operators. 3 operators are supported :
601
602 - AND (implicit)
603 - OR (explicit with the "or" keyword or the "||" operator)
604 - Negation with the exclamation mark ("!")
605
606A condition is formed as a disjonctive form :
607
608 [!]acl1 [!]acl2 ... [!]acln { or [!]acl1 [!]acl2 ... [!]acln } ...
609
610Such conditions are generally used after an "if" or "unless" statement,
611indicating when the condition will trigger the action.
612
613For instance, to block HTTP requests to the "*" URL with methods other than
614"OPTIONS", as well as POST requests without content-length, and GET/HEAD
615requests with a content-length greater than 0, and finally every request
616which is not either GET/HEAD/POST/OPTIONS !
617
618 acl missing_cl hdr_cnt(Content-length) eq 0
619 block if HTTP_URL_STAR !METH_OPTIONS || METH_POST missing_cl
620 block if METH_GET HTTP_CONTENT
621 block unless METH_GET or METH_POST or METH_OPTIONS
622
623To select a different backend for requests to static contents on the "www" site
624and to every request on the "img", "video", "download" and "ftp" hosts :
625
626 acl url_static path_beg /static /images /img /css
627 acl url_static path_end .gif .png .jpg .css .js
628 acl host_www hdr_beg(host) -i www
629 acl host_static hdr_beg(host) -i img. video. download. ftp.
630
631 # now use backend "static" for all static-only hosts, and for static urls
632 # of host "www". Use backend "www" for the rest.
633 use_backend static if host_static or host_www url_static
634 use_backend www if host_www
635
636See below for the detailed help on the "block" and "use_backend" keywords.