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