willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1 | ------------------- |
| 2 | H A - P r o x y |
| 3 | Reference Manual |
| 4 | ------------------- |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 5 | version 1.2.13 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 6 | willy tarreau |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 7 | 2006/05/13 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 8 | |
| 9 | ============ |
| 10 | | Abstract | |
| 11 | ============ |
| 12 | |
| 13 | HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high |
| 14 | availability environments. Indeed, it can : |
| 15 | - route HTTP requests depending on statically assigned cookies ; |
| 16 | - spread the load among several servers while assuring server persistence |
| 17 | through the use of HTTP cookies ; |
| 18 | - switch to backup servers in the event a main one fails ; |
| 19 | - accept connections to special ports dedicated to service monitoring ; |
| 20 | - stop accepting connections without breaking existing ones ; |
| 21 | - add/modify/delete HTTP headers both ways ; |
| 22 | - block requests matching a particular pattern ; |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 23 | - hold clients to the right application server depending on application |
| 24 | cookies |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 25 | |
| 26 | It needs very little resource. Its event-driven architecture allows it to easily |
| 27 | handle thousands of simultaneous connections on hundreds of instances without |
| 28 | risking the system's stability. |
| 29 | |
| 30 | ==================== |
| 31 | | Start parameters | |
| 32 | ==================== |
| 33 | |
| 34 | There are only a few command line options : |
| 35 | |
| 36 | -f <configuration file> |
| 37 | -n <high limit for the total number of simultaneous connections> |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 38 | = 'maxconn' in 'global' section |
| 39 | -N <high limit for the per-listener number of simultaneous connections> |
| 40 | = 'maxconn' in 'listen' or 'default' sections |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 41 | -d starts in foregreound with debugging mode enabled |
| 42 | -D starts in daemon mode |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 43 | -q disable messages on output |
| 44 | -V displays messages on output even when -q or 'quiet' are specified. |
| 45 | -c only checks config file and exits with code 0 if no error was found, or |
| 46 | exits with code 1 if a syntax error was found. |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 47 | -p <pidfile> asks the process to write down each of its children's |
| 48 | pids to this file in daemon mode. |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 49 | -sf specifies a list of pids to send a FINISH signal to after startup. |
| 50 | -st specifies a list of pids to send a TERMINATE signal to after startup. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 51 | -s shows statistics (only if compiled in) |
| 52 | -l shows even more statistics (implies '-s') |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 53 | -de disables use of epoll() |
| 54 | -dp disables use of poll() |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 55 | -db disables background mode (stays in foreground, useful for debugging) |
| 56 | -m <megs> enforces a memory usage limit to a maximum of <megs> megabytes. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 57 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 58 | The maximal number of connections per proxy instance is used as the default |
| 59 | parameter for each instance for which the 'maxconn' paramter is not set in the |
| 60 | 'listen' section. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 61 | |
| 62 | The maximal number of total connections limits the number of connections used by |
| 63 | the whole process if the 'maxconn' parameter is not set in the 'global' section. |
| 64 | |
| 65 | The debugging mode has the same effect as the 'debug' option in the 'global' |
| 66 | section. When the proxy runs in this mode, it dumps every connections, |
| 67 | disconnections, timestamps, and HTTP headers to stdout. This should NEVER |
| 68 | be used in an init script since it will prevent the system from starting up. |
| 69 | |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 70 | For debugging, the '-db' option is very useful as it temporarily disables |
| 71 | daemon mode and multi-process mode. The service can then be stopped by simply |
| 72 | pressing Ctrl-C, without having to edit the config nor run full debug. |
| 73 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 74 | Statistics are only available if compiled in with the 'STATTIME' option. It's |
| 75 | only used during code optimization phases. |
| 76 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 77 | The '-st' and '-sf' options are used for hot reconfiguration (see below). |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 78 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 79 | ====================== |
| 80 | | Configuration file | |
| 81 | ====================== |
| 82 | |
| 83 | Structure |
| 84 | ========= |
| 85 | |
| 86 | The configuration file parser ignores empty lines, spaces, tabs. Anything |
| 87 | between a sharp ('#') not following a backslash ('\'), and the end of a line |
| 88 | constitutes a comment and is ignored too. |
| 89 | |
| 90 | The configuration file is segmented in sections. A section begins whenever |
| 91 | one of these 3 keywords are encountered : |
| 92 | |
| 93 | - 'global' |
| 94 | - 'listen' |
| 95 | - 'defaults' |
| 96 | |
| 97 | Every parameter refer to the section beginning at the last one of these 3 |
| 98 | keywords. |
| 99 | |
| 100 | |
| 101 | 1) Global parameters |
| 102 | ==================== |
| 103 | |
| 104 | Global parameters affect the whole process behaviour. They are all set in the |
| 105 | 'global' section. There may be several 'global' sections if needed, but their |
| 106 | parameters will only be merged. Allowed parameters in 'global' section include |
| 107 | the following ones : |
| 108 | |
| 109 | - log <address> <facility> [max_level] |
| 110 | - maxconn <number> |
| 111 | - uid <user id> |
| 112 | - gid <group id> |
| 113 | - chroot <directory> |
| 114 | - nbproc <number> |
| 115 | - daemon |
| 116 | - debug |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 117 | - noepoll |
| 118 | - nopoll |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 119 | - quiet |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 120 | - pidfile <file> |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 121 | - ulimit-n <number> |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 122 | - stats |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 123 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 124 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 125 | 1.1) Event logging |
| 126 | ------------------ |
| 127 | Most events are logged : start, stop, servers going up and down, connections and |
| 128 | errors. Each event generates a syslog message which can be sent to up to 2 |
| 129 | servers. The syntax is : |
| 130 | |
| 131 | log <ip_address> <facility> [max_level] |
| 132 | |
| 133 | Connections are logged at level "info". Services initialization and servers |
| 134 | going up are logged at level "notice", termination signals are logged at |
| 135 | "warning", and definitive service termination, as well as loss of servers are |
| 136 | logged at level "alert". The optional parameter <max_level> specifies above |
| 137 | what level messages should be sent. Level can take one of these 8 values : |
| 138 | |
| 139 | emerg, alert, crit, err, warning, notice, info, debug |
| 140 | |
| 141 | For backwards compatibility with versions 1.1.16 and earlier, the default level |
| 142 | value is "debug" if not specified. |
| 143 | |
| 144 | Permitted facilities are : |
| 145 | kern, user, mail, daemon, auth, syslog, lpr, news, |
| 146 | uucp, cron, auth2, ftp, ntp, audit, alert, cron2, |
| 147 | local0, local1, local2, local3, local4, local5, local6, local7 |
| 148 | |
| 149 | According to RFC3164, messages are truncated to 1024 bytes before being emitted. |
| 150 | |
| 151 | Example : |
| 152 | --------- |
| 153 | global |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 154 | log 192.168.2.200 local3 |
| 155 | log 127.0.0.1 local4 notice |
| 156 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 157 | |
| 158 | 1.2) limiting the number of connections |
| 159 | --------------------------------------- |
| 160 | It is possible and recommended to limit the global number of per-process |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 161 | connections using the 'maxconn' global keyword. Since one connection includes |
| 162 | both a client and a server, it means that the max number of TCP sessions will |
| 163 | be about the double of this number. It's important to understand this when |
| 164 | trying to find best values for 'ulimit -n' before starting the proxy. To |
| 165 | anticipate the number of sockets needed, all these parameters must be counted : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 166 | |
| 167 | - 1 socket per incoming connection |
| 168 | - 1 socket per outgoing connection |
| 169 | - 1 socket per address/port/proxy tuple. |
| 170 | - 1 socket per server being health-checked |
| 171 | - 1 socket for all logs |
| 172 | |
| 173 | In simple configurations where each proxy only listens one one address/port, |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 174 | set the limit of file descriptors (ulimit -n) to |
| 175 | (2 * maxconn + nbproxies + nbservers + 1). Starting with versions 1.1.32/1.2.6, |
| 176 | it is now possible to set the limit in the configuration using the 'ulimit-n' |
| 177 | global keyword, provided the proxy is started as root. This puts an end to the |
| 178 | recurrent problem of ensuring that the system limits are adapted to the proxy |
| 179 | values. Note that these limits are per-process. |
| 180 | |
| 181 | Example : |
| 182 | --------- |
| 183 | global |
| 184 | maxconn 32000 |
| 185 | ulimit-n 65536 |
| 186 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 187 | |
| 188 | 1.3) Drop of priviledges |
| 189 | ------------------------ |
| 190 | In order to reduce the risk and consequences of attacks, in the event where a |
| 191 | yet non-identified vulnerability would be successfully exploited, it's possible |
| 192 | to lower the process priviledges and even isolate it in a riskless directory. |
| 193 | |
| 194 | In the 'global' section, the 'uid' parameter sets a numerical user identifier |
| 195 | which the process will switch to after binding its listening sockets. The value |
| 196 | '0', which normally represents the super-user, here indicates that the UID must |
| 197 | not change during startup. It's the default behaviour. The 'gid' parameter does |
| 198 | the same for the group identifier. It's particularly advised against use of |
| 199 | generic accounts such as 'nobody' because it has the same consequences as using |
| 200 | 'root' if other services use them. |
| 201 | |
| 202 | The 'chroot' parameter makes the process isolate itself in an empty directory |
| 203 | just before switching its UID. This type of isolation (chroot) can sometimes |
| 204 | be worked around on certain OS (Linux, Solaris), provided that the attacker |
| 205 | has gained 'root' priviledges and has the ability to use or create a directory. |
| 206 | For this reason, it's capital to use a dedicated directory and not to share one |
| 207 | between several services of different nature. To make isolation more resistant, |
| 208 | it's recommended to use an empty directory without any right, and to change the |
| 209 | UID of the process so that it cannot do anything there. |
| 210 | |
| 211 | Note: in the event where such a vulnerability would be exploited, it's most |
| 212 | likely that first attempts would kill the process due to 'Segmentation Fault', |
| 213 | 'Bus Error' or 'Illegal Instruction' signals. Eventhough it's true that |
| 214 | isolating the server reduces the risks of intrusion, it's sometimes useful to |
| 215 | find why a process dies, via the analysis of a 'core' file, although very rare |
| 216 | (the last bug of this sort was fixed in 1.1.9). For security reasons, most |
| 217 | systems disable the generation of core file when a process changes its UID. So |
| 218 | the two workarounds are either to start the process from a restricted user |
| 219 | account, which will not be able to chroot itself, or start it as root and not |
| 220 | change the UID. In both cases the core will be either in the start or the chroot |
| 221 | directories. Do not forget to allow core dumps prior to start the process : |
| 222 | |
| 223 | # ulimit -c unlimited |
| 224 | |
| 225 | Example : |
| 226 | --------- |
| 227 | |
| 228 | global |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 229 | uid 30000 |
| 230 | gid 30000 |
| 231 | chroot /var/chroot/haproxy |
| 232 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 233 | |
| 234 | 1.4) Startup modes |
| 235 | ------------------ |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 236 | The service can start in several different modes : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 237 | - foreground / background |
| 238 | - quiet / normal / debug |
| 239 | |
| 240 | The default mode is normal, foreground, which means that the program doesn't |
| 241 | return once started. NEVER EVER use this mode in a system startup script, or |
| 242 | the system won't boot. It needs to be started in background, so that it |
| 243 | returns immediately after forking. That's accomplished by the 'daemon' option |
| 244 | in the 'global' section, which is the equivalent of the '-D' command line |
| 245 | argument. |
| 246 | |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 247 | The '-db' command line argument overrides the 'daemon' and 'nbproc' global |
| 248 | options to make the process run in normal, foreground mode. |
| 249 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 250 | Moreover, certain alert messages are still sent to the standard output even |
| 251 | in 'daemon' mode. To make them disappear, simply add the 'quiet' option in the |
| 252 | 'global' section. This option has no command-line equivalent. |
| 253 | |
| 254 | Last, the 'debug' mode, enabled with the 'debug' option in the 'global' section, |
| 255 | and which is equivalent of the '-d' option, allows deep TCP/HTTP analysis, with |
| 256 | timestamped display of each connection, disconnection, and HTTP headers for both |
| 257 | ways. This mode is incompatible with 'daemon' and 'quiet' modes for obvious |
| 258 | reasons. |
| 259 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 260 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 261 | 1.5) Increasing the overall processing power |
| 262 | -------------------------------------------- |
| 263 | On multi-processor systems, it may seem to be a shame to use only one processor, |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 264 | eventhough the load needed to saturate a recent processor is far above common |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 265 | usage. Anyway, for very specific needs, the proxy can start several processes |
| 266 | between which the operating system will spread the incoming connections. The |
| 267 | number of processes is controlled by the 'nbproc' parameter in the 'global' |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 268 | section. It defaults to 1, and obviously works only in 'daemon' mode. One |
| 269 | typical usage of this parameter has been to workaround the default per-process |
| 270 | file-descriptor limit that Solaris imposes to user processes. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 271 | |
| 272 | Example : |
| 273 | --------- |
| 274 | |
| 275 | global |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 276 | daemon |
| 277 | quiet |
| 278 | nbproc 2 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 279 | |
| 280 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 281 | 1.6) Helping process management |
| 282 | ------------------------------- |
| 283 | Haproxy now supports the notion of pidfile. If the '-p' command line argument, |
| 284 | or the 'pidfile' global option is followed with a file name, this file will be |
| 285 | removed, then filled with all children's pids, one per line (only in daemon |
| 286 | mode). This file is NOT within the chroot, which allows to work with a readonly |
| 287 | chroot. It will be owned by the user starting the process, and will have |
| 288 | permissions 0644. |
| 289 | |
| 290 | Example : |
| 291 | --------- |
| 292 | |
| 293 | global |
| 294 | daemon |
| 295 | quiet |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 296 | nbproc 2 |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 297 | pidfile /var/run/haproxy-private.pid |
| 298 | |
| 299 | # to stop only those processes among others : |
| 300 | # kill $(</var/run/haproxy-private.pid) |
| 301 | |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 302 | # to reload a new configuration with minimal service impact and without |
| 303 | # breaking existing sessions : |
| 304 | # haproxy -f haproxy.cfg -p $(</var/run/haproxy-private.pid) -st $(</var/run/haproxy-private.pid) |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 305 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 306 | 1.7) Polling mechanisms |
| 307 | ----------------------- |
| 308 | Starting from version 1.2.5, haproxy supports the poll() and epoll() polling |
| 309 | mechanisms. On systems where select() is limited by FD_SETSIZE (like Solaris), |
| 310 | poll() can be an interesting alternative. Performance tests show that Solaris' |
| 311 | poll() performance does not decay as fast as the numbers of sockets increase, |
| 312 | making it a safe solution for high loads. However, Solaris already uses poll() |
| 313 | to emulate select(), so as long as the number of sockets has no reason to go |
| 314 | higher than FD_SETSIZE, poll() should not provide any better performance. On |
| 315 | Linux systems with the epoll() patch (or any 2.6 version), haproxy will use |
| 316 | epoll() which is extremely fast and non dependant on the number of sockets. |
| 317 | Tests have shown constant performance from 1 to 20000 simultaneous sessions. |
| 318 | |
| 319 | Haproxy will use epoll() when available, and will fall back to poll(), then to |
| 320 | select(). However, if for any reason you need to disable epoll() or poll() (eg. |
| 321 | because of a bug or just to compare performance), two new global options have |
| 322 | been created for this matter : 'noepoll' and 'nopoll'. |
| 323 | |
| 324 | Example : |
| 325 | --------- |
| 326 | |
| 327 | global |
| 328 | # use only select() |
| 329 | noepoll |
| 330 | nopoll |
| 331 | |
| 332 | Note : |
| 333 | ------ |
| 334 | For the sake of configuration file portability, these options are accepted but |
| 335 | ignored if the poll() or epoll() mechanisms have not been enabled at compile |
| 336 | time. |
| 337 | |
| 338 | To make debugging easier, the '-de' runtime argument disables epoll support and |
| 339 | the '-dp' argument disables poll support. They are respectively equivalent to |
| 340 | 'noepoll' and 'nopoll'. |
| 341 | |
| 342 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 343 | 2) Declaration of a listening service |
| 344 | ===================================== |
| 345 | |
| 346 | Service sections start with the 'listen' keyword : |
| 347 | |
| 348 | listen <instance_name> [ <IP_address>:<port_range>[,...] ] |
| 349 | |
| 350 | - <instance_name> is the name of the instance. This name will be reported in |
| 351 | logs, so it is good to have it reflect the proxied service. No unicity test |
| 352 | is done on this name, and it's not mandatory for it to be unique, but highly |
| 353 | recommended. |
| 354 | |
| 355 | - <IP_address> is the IP address the proxy binds to. Empty address, '*' and |
| 356 | '0.0.0.0' all mean that the proxy listens to all valid addresses on the |
| 357 | system. |
| 358 | |
| 359 | - <port_range> is either a unique port, or a port range for which the proxy will |
| 360 | accept connections for the IP address specified above. This range can be : |
| 361 | - a numerical port (ex: '80') |
| 362 | - a dash-delimited ports range explicitly stating the lower and upper bounds |
| 363 | (ex: '2000-2100') which are included in the range. |
| 364 | |
| 365 | Particular care must be taken against port ranges, because every <addr:port> |
| 366 | couple consumes one socket (=a file descriptor), so it's easy to eat lots of |
| 367 | descriptors with a simple range. The <addr:port> couple must be used only once |
| 368 | among all instances running on a same system. Please note that attaching to |
| 369 | ports lower than 1024 need particular priviledges to start the program, which |
| 370 | are independant of the 'uid' parameter. |
| 371 | |
| 372 | - the <IP_address>:<port_range> couple may be repeated indefinitely to require |
| 373 | the proxy to listen to other addresses and/or ports. To achieve this, simply |
| 374 | separate them with a coma. |
| 375 | |
| 376 | Examples : |
| 377 | --------- |
| 378 | listen http_proxy :80 |
| 379 | listen x11_proxy 127.0.0.1:6000-6009 |
| 380 | listen smtp_proxy 127.0.0.1:25,127.0.0.1:587 |
| 381 | listen ldap_proxy :389,:663 |
| 382 | |
| 383 | In the event that all addresses do not fit line width, it's preferable to |
| 384 | detach secondary addresses on other lines with the 'bind' keyword. If this |
| 385 | keyword is used, it's not even necessary to specify the first address on the |
| 386 | 'listen' line, which sometimes makes multiple configuration handling easier : |
| 387 | |
| 388 | bind [ <IP_address>:<port_range>[,...] ] |
| 389 | |
| 390 | Examples : |
| 391 | ---------- |
| 392 | listen http_proxy |
| 393 | bind :80,:443 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 394 | bind 10.0.0.1:10080,10.0.0.1:10443 |
| 395 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 396 | |
| 397 | 2.1) Inhibiting a service |
| 398 | ------------------------- |
| 399 | A service may be disabled for maintenance reasons, without needing to comment |
| 400 | out the whole section, simply by specifying the 'disabled' keyword in the |
| 401 | section to be disabled : |
| 402 | |
| 403 | listen smtp_proxy 0.0.0.0:25 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 404 | disabled |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 405 | |
| 406 | Note: the 'enabled' keyword allows to enable a service which has been disabled |
| 407 | previously by a default configuration. |
| 408 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 409 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 410 | 2.2) Modes of operation |
| 411 | ----------------------- |
| 412 | A service can work in 3 different distinct modes : |
| 413 | - TCP |
| 414 | - HTTP |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 415 | - health |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 416 | |
| 417 | TCP mode |
| 418 | -------- |
| 419 | In this mode, the service relays TCP connections as soon as they're established, |
| 420 | towards one or several servers. No processing is done on the stream. It's only |
| 421 | an association of source(addr:port) -> destination(addr:port). To use this mode, |
| 422 | you must specify 'mode tcp' in the 'listen' section. This is the default mode. |
| 423 | |
| 424 | Example : |
| 425 | --------- |
| 426 | listen smtp_proxy 0.0.0.0:25 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 427 | mode tcp |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 428 | |
| 429 | HTTP mode |
| 430 | --------- |
| 431 | In this mode, the service relays TCP connections towards one or several servers, |
| 432 | when it has enough informations to decide, which normally means that all HTTP |
| 433 | headers have been read. Some of them may be scanned for a cookie or a pattern |
| 434 | matching a regex. To use this mode, specify 'mode http' in the 'listen' section. |
| 435 | |
| 436 | Example : |
| 437 | --------- |
| 438 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 439 | mode http |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 440 | |
| 441 | Health-checking mode |
| 442 | -------------------- |
| 443 | This mode provides a way for external components to check the proxy's health. |
| 444 | It is meant to be used with intelligent load-balancers which can use send/expect |
| 445 | scripts to check for all of their servers' availability. This one simply accepts |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 446 | the connection, returns the word 'OK' and closes it. If the 'option httpchk' is |
| 447 | set, then the reply will be 'HTTP/1.0 200 OK' with no data, so that it can be |
| 448 | tested from a tool which supports HTTP health-checks. To enable it, simply |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 449 | specify 'health' as the working mode : |
| 450 | |
| 451 | Example : |
| 452 | --------- |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 453 | # simple response : 'OK' |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 454 | listen health_check 0.0.0.0:60000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 455 | mode health |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 456 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 457 | # HTTP response : 'HTTP/1.0 200 OK' |
| 458 | listen http_health_check 0.0.0.0:60001 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 459 | mode health |
| 460 | option httpchk |
| 461 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 462 | 2.2.1 Monitoring |
| 463 | ---------------- |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 464 | Versions 1.1.32 and 1.2.6 provide a new solution to check the proxy's |
| 465 | availability without perturbating the service. The 'monitor-net' keyword was |
| 466 | created to specify a network of equipments which CANNOT use the service for |
| 467 | anything but health-checks. This is particularly suited to TCP proxies, because |
| 468 | it prevents the proxy from relaying the monitor's connection to the remote |
| 469 | server. |
| 470 | |
| 471 | When used with TCP, the connection is accepted then closed and nothing is |
| 472 | logged. This is enough for a front-end load-balancer to detect the service as |
| 473 | available. |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 474 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 475 | When used with HTTP, the connection is accepted, nothing is logged, the |
| 476 | following response is sent, then the session is closed : "HTTP/1.0 200 OK". |
| 477 | This is normally enough for any front-end HTTP load-balancer to detect the |
| 478 | service as available too, both with TCP and HTTP checks. |
| 479 | |
| 480 | Proxies using the "monitor-net" keyword can remove the "option dontlognull", as |
| 481 | it will make them log empty connections from hosts outside the monitoring |
| 482 | network. |
| 483 | |
| 484 | Example : |
| 485 | --------- |
| 486 | |
| 487 | listen tse-proxy |
| 488 | bind :3389,:1494,:5900 # TSE, ICA and VNC at once. |
| 489 | mode tcp |
| 490 | balance roundrobin |
| 491 | server tse-farm 192.168.1.10 |
| 492 | monitor-net 192.168.1.252/31 # L4 load-balancers on .252 and .253 |
| 493 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 494 | |
| 495 | 2.3) Limiting the number of simultaneous connections |
| 496 | ---------------------------------------------------- |
| 497 | The 'maxconn' parameter allows a proxy to refuse connections above a certain |
| 498 | amount of simultaneous ones. When the limit is reached, it simply stops |
| 499 | listening, but the system may still be accepting them because of the back log |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 500 | queue. These connections will be processed later when other ones have freed |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 501 | some slots. This provides a serialization effect which helps very fragile |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 502 | servers resist to high loads. See further for system limitations. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 503 | |
| 504 | Example : |
| 505 | --------- |
| 506 | listen tiny_server 0.0.0.0:80 |
| 507 | maxconn 10 |
| 508 | |
| 509 | |
| 510 | 2.4) Soft stop |
| 511 | -------------- |
| 512 | It is possible to stop services without breaking existing connections by the |
willy tarreau | 22739ef | 2006-01-20 20:43:32 +0100 | [diff] [blame] | 513 | sending of the SIGUSR1 signal to the process. All services are then put into |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 514 | soft-stop state, which means that they will refuse to accept new connections, |
| 515 | except for those which have a non-zero value in the 'grace' parameter, in which |
| 516 | case they will still accept connections for the specified amount of time, in |
willy tarreau | 22739ef | 2006-01-20 20:43:32 +0100 | [diff] [blame] | 517 | milliseconds. This makes it possible to tell a load-balancer that the service |
| 518 | is failing, while still doing the job during the time it needs to detect it. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 519 | |
| 520 | Note: active connections are never killed. In the worst case, the user will have |
| 521 | to wait for all of them to close or to time-out, or simply kill the process |
willy tarreau | 22739ef | 2006-01-20 20:43:32 +0100 | [diff] [blame] | 522 | normally (SIGTERM). The default 'grace' value is '0'. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 523 | |
| 524 | Example : |
| 525 | --------- |
| 526 | # enter soft stop after 'killall -USR1 haproxy' |
| 527 | # the service will still run 10 seconds after the signal |
| 528 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 529 | mode http |
| 530 | grace 10000 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 531 | |
| 532 | # this port is dedicated to a load-balancer, and must fail immediately |
| 533 | listen health_check 0.0.0.0:60000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 534 | mode health |
| 535 | grace 0 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 536 | |
| 537 | |
willy tarreau | 39df2dc | 2006-01-29 21:56:05 +0100 | [diff] [blame] | 538 | As of version 1.2.8, a new soft-reconfiguration mechanism has been introduced. |
willy tarreau | 22739ef | 2006-01-20 20:43:32 +0100 | [diff] [blame] | 539 | It is now possible to "pause" all the proxies by sending a SIGTTOU signal to |
| 540 | the processes. This will disable the listening socket without breaking existing |
| 541 | connections. After that, sending a SIGTTIN signal to those processes enables |
| 542 | the listening sockets again. This is very useful to try to load a new |
| 543 | configuration or even a new version of haproxy without breaking existing |
| 544 | connections. If the load succeeds, then simply send a SIGUSR1 which will make |
| 545 | the previous proxies exit immediately once their sessions are closed ; and if |
| 546 | the load fails, then simply send a SIGTTIN to restore the service immediately. |
| 547 | Please note that the 'grace' parameter is ignored for SIGTTOU, as well as for |
| 548 | SIGUSR1 when the process was in the pause mode. Please also note that it would |
| 549 | be useful to save the pidfile before starting a new instance. |
| 550 | |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 551 | This mechanism fully exploited since 1.2.11 with the '-st' and '-sf' options |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 552 | (see below). |
| 553 | |
| 554 | 2.4.1) Hot reconfiguration |
| 555 | -------------------------- |
| 556 | The '-st' and '-sf' command line options are used to inform previously running |
| 557 | processes that a configuration is being reloaded. They will receive the SIGTTOU |
| 558 | signal to ask them to temporarily stop listening to the ports so that the new |
| 559 | process can grab them. If anything wrong happens, the new process will send |
| 560 | them a SIGTTIN to tell them to re-listen to the ports and continue their normal |
| 561 | work. Otherwise, it will either ask them to finish (-sf) their work then softly |
| 562 | exit, or immediately terminate (-st), breaking existing sessions. A typical use |
| 563 | of this allows a configuration reload without service interruption : |
| 564 | |
| 565 | # haproxy -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid) |
| 566 | |
willy tarreau | 22739ef | 2006-01-20 20:43:32 +0100 | [diff] [blame] | 567 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 568 | 2.5) Connections expiration time |
| 569 | -------------------------------- |
| 570 | It is possible (and recommended) to configure several time-outs on TCP |
| 571 | connections. Three independant timers are adjustable with values specified |
| 572 | in milliseconds. A session will be terminated if either one of these timers |
| 573 | expire. |
| 574 | |
| 575 | - the time we accept to wait for data from the client, or for the client to |
| 576 | accept data : 'clitimeout' : |
| 577 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 578 | # client time-out set to 2mn30. |
| 579 | clitimeout 150000 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 580 | |
| 581 | - the time we accept to wait for data from the server, or for the server to |
| 582 | accept data : 'srvtimeout' : |
| 583 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 584 | # server time-out set to 30s. |
| 585 | srvtimeout 30000 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 586 | |
| 587 | - the time we accept to wait for a connection to establish on a server : |
| 588 | 'contimeout' : |
| 589 | |
| 590 | # we give up if the connection does not complete within 4 seconds |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 591 | contimeout 4000 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 592 | |
| 593 | Notes : |
| 594 | ------- |
| 595 | - 'contimeout' and 'srvtimeout' have no sense on 'health' mode servers ; |
| 596 | - under high loads, or with a saturated or defective network, it's possible |
| 597 | that some packets get lost. Since the first TCP retransmit only happens |
| 598 | after 3 seconds, a time-out equal to, or lower than 3 seconds cannot |
| 599 | compensate for a packet loss. A 4 seconds time-out seems a reasonable |
| 600 | minimum which will considerably reduce connection failures. |
| 601 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 602 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 603 | 2.6) Attempts to reconnect |
| 604 | -------------------------- |
| 605 | After a connection failure to a server, it is possible to retry, potentially |
| 606 | on another server. This is useful if health-checks are too rare and you don't |
| 607 | want the clients to see the failures. The number of attempts to reconnect is |
| 608 | set by the 'retries' paramter. |
| 609 | |
| 610 | Example : |
| 611 | --------- |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 612 | # we can retry 3 times max after a failure |
| 613 | retries 3 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 614 | |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 615 | Please note that the reconnection attempt may lead to getting the connection |
| 616 | sent to a new server if the original one died between connection attempts. |
| 617 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 618 | |
| 619 | 2.7) Address of the dispatch server (deprecated) |
| 620 | ------------------------------------------------ |
| 621 | The server which will be sent all new connections is defined by the 'dispatch' |
| 622 | parameter, in the form <address>:<port>. It generally is dedicated to unknown |
| 623 | connections and will assign them a cookie, in case of HTTP persistence mode, |
| 624 | or simply is a single server in case of generic TCP proxy. This old mode is only |
| 625 | provided for backwards compatibility, but doesn't allow to check remote servers |
| 626 | state, and has a rather limited usage. All new setups should switch to 'balance' |
| 627 | mode. The principle of the dispatcher is to be able to perform the load |
| 628 | balancing itself, but work only on new clients so that the server doesn't need |
| 629 | to be a big machine. |
| 630 | |
| 631 | Example : |
| 632 | --------- |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 633 | # all new connections go there |
| 634 | dispatch 192.168.1.2:80 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 635 | |
| 636 | Note : |
| 637 | ------ |
| 638 | This parameter has no sense for 'health' servers, and is incompatible with |
| 639 | 'balance' mode. |
| 640 | |
| 641 | |
| 642 | 2.8) Outgoing source address |
| 643 | ---------------------------- |
| 644 | It is often necessary to bind to a particular address when connecting to some |
| 645 | remote hosts. This is done via the 'source' parameter which is a per-proxy |
| 646 | parameter. A newer version may allow to fix different sources to reach different |
| 647 | servers. The syntax is 'source <address>[:<port>]', where <address> is a valid |
| 648 | local address (or '0.0.0.0' or '*' or empty to let the system choose), and |
| 649 | <port> is an optional parameter allowing the user to force the source port for |
| 650 | very specific needs. If the port is not specified or is '0', the system will |
| 651 | choose a free port. Note that as of version 1.1.18, the servers health checks |
| 652 | are also performed from the same source. |
| 653 | |
| 654 | Examples : |
| 655 | ---------- |
| 656 | listen http_proxy *:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 657 | # all connections take 192.168.1.200 as source address |
| 658 | source 192.168.1.200:0 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 659 | |
| 660 | listen rlogin_proxy *:513 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 661 | # use address 192.168.1.200 and the reserved port 900 (needs to be root) |
| 662 | source 192.168.1.200:900 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 663 | |
| 664 | |
| 665 | 2.9) Setting the cookie name |
| 666 | ---------------------------- |
| 667 | In HTTP mode, it is possible to look for a particular cookie which will contain |
| 668 | a server identifier which should handle the connection. The cookie name is set |
| 669 | via the 'cookie' parameter. |
| 670 | |
| 671 | Example : |
| 672 | --------- |
| 673 | listen http_proxy :80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 674 | mode http |
| 675 | cookie SERVERID |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 676 | |
| 677 | It is possible to change the cookie behaviour to get a smarter persistence, |
| 678 | depending on applications. It is notably possible to delete or modify a cookie |
| 679 | emitted by a server, insert a cookie identifying the server in an HTTP response |
| 680 | and even add a header to tell upstream caches not to cache this response. |
| 681 | |
| 682 | Examples : |
| 683 | ---------- |
| 684 | |
| 685 | To remove the cookie for direct accesses (ie when the server matches the one |
| 686 | which was specified in the client cookie) : |
| 687 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 688 | cookie SERVERID indirect |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 689 | |
| 690 | To replace the cookie value with the one assigned to the server if any (no |
| 691 | cookie will be created if the server does not provide one, nor if the |
| 692 | configuration does not provide one). This lets the application put the cookie |
| 693 | exactly on certain pages (eg: successful authentication) : |
| 694 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 695 | cookie SERVERID rewrite |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 696 | |
| 697 | To create a new cookie and assign the server identifier to it (in this case, all |
| 698 | servers should be associated with a valid cookie, since no cookie will simply |
| 699 | delete the cookie from the client's browser) : |
| 700 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 701 | cookie SERVERID insert |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 702 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 703 | To reuse an existing application cookie and prefix it with the server's |
| 704 | identifier, and remove it in the request, use the 'prefix' option. This allows |
| 705 | to insert a haproxy in front of an application without risking to break clients |
| 706 | which does not support more than one cookie : |
| 707 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 708 | cookie JSESSIONID prefix |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 709 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 710 | To insert a cookie and ensure that no upstream cache will store it, add the |
| 711 | 'nocache' option : |
| 712 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 713 | cookie SERVERID insert nocache |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 714 | |
| 715 | To insert a cookie only after a POST request, add 'postonly' after 'insert'. |
| 716 | This has the advantage that there's no risk of caching, and that all pages |
| 717 | seen before the POST one can still be cached : |
| 718 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 719 | cookie SERVERID insert postonly |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 720 | |
| 721 | Notes : |
| 722 | ----------- |
| 723 | - it is possible to combine 'insert' with 'indirect' or 'rewrite' to adapt to |
| 724 | applications which already generate the cookie with an invalid content. |
| 725 | |
| 726 | - in the case where 'insert' and 'indirect' are both specified, the cookie is |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 727 | never transmitted to the server, since it wouldn't understand it. This is the |
| 728 | most application-transparent mode. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 729 | |
| 730 | - it is particularly recommended to use 'nocache' in 'insert' mode if any |
| 731 | upstream HTTP/1.0 cache is susceptible to cache the result, because this may |
| 732 | lead to many clients going to the same server, or even worse, some clients |
| 733 | having their server changed while retrieving a page from the cache. |
| 734 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 735 | - the 'prefix' mode normally does not need 'indirect', 'nocache', nor |
| 736 | 'postonly', because just as in the 'rewrite' mode, it relies on the |
| 737 | application to know when a cookie can be emitted. However, since it has to |
| 738 | fix the cookie name in every subsequent requests, you must ensure that the |
| 739 | proxy will be used without any "HTTP keep-alive". Use option "httpclose" if |
| 740 | unsure. |
| 741 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 742 | - when the application is well known and controlled, the best method is to |
| 743 | only add the persistence cookie on a POST form because it's up to the |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 744 | application to select which page it wants the upstream servers to cache. In |
| 745 | this case, you would use 'insert postonly indirect'. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 746 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 747 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 748 | 2.10) Associating a cookie value with a server |
| 749 | ---------------------------------------------- |
| 750 | In HTTP mode, it's possible to associate a cookie value to each server. This |
| 751 | was initially used in combination with 'dispatch' mode to handle direct accesses |
| 752 | but it is now the standard way of doing the load balancing. The syntax is : |
| 753 | |
| 754 | server <identifier> <address>:<port> cookie <value> |
| 755 | |
| 756 | - <identifier> is any name which can be used to identify the server in the logs. |
| 757 | - <address>:<port> specifies where the server is bound. |
| 758 | - <value> is the value to put in or to read from the cookie. |
| 759 | |
| 760 | Example : the 'SERVERID' cookie can be either 'server01' or 'server02' |
| 761 | --------- |
| 762 | listen http_proxy :80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 763 | mode http |
| 764 | cookie SERVERID |
| 765 | dispatch 192.168.1.100:80 |
| 766 | server web1 192.168.1.1:80 cookie server01 |
| 767 | server web2 192.168.1.2:80 cookie server02 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 768 | |
| 769 | Warning : the syntax has changed since version 1.0 ! |
| 770 | --------- |
| 771 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 772 | |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 773 | 2.11) Application Cookies |
| 774 | ------------------------- |
| 775 | Since 1.2.4 it is possible to catch the cookie that comes from an |
| 776 | application server in order to apply "application session stickyness". |
| 777 | The server's response is searched for 'appsession' cookie, the first |
| 778 | 'len' bytes are used for matching and it is stored for a period of |
| 779 | 'timeout'. |
| 780 | The syntax is: |
| 781 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 782 | appsession <session_cookie> len <match_length> timeout <holdtime> |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 783 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 784 | - <session_cookie> is the cookie, the server uses for it's session-handling |
| 785 | - <match_length> how many bytes/characters should be used for matching equal |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 786 | sessions |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 787 | - <holdtime> after this inactivaty time, in ms, the cookie will be deleted |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 788 | from the sessionstore |
| 789 | |
| 790 | The appsession is only per 'listen' section possible. |
| 791 | |
| 792 | Example : |
| 793 | --------- |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 794 | listen http_lb1 192.168.3.4:80 |
| 795 | mode http |
| 796 | capture request header Cookie len 200 |
| 797 | # Havind a ServerID cookie on the client allows him to reach |
| 798 | # the right server even after expiration of the appsession. |
| 799 | cookie ServerID insert nocache indirect |
| 800 | # Will memorize 52 bytes of the cookie 'JSESSIONID' and keep them |
| 801 | # for 3 hours. It will match it in the cookie and the URL field. |
| 802 | appsession JSESSIONID len 52 timeout 10800000 |
| 803 | server first1 10.3.9.2:10805 check inter 3000 cookie first |
| 804 | server secon1 10.3.9.3:10805 check inter 3000 cookie secon |
| 805 | server first1 10.3.9.4:10805 check inter 3000 cookie first |
| 806 | server secon2 10.3.9.5:10805 check inter 3000 cookie secon |
| 807 | option httpchk GET /test.jsp |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 808 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 809 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 810 | 3) Autonomous load balancer |
| 811 | =========================== |
| 812 | |
| 813 | The proxy can perform the load-balancing itself, both in TCP and in HTTP modes. |
| 814 | This is the most interesting mode which obsoletes the old 'dispatch' mode |
| 815 | described above. It has advantages such as server health monitoring, multiple |
| 816 | port binding and port mapping. To use this mode, the 'balance' keyword is used, |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 817 | followed by the selected algorithm. Up to version 1.2.11, only 'roundrobin' was |
| 818 | available, which is also the default value if unspecified. Starting with |
| 819 | version 1.2.12, a new 'source' keyword appeared. In this mode, there will be no |
| 820 | dispatch address, but the proxy needs at least one server. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 821 | |
| 822 | Example : same as the last one, with internal load balancer |
| 823 | --------- |
| 824 | |
| 825 | listen http_proxy :80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 826 | mode http |
| 827 | cookie SERVERID |
| 828 | balance roundrobin |
| 829 | server web1 192.168.1.1:80 cookie server01 |
| 830 | server web2 192.168.1.2:80 cookie server02 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 831 | |
| 832 | |
| 833 | Since version 1.1.22, it is possible to automatically determine on which port |
| 834 | the server will get the connection, depending on the port the client connected |
| 835 | to. Indeed, there now are 4 possible combinations for the server's <port> field: |
| 836 | |
| 837 | - unspecified or '0' : |
| 838 | the connection will be sent to the same port as the one on which the proxy |
| 839 | received the client connection itself. |
| 840 | |
| 841 | - numerical value (the only one supported in versions earlier than 1.1.22) : |
| 842 | the connection will always be sent to the specified port. |
| 843 | |
| 844 | - '+' followed by a numerical value : |
| 845 | the connection will be sent to the same port as the one on which the proxy |
| 846 | received the connection, plus this value. |
| 847 | |
| 848 | - '-' followed by a numerical value : |
| 849 | the connection will be sent to the same port as the one on which the proxy |
| 850 | received the connection, minus this value. |
| 851 | |
| 852 | Examples : |
| 853 | ---------- |
| 854 | |
| 855 | # same as previous example |
| 856 | |
| 857 | listen http_proxy :80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 858 | mode http |
| 859 | cookie SERVERID |
| 860 | balance roundrobin |
| 861 | server web1 192.168.1.1 cookie server01 |
| 862 | server web2 192.168.1.2 cookie server02 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 863 | |
| 864 | # simultaneous relaying of ports 80, 81 and 8080-8089 |
| 865 | |
| 866 | listen http_proxy :80,:81,:8080-8089 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 867 | mode http |
| 868 | cookie SERVERID |
| 869 | balance roundrobin |
| 870 | server web1 192.168.1.1 cookie server01 |
| 871 | server web2 192.168.1.2 cookie server02 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 872 | |
| 873 | # relaying of TCP ports 25, 389 and 663 to ports 1025, 1389 and 1663 |
| 874 | |
| 875 | listen http_proxy :25,:389,:663 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 876 | mode tcp |
| 877 | balance roundrobin |
| 878 | server srv1 192.168.1.1:+1000 |
| 879 | server srv2 192.168.1.2:+1000 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 880 | |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 881 | As previously stated, version 1.2.12 brought the 'source' keyword. When this |
| 882 | keyword is used, the client's IP address is hashed and evenly distributed among |
| 883 | the available servers so that a same source IP will always go to the same |
| 884 | server as long as there are no change in the number of available servers. This |
| 885 | can be used for instance to bind HTTP and HTTPS to the same server. It can also |
| 886 | be used to improve stickyness when one part of the client population does not |
| 887 | accept cookies. In this case, only those ones will be perturbated should a |
| 888 | server fail. |
| 889 | |
| 890 | NOTE: It is important to consider the fact that many clients surf the net |
| 891 | through proxy farms which assign different IP addresses for each |
| 892 | request. Others use dialup connections with a different IP at each |
| 893 | connection. Thus, the 'source' parameter should be used with extreme |
| 894 | care. |
| 895 | |
| 896 | Examples : |
| 897 | ---------- |
| 898 | |
| 899 | # make a same IP go to the same server whatever the service |
| 900 | |
| 901 | listen http_proxy |
| 902 | bind :80,:443 |
| 903 | mode http |
| 904 | balance source |
| 905 | server web1 192.168.1.1 |
| 906 | server web2 192.168.1.2 |
| 907 | |
| 908 | # try to improve client-server binding by using both source IP and cookie : |
| 909 | |
| 910 | listen http_proxy :80 |
| 911 | mode http |
| 912 | cookie SERVERID |
| 913 | balance source |
| 914 | server web1 192.168.1.1 cookie server01 |
| 915 | server web2 192.168.1.2 cookie server02 |
| 916 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 917 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 918 | 3.1) Server monitoring |
| 919 | ---------------------- |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 920 | It is possible to check the servers status by trying to establish TCP |
| 921 | connections or even sending HTTP requests to them. A server which fails to |
| 922 | reply to health checks as expected will not be used by the load balancing |
| 923 | algorithms. To enable monitoring, add the 'check' keyword on a server line. |
| 924 | It is possible to specify the interval between tests (in milliseconds) with |
| 925 | the 'inter' parameter, the number of failures supported before declaring that |
| 926 | the server has fallen down with the 'fall' parameter, and the number of valid |
| 927 | checks needed for the server to fully get up with the 'rise' parameter. Since |
| 928 | version 1.1.22, it is also possible to send checks to a different port |
| 929 | (mandatory when none is specified) with the 'port' parameter. The default |
| 930 | values are the following ones : |
| 931 | |
| 932 | - inter : 2000 |
| 933 | - rise : 2 |
| 934 | - fall : 3 |
| 935 | - port : default server port |
| 936 | |
| 937 | The default mode consists in establishing TCP connections only. But in certain |
| 938 | types of application failures, it is often that the server continues to accept |
| 939 | connections because the system does it itself while the application is running |
| 940 | an endless loop, or is completely stuck. So in version 1.1.16 were introduced |
| 941 | HTTP health checks which only performed simple lightweight requests and analysed |
| 942 | the response. Now, as of version 1.1.23, it is possible to change the HTTP |
| 943 | method, the URI, and the HTTP version string (which even allows to send headers |
| 944 | with a dirty trick). To enable HTTP health-checks, use 'option httpchk'. |
| 945 | |
| 946 | By default, requests use the 'OPTIONS' method because it's very light and easy |
| 947 | to filter from logs, and does it on '/'. Only HTTP responses 2xx and 3xx are |
| 948 | considered valid ones, and only if they come before the time to send a new |
| 949 | request is reached ('inter' parameter). If some servers block this type of |
| 950 | request, 3 other forms help to forge a request : |
| 951 | |
| 952 | - option httpchk -> OPTIONS / HTTP/1.0 |
| 953 | - option httpchk URI -> OPTIONS <URI> HTTP/1.0 |
| 954 | - option httpchk METH URI -> <METH> <URI> HTTP/1.0 |
| 955 | - option httpchk METH URI VER -> <METH> <URI> <VER> |
| 956 | |
| 957 | See examples below. |
| 958 | |
| 959 | Since version 1.1.17, it is possible to specify backup servers. These servers |
| 960 | are only sollicited when no other server is available. This may only be useful |
| 961 | to serve a maintenance page, or define one active and one backup server (seldom |
| 962 | used in TCP mode). To make a server a backup one, simply add the 'backup' option |
| 963 | on its line. These servers also support cookies, so if a cookie is specified for |
| 964 | a backup server, clients assigned to this server will stick to it even when the |
| 965 | other ones come back. Conversely, if no cookie is assigned to such a server, |
| 966 | the clients will get their cookies removed (empty cookie = removal), and will |
| 967 | be balanced against other servers once they come back. Please note that there |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 968 | is no load-balancing among backup servers by default. If there are several |
| 969 | backup servers, the second one will only be used when the first one dies, and |
| 970 | so on. To force load-balancing between backup servers, specify the 'allbackups' |
| 971 | option. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 972 | |
| 973 | Since version 1.1.17, it is also possible to visually check the status of all |
| 974 | servers at once. For this, you just have to send a SIGHUP signal to the proxy. |
| 975 | The servers status will be dumped into the logs at the 'notice' level, as well |
| 976 | as on <stderr> if not closed. For this reason, it's always a good idea to have |
| 977 | one local log server at the 'notice' level. |
| 978 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 979 | Since version 1.1.28 and 1.2.1, if an instance loses all its servers, an |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 980 | emergency message will be sent in the logs to inform the administator that an |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 981 | immediate action must be taken. |
| 982 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 983 | Since version 1.1.30 and 1.2.3, several servers can share the same cookie |
| 984 | value. This is particularly useful in backup mode, to select alternate paths |
| 985 | for a given server for example, to provide soft-stop, or to direct the clients |
| 986 | to a temporary page during an application restart. The principle is that when |
| 987 | a server is dead, the proxy will first look for another server which shares the |
| 988 | same cookie value for every client which presents the cookie. If there is no |
| 989 | standard server for this cookie, it will then look for a backup server which |
| 990 | shares the same name. Please consult the architecture guide for more information. |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 991 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 992 | Examples : |
| 993 | ---------- |
| 994 | # same setup as in paragraph 3) with TCP monitoring |
| 995 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 996 | mode http |
| 997 | cookie SERVERID |
| 998 | balance roundrobin |
| 999 | server web1 192.168.1.1:80 cookie server01 check |
| 1000 | server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1001 | |
| 1002 | # same with HTTP monitoring via 'OPTIONS / HTTP/1.0' |
| 1003 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1004 | mode http |
| 1005 | cookie SERVERID |
| 1006 | balance roundrobin |
| 1007 | option httpchk |
| 1008 | server web1 192.168.1.1:80 cookie server01 check |
| 1009 | server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1010 | |
| 1011 | # same with HTTP monitoring via 'OPTIONS /index.html HTTP/1.0' |
| 1012 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1013 | mode http |
| 1014 | cookie SERVERID |
| 1015 | balance roundrobin |
| 1016 | option httpchk /index.html |
| 1017 | server web1 192.168.1.1:80 cookie server01 check |
| 1018 | server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1019 | |
| 1020 | # same with HTTP monitoring via 'HEAD /index.jsp? HTTP/1.1\r\nHost: www' |
| 1021 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1022 | mode http |
| 1023 | cookie SERVERID |
| 1024 | balance roundrobin |
| 1025 | option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www |
| 1026 | server web1 192.168.1.1:80 cookie server01 check |
| 1027 | server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1028 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1029 | # Load-balancing with 'prefixed cookie' persistence, and soft-stop using an |
| 1030 | # alternate port 81 on the server for health-checks. |
| 1031 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1032 | mode http |
| 1033 | cookie JSESSIONID prefix |
| 1034 | balance roundrobin |
| 1035 | option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www |
| 1036 | server web1-norm 192.168.1.1:80 cookie s1 check port 81 |
| 1037 | server web2-norm 192.168.1.2:80 cookie s2 check port 81 |
| 1038 | server web1-stop 192.168.1.1:80 cookie s1 check port 80 backup |
| 1039 | server web2-stop 192.168.1.2:80 cookie s2 check port 80 backup |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1040 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1041 | # automatic insertion of a cookie in the server's response, and automatic |
| 1042 | # deletion of the cookie in the client request, while asking upstream caches |
| 1043 | # not to cache replies. |
| 1044 | listen web_appl 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1045 | mode http |
| 1046 | cookie SERVERID insert nocache indirect |
| 1047 | balance roundrobin |
| 1048 | server web1 192.168.1.1:80 cookie server01 check |
| 1049 | server web2 192.168.1.2:80 cookie server02 check |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1050 | |
| 1051 | # same with off-site application backup and local error pages server |
| 1052 | listen web_appl 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1053 | mode http |
| 1054 | cookie SERVERID insert nocache indirect |
| 1055 | balance roundrobin |
| 1056 | server web1 192.168.1.1:80 cookie server01 check |
| 1057 | server web2 192.168.1.2:80 cookie server02 check |
| 1058 | server web-backup 192.168.2.1:80 cookie server03 check backup |
| 1059 | server web-excuse 192.168.3.1:80 check backup |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1060 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1061 | # SMTP+TLS relaying with health-checks and backup servers |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1062 | |
| 1063 | listen http_proxy :25,:587 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1064 | mode tcp |
| 1065 | balance roundrobin |
| 1066 | server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2 |
| 1067 | server srv2 192.168.1.2 backup |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1068 | |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 1069 | # Load-balancing using a backup pool (requires haproxy 1.2.9) |
| 1070 | listen http_proxy 0.0.0.0:80 |
| 1071 | mode http |
| 1072 | balance roundrobin |
| 1073 | option httpchk |
| 1074 | server inst1 192.168.1.1:80 cookie s1 check |
| 1075 | server inst2 192.168.1.2:80 cookie s2 check |
| 1076 | server inst3 192.168.1.3:80 cookie s3 check |
| 1077 | server back1 192.168.1.10:80 check backup |
| 1078 | server back2 192.168.1.11:80 check backup |
| 1079 | option allbackups # all backups will be used |
| 1080 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1081 | |
| 1082 | 3.2) Redistribute connections in case of failure |
| 1083 | ------------------------------------------------ |
| 1084 | In HTTP mode, if a server designated by a cookie does not respond, the clients |
| 1085 | may definitely stick to it because they cannot flush the cookie, so they will |
| 1086 | not be able to access the service anymore. Specifying 'redispatch' will allow |
| 1087 | the proxy to break their persistence and redistribute them to working servers. |
| 1088 | |
| 1089 | Example : |
| 1090 | --------- |
| 1091 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1092 | mode http |
| 1093 | cookie SERVERID |
| 1094 | dispatch 192.168.1.100:80 |
| 1095 | server web1 192.168.1.1:80 cookie server01 |
| 1096 | server web2 192.168.1.2:80 cookie server02 |
| 1097 | redispatch # send back to dispatch in case of connection failure |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1098 | |
| 1099 | Up to, and including version 1.1.16, this parameter only applied to connection |
| 1100 | failures. Since version 1.1.17, it also applies to servers which have been |
| 1101 | detected as failed by the health check mechanism. Indeed, a server may be broken |
| 1102 | but still accepting connections, which would not solve every case. But it is |
| 1103 | possible to conserve the old behaviour, that is, make a client insist on trying |
| 1104 | to connect to a server even if it is said to be down, by setting the 'persist' |
| 1105 | option : |
| 1106 | |
| 1107 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1108 | mode http |
| 1109 | option persist |
| 1110 | cookie SERVERID |
| 1111 | dispatch 192.168.1.100:80 |
| 1112 | server web1 192.168.1.1:80 cookie server01 |
| 1113 | server web2 192.168.1.2:80 cookie server02 |
| 1114 | redispatch # send back to dispatch in case of connection failure |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1115 | |
| 1116 | |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 1117 | 3.3) Assigning different weights to servers |
| 1118 | ------------------------------------------- |
| 1119 | Sometimes you will need to bring new servers to increase your server farm's |
| 1120 | capacity, but the new server will be either smaller (emergency use of anything |
| 1121 | that fits) or bigger (when investing in new hardware). For this reason, it |
| 1122 | might be wise to be able to send more clients to biggest servers. Till version |
| 1123 | 1.2.11, it was necessary to replicate the same server multiple times in the |
| 1124 | configuration. Starting with 1.2.12, the 'weight' option is available. HAProxy |
| 1125 | then computes the most homogenous possible map of servers based on their |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1126 | weights so that the load gets distributed as smoothly as possible among them. |
| 1127 | The weight, between 1 and 256, should reflect one server's capacity relative to |
| 1128 | others. Weight 1 represents the lowest frequency and 256 the highest. This way, |
| 1129 | if a server fails, the remaining capacities are still respected. |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 1130 | |
| 1131 | Example : |
| 1132 | --------- |
| 1133 | # fair distribution among two opterons and one old pentium3 |
| 1134 | |
| 1135 | listen web_appl 0.0.0.0:80 |
| 1136 | mode http |
| 1137 | cookie SERVERID insert nocache indirect |
| 1138 | balance roundrobin |
| 1139 | server pentium3-800 192.168.1.1:80 cookie server01 weight 8 check |
| 1140 | server opteron-2.0G 192.168.1.2:80 cookie server02 weight 20 check |
| 1141 | server opteron-2.4G 192.168.1.3:80 cookie server03 weight 24 check |
| 1142 | server web-backup1 192.168.2.1:80 cookie server04 check backup |
| 1143 | server web-excuse 192.168.3.1:80 check backup |
| 1144 | |
| 1145 | Notes : |
| 1146 | ------- |
| 1147 | - if unspecified, the default weight is 1 |
| 1148 | |
| 1149 | - the weight does not impact health checks, so it is cleaner to use weights |
| 1150 | than replicating the same server several times |
| 1151 | |
| 1152 | - weights also work on backup servers if the 'allbackups' option is used |
| 1153 | |
| 1154 | - the weights also apply to the source address load balancing |
| 1155 | ('balance source'). |
| 1156 | |
| 1157 | - whatever the weights, the first server will always be assigned first. This |
| 1158 | is helpful for troubleshooting. |
| 1159 | |
| 1160 | - for the purists, the map calculation algorithm gives precedence to first |
| 1161 | server, so the map is the most uniform when servers are declared in |
| 1162 | ascending order relative to their weights. |
| 1163 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1164 | The load distribution will follow exactly this sequence : |
| 1165 | |
| 1166 | Request| 1 1 1 1 |
| 1167 | number | 1 2 3 4 5 6 7 8 9 0 1 2 3 |
| 1168 | --------+--------------------------- |
| 1169 | p3-800 | X . . . . . . X . . . . . |
| 1170 | opt-20 | . X . X . X . . . X . X . |
| 1171 | opt-24 | . . X . X . X . X . X . X |
| 1172 | |
| 1173 | |
| 1174 | 3.4) Limiting the number of concurrent sessions on each server |
| 1175 | -------------------------------------------------------------- |
| 1176 | Some pre-forked servers such as Apache suffer from too many concurrent |
| 1177 | sessions, because it's very expensive to run hundreds or thousands of |
| 1178 | processes on one system. One solution is to increase the number of servers |
| 1179 | and load-balance between them, but it is a problem when the only goal is |
| 1180 | to resist to short surges. |
| 1181 | |
| 1182 | To solve this problem, a new feature was implemented in HAProxy 1.2.13. |
| 1183 | It's a per-server 'maxconn', associated with a per-server and a per-proxy |
| 1184 | queue. This transforms haproxy into a request buffer between the thousands of |
| 1185 | clients and the few servers. On many circumstances, lowering the maxconn value |
| 1186 | will increase the server's performance and decrease the overall response times |
| 1187 | because the servers will be less congested. |
| 1188 | |
| 1189 | When a request tries to reach any server, the first non-saturated server is |
| 1190 | used, respective to the load balancing algorithm. If all servers are saturated, |
| 1191 | then the request gets queued into the instance's global queue. It will be |
| 1192 | dequeued once a server will have freed a session and all previously queued |
| 1193 | requests have been processed. |
| 1194 | |
| 1195 | If a request references a particular server (eg: source hashing, or persistence |
| 1196 | cookie), and if this server is full, then the request will be queued into the |
| 1197 | server's dedicated queue. This queue has higher priority than the global queue, |
| 1198 | so it's easier for already registered users to enter the site than for new |
| 1199 | users. |
| 1200 | |
| 1201 | For this, the logs have been enhanced to show the number of sessions per |
| 1202 | server, the request's position in the queue and the time spent in the queue. |
| 1203 | This helps doing capacity planning. See the 'logs' section below for more info. |
| 1204 | |
| 1205 | Example : |
| 1206 | --------- |
| 1207 | # be nice with P3 which only has 256 MB of RAM. |
| 1208 | listen web_appl 0.0.0.0:80 |
| 1209 | maxconn 10000 |
| 1210 | mode http |
| 1211 | cookie SERVERID insert nocache indirect |
| 1212 | balance roundrobin |
| 1213 | server pentium3-800 192.168.1.1:80 cookie s1 weight 8 maxconn 100 check |
| 1214 | server opteron-2.0G 192.168.1.2:80 cookie s2 weight 20 maxconn 300 check |
| 1215 | server opteron-2.4G 192.168.1.3:80 cookie s3 weight 24 maxconn 300 check |
| 1216 | server web-backup1 192.168.2.1:80 cookie s4 check maxconn 200 backup |
| 1217 | server web-excuse 192.168.3.1:80 check backup |
| 1218 | |
| 1219 | Notes : |
| 1220 | ------- |
| 1221 | - The requests will not stay indefinitely in the queue, they follow the |
| 1222 | 'contimeout' parameter, and if a request cannot be dequeued within this |
| 1223 | timeout because the server is saturated or because the queue is filled, |
| 1224 | the session will expire with a 503 error. |
| 1225 | |
| 1226 | - setting too low values for maxconn might improve performance but might also |
| 1227 | allow slow users to block access to the server for other users. |
| 1228 | |
willy tarreau | 34f4530 | 2006-04-15 21:37:14 +0200 | [diff] [blame] | 1229 | |
willy tarreau | e0bdd62 | 2006-05-21 20:51:54 +0200 | [diff] [blame^] | 1230 | 3.5) Dropping aborted requests |
| 1231 | ------------------------------ |
| 1232 | In presence of very high loads, the servers will take some time to respond. The |
| 1233 | per-proxy's connection queue will inflate, and the response time will increase |
| 1234 | respective to the size of the queue times the average per-session response |
| 1235 | time. When clients will wait for more than a few seconds, they will often hit |
| 1236 | the 'STOP' button on their browser, leaving a useless request in the queue, and |
| 1237 | slowing down other users. |
| 1238 | |
| 1239 | As there is no way to distinguish between a full STOP and a simple |
| 1240 | shutdown(SHUT_WR) on the client side, HTTP agents should be conservative and |
| 1241 | consider that the client might only have closed its output channel while |
| 1242 | waiting for the response. However, this introduces risks of congestion when |
| 1243 | lots of users do the same, and is completely useless nowadays because probably |
| 1244 | no client at all will close the session while waiting for the response. Some |
| 1245 | HTTP agents support this (Squid, Apache, HAProxy), and others do not (TUX, most |
| 1246 | hardware-based load balancers). So the probability for a closed input channel |
| 1247 | to represent a user hitting the 'STOP' button is close to 100%, and it is very |
| 1248 | tempting to be able to abort the session early without polluting the servers. |
| 1249 | |
| 1250 | For this reason, a new option "abortonclose" was introduced in version 1.2.14. |
| 1251 | By default (without the option) the behaviour is HTTP-compliant. But when the |
| 1252 | option is specified, a session with an incoming channel closed will be aborted |
| 1253 | if it's still possible, which means that it's either waiting for a connect() to |
| 1254 | establish or it is queued waiting for a connection slot. This considerably |
| 1255 | reduces the queue size and the load on saturated servers when users are tempted |
| 1256 | to click on STOP, which in turn reduces the response time for other users. |
| 1257 | |
| 1258 | Example : |
| 1259 | --------- |
| 1260 | listen web_appl 0.0.0.0:80 |
| 1261 | maxconn 10000 |
| 1262 | mode http |
| 1263 | cookie SERVERID insert nocache indirect |
| 1264 | balance roundrobin |
| 1265 | server web1 192.168.1.1:80 cookie s1 weight 10 maxconn 100 check |
| 1266 | server web2 192.168.1.2:80 cookie s2 weight 10 maxconn 100 check |
| 1267 | server web3 192.168.1.3:80 cookie s3 weight 10 maxconn 100 check |
| 1268 | server bck1 192.168.2.1:80 cookie s4 check maxconn 200 backup |
| 1269 | option abortonclose |
| 1270 | |
| 1271 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1272 | 4) Additionnal features |
| 1273 | ======================= |
| 1274 | |
| 1275 | Other features are available. They are transparent mode, event logging and |
| 1276 | header rewriting/filtering. |
| 1277 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1278 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1279 | 4.1) Network features |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1280 | --------------------- |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1281 | 4.1.1) Transparent mode |
| 1282 | ----------------------- |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1283 | In HTTP mode, the 'transparent' keyword allows to intercept sessions which are |
| 1284 | routed through the system hosting the proxy. This mode was implemented as a |
| 1285 | replacement for the 'dispatch' mode, since connections without cookie will be |
| 1286 | sent to the original address while known cookies will be sent to the servers. |
| 1287 | This mode implies that the system can redirect sessions to a local port. |
| 1288 | |
| 1289 | Example : |
| 1290 | --------- |
| 1291 | listen http_proxy 0.0.0.0:65000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1292 | mode http |
| 1293 | transparent |
| 1294 | cookie SERVERID |
| 1295 | server server01 192.168.1.1:80 |
| 1296 | server server02 192.168.1.2:80 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1297 | |
| 1298 | # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \ |
| 1299 | --dport 80 -j REDIRECT --to-ports 65000 |
| 1300 | |
| 1301 | Note : |
| 1302 | ------ |
| 1303 | If the port is left unspecified on the server, the port the client connected to |
| 1304 | will be used. This allows to relay a full port range without using transparent |
| 1305 | mode nor thousands of file descriptors, provided that the system can redirect |
| 1306 | sessions to local ports. |
| 1307 | |
| 1308 | Example : |
| 1309 | --------- |
| 1310 | # redirect all ports to local port 65000, then forward to the server on the |
| 1311 | # original port. |
| 1312 | listen http_proxy 0.0.0.0:65000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1313 | mode tcp |
| 1314 | server server01 192.168.1.1 check port 60000 |
| 1315 | server server02 192.168.1.2 check port 60000 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1316 | |
| 1317 | # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \ |
| 1318 | -j REDIRECT --to-ports 65000 |
| 1319 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1320 | 4.1.2) Per-server source address binding |
| 1321 | ---------------------------------------- |
| 1322 | As of versions 1.1.30 and 1.2.3, it is possible to specify a particular source |
| 1323 | to reach each server. This is useful when reaching backup servers from a |
| 1324 | different LAN, or to use an alternate path to reach the same server. It is also |
| 1325 | usable to provide source load-balancing for outgoing connections. Obviously, |
| 1326 | the same source address is used to send health-checks. |
| 1327 | |
| 1328 | Example : |
| 1329 | --------- |
| 1330 | # use a particular source to reach both servers |
| 1331 | listen http_proxy 0.0.0.0:65000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1332 | mode http |
| 1333 | balance roundrobin |
| 1334 | server server01 192.168.1.1:80 source 192.168.2.13 |
| 1335 | server server02 192.168.1.2:80 source 192.168.2.13 |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1336 | |
| 1337 | Example : |
| 1338 | --------- |
| 1339 | # use a particular source to reach each servers |
| 1340 | listen http_proxy 0.0.0.0:65000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1341 | mode http |
| 1342 | balance roundrobin |
| 1343 | server server01 192.168.1.1:80 source 192.168.1.1 |
| 1344 | server server02 192.168.2.1:80 source 192.168.2.1 |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1345 | |
| 1346 | Example : |
| 1347 | --------- |
| 1348 | # provide source load-balancing to reach the same proxy through 2 WAN links |
| 1349 | listen http_proxy 0.0.0.0:65000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1350 | mode http |
| 1351 | balance roundrobin |
| 1352 | server remote-proxy-way1 192.168.1.1:3128 source 192.168.2.1 |
| 1353 | server remote-proxy-way2 192.168.1.1:3128 source 192.168.3.1 |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1354 | |
| 1355 | Example : |
| 1356 | --------- |
| 1357 | # force a TCP connection to bind to a specific port |
| 1358 | listen http_proxy 0.0.0.0:2000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1359 | mode tcp |
| 1360 | balance roundrobin |
| 1361 | server srv1 192.168.1.1:80 source 192.168.2.1:20 |
| 1362 | server srv2 192.168.1.2:80 source 192.168.2.1:20 |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1363 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1364 | 4.1.3) TCP keep-alive |
| 1365 | --------------------- |
| 1366 | With version 1.2.7, it becomes possible to enable TCP keep-alives on both the |
| 1367 | client and server sides. This makes it possible to prevent long sessions from |
| 1368 | expiring on external layer 4 components such as firewalls and load-balancers. |
| 1369 | It also allows the system to terminate dead sessions when no timeout has been |
| 1370 | set (not recommanded). The proxy cannot set the keep-alive probes intervals nor |
| 1371 | maximal count, consult your operating system manual for this. There are 3 |
| 1372 | options to enable TCP keep-alive : |
| 1373 | |
| 1374 | option tcpka # enables keep-alive both on client and server side |
| 1375 | option clitcpka # enables keep-alive only on client side |
| 1376 | option srvtcpka # enables keep-alive only on server side |
| 1377 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1378 | |
| 1379 | 4.2) Event logging |
| 1380 | ------------------ |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1381 | |
| 1382 | HAProxy's strength certainly lies in its precise logs. It probably provides the |
| 1383 | finest level of information available for such a product, which is very |
| 1384 | important for troubleshooting complex environments. Standard log information |
| 1385 | include client ports, TCP/HTTP state timers, precise session state at |
| 1386 | termination and precise termination cause, information about decisions to |
| 1387 | direct trafic to a server, and of course the ability to capture arbitrary |
| 1388 | headers. |
| 1389 | |
| 1390 | In order to improve administrators reactivity, it offers a great transparency |
| 1391 | about encountered problems, both internal and external, and it is possible to |
| 1392 | send logs to different sources at the same time with different level filters : |
| 1393 | |
| 1394 | - global process-level logs (system errors, start/stop, etc..) |
| 1395 | - per-listener system and internal errors (lack of resource, bugs, ...) |
| 1396 | - per-listener external troubles (servers up/down, max connections) |
| 1397 | - per-listener activity (client connections), either at the establishment or |
| 1398 | at the termination. |
| 1399 | |
| 1400 | The ability to distribute different levels of logs to different log servers |
| 1401 | allow several production teams to interact and to fix their problems as soon |
| 1402 | as possible. For example, the system team might monitor system-wide errors, |
| 1403 | while the application team might be monitoring the up/down for their servers in |
| 1404 | real time, and the security team might analyze the activity logs with one hour |
| 1405 | delay. |
| 1406 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1407 | 4.2.1) Log levels |
| 1408 | ----------------- |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1409 | TCP and HTTP connections can be logged with informations such as date, time, |
| 1410 | source IP address, destination address, connection duration, response times, |
| 1411 | HTTP request, the HTTP return code, number of bytes transmitted, the conditions |
| 1412 | in which the session ended, and even exchanged cookies values, to track a |
| 1413 | particular user's problems for example. All messages are sent to up to two |
| 1414 | syslog servers. Consult section 1.1 for more info about log facilities. The |
| 1415 | syntax follows : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1416 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1417 | log <address_1> <facility_1> [max_level_1] |
| 1418 | log <address_2> <facility_2> [max_level_2] |
| 1419 | or |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1420 | log global |
| 1421 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1422 | Note : |
| 1423 | ------ |
| 1424 | The particular syntax 'log global' means that the same log configuration as the |
| 1425 | 'global' section will be used. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1426 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1427 | Example : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1428 | --------- |
| 1429 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1430 | mode http |
| 1431 | log 192.168.2.200 local3 |
| 1432 | log 192.168.2.201 local4 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1433 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1434 | 4.2.2) Log format |
| 1435 | ----------------- |
| 1436 | By default, connections are logged at the TCP level, as soon as the session |
| 1437 | establishes between the client and the proxy. By enabling the 'tcplog' option, |
| 1438 | the proxy will wait until the session ends to generate an enhanced log |
| 1439 | containing more information such as session duration and its state during the |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1440 | disconnection. The number of remaining session after disconnection is also |
| 1441 | indicated (for the server, the listener, and the process). |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1442 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1443 | Example of TCP logging : |
| 1444 | ------------------------ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1445 | listen relais-tcp 0.0.0.0:8000 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1446 | mode tcp |
| 1447 | option tcplog |
| 1448 | log 192.168.2.200 local3 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1449 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1450 | >>> haproxy[18989]: 127.0.0.1:34550 [15/Oct/2003:15:24:28] relais-tcp Srv1 0/0/5007 0 -- 1/1/1 0/0 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1451 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1452 | Field Format Example |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1453 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1454 | 1 process_name '[' pid ']:' haproxy[18989]: |
| 1455 | 2 client_ip ':' client_port 127.0.0.1:34550 |
| 1456 | 3 '[' date ']' [15/Oct/2003:15:24:28] |
| 1457 | 4 listener_name relais-tcp |
| 1458 | 5 server_name Srv1 |
| 1459 | 6 queue_time '/' connect_time '/' total_time 0/0/5007 |
| 1460 | 7 bytes_read 0 |
| 1461 | 8 termination_state -- |
| 1462 | 9 srv_conn '/' listener_conn '/' process_conn 1/1/1 |
| 1463 | 10 position in srv_queue / listener_queue 0/0 |
| 1464 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1465 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1466 | Another option, 'httplog', provides more detailed information about HTTP |
| 1467 | contents, such as the request and some cookies. In the event where an external |
| 1468 | component would establish frequent connections to check the service, logs may be |
| 1469 | full of useless lines. So it is possible not to log any session which didn't |
| 1470 | transfer any data, by the setting of the 'dontlognull' option. This only has |
| 1471 | effect on sessions which are established then closed. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1472 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1473 | Example of HTTP logging : |
| 1474 | ------------------------- |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1475 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1476 | mode http |
| 1477 | option httplog |
| 1478 | option dontlognull |
| 1479 | log 192.168.2.200 local3 |
| 1480 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1481 | >>> haproxy[674]: 127.0.0.1:33319 [15/Oct/2003:08:31:57] relais-http Srv1 9/0/7/147/723 200 243 - - ---- 2/3/3 0/0 "HEAD / HTTP/1.0" |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1482 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1483 | More complete example |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1484 | haproxy[18989]: 10.0.0.1:34552 [15/Oct/2003:15:26:31] relais-http Srv1 3183/-1/-1/-1/11215 503 0 - - SC-- 137/202/205 0/0 {w.ods.org|Mozilla} {} "HEAD / HTTP/1.0" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1485 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1486 | Field Format Example |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1487 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1488 | 1 process_name '[' pid ']:' haproxy[18989]: |
| 1489 | 2 client_ip ':' client_port 10.0.0.1:34552 |
| 1490 | 3 '[' date ']' [15/Oct/2003:15:26:31] |
| 1491 | 4 listener_name relais-http |
| 1492 | 5 server_name Srv1 |
| 1493 | 6 Tq '/' Tw '/' Tc '/' Tr '/' Tt 3183/-1/-1/-1/11215 |
| 1494 | 7 HTTP_return_code 503 |
| 1495 | 8 bytes_read 0 |
| 1496 | 9 captured_request_cookie - |
| 1497 | 10 captured_response_cookie - |
| 1498 | 11 termination_state SC-- |
| 1499 | 12 srv_conn '/' listener_conn '/' process_conn 137/202/205 |
| 1500 | 13 position in srv_queue / listener_queue 0/0 |
| 1501 | 14 '{' captured_request_headers '}' {w.ods.org|Mozilla} |
| 1502 | 15 '{' captured_response_headers '}' {} |
| 1503 | 16 '"' HTTP_request '"' "HEAD / HTTP/1.0" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1504 | |
| 1505 | Note for log parsers: the URI is ALWAYS the end of the line starting with the |
| 1506 | first double quote '"'. |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1507 | |
| 1508 | The problem when logging at end of connection is that you have no clue about |
| 1509 | what is happening during very long sessions. To workaround this problem, a |
| 1510 | new option 'logasap' has been introduced in 1.1.28/1.2.1. When specified, the |
| 1511 | proxy will log as soon as possible, just before data transfer begins. This means |
| 1512 | that in case of TCP, it will still log the connection status to the server, and |
| 1513 | in case of HTTP, it will log just after processing the server headers. In this |
| 1514 | case, the number of bytes reported is the number of header bytes sent to the |
| 1515 | client. |
| 1516 | |
| 1517 | In order to avoid confusion with normal logs, the total time field and the |
| 1518 | number of bytes are prefixed with a '+' sign which mean that real numbers are |
| 1519 | certainly bigger. |
| 1520 | |
| 1521 | Example : |
| 1522 | --------- |
| 1523 | |
| 1524 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1525 | mode http |
| 1526 | option httplog |
| 1527 | option dontlognull |
| 1528 | option logasap |
| 1529 | log 192.168.2.200 local3 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1530 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1531 | >>> haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/10/7/14/+30 200 +243 - - ---- 1/1/3 1/0 "GET /image.iso HTTP/1.0" |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1532 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1533 | 4.2.3) Timing events |
| 1534 | -------------------- |
| 1535 | Timers provide a great help in trouble shooting network problems. All values |
| 1536 | are reported in milliseconds (ms). In HTTP mode, four control points are |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1537 | reported under the form 'Tq/Tw/Tc/Tr/Tt' : |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1538 | |
| 1539 | - Tq: total time to get the client request. |
| 1540 | It's the time elapsed between the moment the client connection was accepted |
| 1541 | and the moment the proxy received the last HTTP header. The value '-1' |
| 1542 | indicates that the end of headers (empty line) has never been seen. |
| 1543 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1544 | - Tw: total time spent in the queues waiting for a connection slot. It |
| 1545 | accounts for listener's queue as well as the server's queue, and depends |
| 1546 | on the queue size, and the time needed for the server to complete previous |
| 1547 | sessions. The value '-1' means that the request was killed before reaching |
| 1548 | the queue. |
| 1549 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1550 | - Tc: total time to establish the TCP connection to the server. |
| 1551 | It's the time elapsed between the moment the proxy sent the connection |
| 1552 | request, and the moment it was acknowledged, or between the TCP SYN packet |
| 1553 | and the matching SYN/ACK in return. The value '-1' means that the |
| 1554 | connection never established. |
| 1555 | |
| 1556 | - Tr: server response time. It's the time elapsed between the moment the |
| 1557 | TCP connection was established to the server and the moment it send its |
| 1558 | complete response header. It purely shows its request processing time, |
| 1559 | without the network overhead due to the data transmission. The value '-1' |
| 1560 | means that the last the response header (empty line) was never seen. |
| 1561 | |
| 1562 | - Tt: total session duration time, between the moment the proxy accepted it |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1563 | and the moment both ends were closed. The exception is when the 'logasap' |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1564 | option is specified. In this case, it only equals (Tq+Tw+Tc+Tr), and is |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1565 | prefixed with a '+' sign. From this field, we can deduce Td, the data |
| 1566 | transmission time, by substracting other timers when valid : |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1567 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1568 | Td = Tt - (Tq + Tw + Tc + Tr) |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1569 | |
| 1570 | Timers with '-1' values have to be excluded from this equation. |
| 1571 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1572 | In TCP mode ('option tcplog'), only Tw, Tc and Tt are reported. |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1573 | |
| 1574 | These timers provide precious indications on trouble causes. Since the TCP |
| 1575 | protocol defines retransmit delays of 3, 6, 12... seconds, we know for sure |
| 1576 | that timers close to multiples of 3s are nearly always related to packets lost |
| 1577 | due to network problems (wires or negociation). Moreover, if <Tt> is close to |
| 1578 | a timeout value specified in the configuration, it often means that a session |
| 1579 | has been aborted on time-out. |
| 1580 | |
| 1581 | Most common cases : |
| 1582 | |
| 1583 | - If Tq is close to 3000, a packet has probably been lost between the client |
| 1584 | and the proxy. |
| 1585 | - If Tc is close to 3000, a packet has probably been lost between the server |
| 1586 | and the proxy during the server connection phase. This one should always be |
| 1587 | very low (less than a few tens). |
| 1588 | - If Tr is nearly always lower than 3000 except some rare values which seem to |
| 1589 | be the average majored by 3000, there are probably some packets lost between |
| 1590 | the proxy and the server. |
| 1591 | - If Tt is often slightly higher than a time-out, it's often because the |
| 1592 | client and the server use HTTP keep-alive and the session is maintained |
| 1593 | after the response ends. Se further for how to disable HTTP keep-alive. |
| 1594 | |
| 1595 | Other cases ('xx' means any value to be ignored) : |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1596 | -1/xx/xx/xx/Tt: the client was not able to send its complete request in time, |
| 1597 | or that it aborted it too early. |
| 1598 | Tq/-1/xx/xx/Tt: it was not possible to process the request, maybe because |
| 1599 | servers were out of order. |
| 1600 | Tq/Tw/-1/xx/Tt: the connection could not establish on the server. Either it |
| 1601 | refused it or it timed out after Tt-(Tq+Tw) ms. |
| 1602 | Tq/Tw/Tc/-1/Tt: the server has accepted the connection but did not return a |
| 1603 | complete response in time, or it closed its connexion |
| 1604 | unexpectedly, after Tt-(Tq+Tw+Tc) ms. |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1605 | |
| 1606 | 4.2.4) Session state at disconnection |
| 1607 | ------------------------------------- |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1608 | TCP and HTTP logs provide a session completion indicator in the |
| 1609 | <termination_state> field, just before the number of active |
| 1610 | connections. It is 2-characters long in TCP, and 4-characters long in |
| 1611 | HTTP, each of which has a special meaning : |
| 1612 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1613 | - On the first character, a code reporting the first event which caused the |
| 1614 | session to terminate : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1615 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1616 | C : the TCP session was unexpectedly aborted by the client. |
| 1617 | |
| 1618 | S : the TCP session was unexpectedly aborted by the server, or the |
| 1619 | server explicitly refused it. |
| 1620 | |
| 1621 | P : the session was prematurely aborted by the proxy, because of a |
| 1622 | connection limit enforcement, because a DENY filter was matched, |
| 1623 | or because of a security check which detected and blocked a |
| 1624 | dangerous error in server response which might have caused |
| 1625 | information leak (eg: cacheable cookie). |
| 1626 | |
| 1627 | R : a resource on the proxy has been exhausted (memory, sockets, source |
| 1628 | ports, ...). Usually, this appears during the connection phase, and |
| 1629 | system logs should contain a copy of the precise error. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1630 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1631 | I : an internal error was identified by the proxy during a self-check. |
| 1632 | This should NEVER happen, and you are encouraged to report any log |
| 1633 | containing this, because this is a bug. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1634 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1635 | c : the client-side time-out expired first. |
| 1636 | |
| 1637 | s : the server-side time-out expired first. |
| 1638 | |
| 1639 | - : normal session completion. |
| 1640 | |
| 1641 | - on the second character, the TCP/HTTP session state when it was closed : |
| 1642 | |
| 1643 | R : waiting for complete REQUEST from the client (HTTP only). Nothing |
| 1644 | was sent to any server. |
| 1645 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1646 | Q : waiting in the QUEUE for a connection slot. This can only happen on |
| 1647 | servers which have a 'maxconn' parameter set. No connection attempt |
| 1648 | was made to any server. |
| 1649 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1650 | C : waiting for CONNECTION to establish on the server. The server might |
| 1651 | at most have noticed a connection attempt. |
| 1652 | |
| 1653 | H : waiting for, receiving and processing server HEADERS (HTTP only). |
| 1654 | |
| 1655 | D : the session was in the DATA phase. |
| 1656 | |
| 1657 | L : the proxy was still transmitting LAST data to the client while the |
| 1658 | server had already finished. |
| 1659 | |
| 1660 | - : normal session completion after end of data transfer. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1661 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1662 | - the third character tells whether the persistence cookie was provided by |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1663 | the client (only in HTTP mode) : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1664 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1665 | N : the client provided NO cookie. This is usually the case on new |
| 1666 | connections. |
| 1667 | |
| 1668 | I : the client provided an INVALID cookie matching no known |
| 1669 | server. This might be caused by a recent configuration change, |
| 1670 | mixed cookies between HTTP/HTTPS sites, or an attack. |
| 1671 | |
| 1672 | D : the client provided a cookie designating a server which was DOWN, |
| 1673 | so either the 'persist' option was used and the client was sent to |
| 1674 | this server, or it was not set and the client was redispatched to |
| 1675 | another server. |
| 1676 | |
| 1677 | V : the client provided a valid cookie, and was sent to the associated |
| 1678 | server. |
| 1679 | |
| 1680 | - : does not apply (no cookie set in configuration). |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1681 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1682 | - the last character reports what operations were performed on the persistence |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1683 | cookie returned by the server (only in HTTP mode) : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1684 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1685 | N : NO cookie was provided by the server, and none was inserted either. |
| 1686 | |
| 1687 | I : no cookie was provided by the server, and the proxy INSERTED one. |
| 1688 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1689 | P : a cookie was PROVIDED by the server and transmitted as-is. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1690 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1691 | R : the cookie provided by the server was REWRITTEN by the proxy. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1692 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1693 | D : the cookie provided by the server was DELETED by the proxy. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1694 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1695 | - : does not apply (no cookie set in configuration). |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1696 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1697 | The combination of the two first flags give a lot of information about what was |
| 1698 | happening when the session terminated. It can be helpful to detect server |
| 1699 | saturation, network troubles, local system resource starvation, attacks, etc... |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1700 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1701 | The most common termination flags combinations are indicated here. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1702 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1703 | Flags Reason |
| 1704 | CR The client aborted before sending a full request. Most probably the |
| 1705 | request was done by hand using a telnet client, and aborted early. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1706 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1707 | cR The client timed out before sending a full request. This is sometimes |
| 1708 | caused by too large TCP MSS values on the client side for PPPoE |
| 1709 | networks which cannot transport full-sized packets, or by clients |
| 1710 | sending requests by hand and not typing fast enough. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1711 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1712 | SC The server explicitly refused the connection (the proxy received a |
| 1713 | TCP RST or an ICMP in return). Under some circumstances, it can |
| 1714 | also be the network stack telling the proxy that the server is |
| 1715 | unreachable (eg: no route, or no ARP response on local network). |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1716 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1717 | sC The connection to the server did not complete during contimeout. |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1718 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1719 | PC The proxy refused to establish a connection to the server because the |
| 1720 | maxconn limit has been reached. The listener's maxconn parameter may |
| 1721 | be increased in the proxy configuration, as well as the global |
| 1722 | maxconn parameter. |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1723 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1724 | RC A local resource has been exhausted (memory, sockets, source ports) |
| 1725 | preventing the connection to the server from establishing. The error |
| 1726 | logs will tell precisely what was missing. Anyway, this can only be |
| 1727 | solved by system tuning. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1728 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1729 | cH The client timed out during a POST request. This is sometimes caused |
| 1730 | by too large TCP MSS values for PPPoE networks which cannot transport |
| 1731 | full-sized packets. |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 1732 | |
willy tarreau | 078c79a | 2006-05-13 12:23:58 +0200 | [diff] [blame] | 1733 | CH The client aborted while waiting for the server to start responding. |
| 1734 | It might be the server taking too long to respond or the client |
| 1735 | clicking the 'Stop' button too fast. |
| 1736 | |
| 1737 | CQ The client aborted while its session was queued, waiting for a server |
| 1738 | with enough empty slots to accept it. It might be that either all the |
| 1739 | servers were saturated or the assigned server taking too long to |
| 1740 | respond. |
| 1741 | |
| 1742 | sQ The session spent too much time in queue and has been expired. |
| 1743 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1744 | SH The server aborted before sending its full headers, or it crashed. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1745 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1746 | sH The server failed to reply during the srvtimeout delay, which |
| 1747 | indicates too long transactions, probably caused by back-end |
| 1748 | saturation. The only solutions are to fix the problem on the |
| 1749 | application or to increase the 'srvtimeout' parameter to support |
| 1750 | longer delays (at the risk of the client giving up anyway). |
| 1751 | |
| 1752 | PR The proxy blocked the client's request, either because of an invalid |
| 1753 | HTTP syntax, in which case it returned an HTTP 400 error to the |
| 1754 | client, or because a deny filter matched, in which case it returned |
| 1755 | an HTTP 403 error. |
| 1756 | |
| 1757 | PH The proxy blocked the server's response, because it was invalid, |
| 1758 | incomplete, dangerous (cache control), or matched a security filter. |
| 1759 | In any case, an HTTP 502 error is sent to the client. |
| 1760 | |
| 1761 | cD The client did not read any data for as long as the clitimeout delay. |
| 1762 | This is often caused by network failures on the client side. |
| 1763 | |
| 1764 | CD The client unexpectedly aborted during data transfer. This is either |
| 1765 | caused by a browser crash, or by a keep-alive session between the |
| 1766 | server and the client terminated first by the client. |
| 1767 | |
| 1768 | sD The server did nothing during the srvtimeout delay. This is often |
| 1769 | caused by too short timeouts on L4 equipements before the server |
| 1770 | (firewalls, load-balancers, ...). |
| 1771 | |
| 1772 | 4.2.5) Non-printable characters |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1773 | ------------------------------- |
| 1774 | As of version 1.1.29, non-printable characters are not sent as-is into log |
| 1775 | files, but are converted to their two-digits hexadecimal representation, |
| 1776 | prefixed by the character '#'. The only characters that can now be logged |
| 1777 | without being escaped are between 32 and 126 (inclusive). Obviously, the |
| 1778 | escape character '#' is also encoded to avoid any ambiguity. It is the same for |
| 1779 | the character '"', as well as '{', '|' and '}' when logging headers. |
| 1780 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1781 | 4.2.6) Capturing HTTP headers and cookies |
| 1782 | ----------------------------------------- |
| 1783 | Version 1.1.23 brought cookie capture, and 1.1.29 the header capture. All this |
| 1784 | is performed using the 'capture' keyword. |
| 1785 | |
| 1786 | Cookie capture makes it easy to track a complete user session. The syntax is : |
| 1787 | |
| 1788 | capture cookie <cookie_prefix> len <capture_length> |
| 1789 | |
| 1790 | This will enable cookie capture from both requests and responses. This way, |
| 1791 | it's easy to detect when a user switches to a new session for example, because |
| 1792 | the server will reassign it a new cookie. |
| 1793 | |
| 1794 | The FIRST cookie whose name starts with <cookie_prefix> will be captured, and |
| 1795 | logged as 'NAME=value', without exceeding <capture_length> characters (64 max). |
| 1796 | When the cookie name is fixed and known, it's preferable to suffix '=' to it to |
| 1797 | ensure that no other cookie will be logged. |
| 1798 | |
| 1799 | Examples : |
| 1800 | ---------- |
| 1801 | # capture the first cookie whose name starts with "ASPSESSION" |
| 1802 | capture cookie ASPSESSION len 32 |
| 1803 | |
| 1804 | # capture the first cookie whose name is exactly "vgnvisitor" |
| 1805 | capture cookie vgnvisitor= len 32 |
| 1806 | |
| 1807 | In the logs, the field preceeding the completion indicator contains the cookie |
| 1808 | value as sent by the server, preceeded by the cookie value as sent by the |
| 1809 | client. Each of these field is replaced with '-' when no cookie was seen or |
| 1810 | when the option is disabled. |
| 1811 | |
| 1812 | Header captures have a different goal. They are useful to track unique request |
| 1813 | identifiers set by a previous proxy, virtual host names, user-agents, POST |
| 1814 | content-length, referrers, etc. In the response, one can search for information |
| 1815 | about the response length, how the server asked the cache to behave, or an |
| 1816 | object location during a redirection. As for cookie captures, it is both |
| 1817 | possible to include request headers and response headers at the same time. The |
| 1818 | syntax is : |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1819 | |
| 1820 | capture request header <name> len <max length> |
| 1821 | capture response header <name> len <max length> |
| 1822 | |
| 1823 | Note: Header names are not case-sensitive. |
| 1824 | |
| 1825 | Examples: |
| 1826 | --------- |
| 1827 | # keep the name of the virtual server |
| 1828 | capture request header Host len 20 |
| 1829 | # keep the amount of data uploaded during a POST |
| 1830 | capture request header Content-Length len 10 |
| 1831 | |
| 1832 | # note the expected cache behaviour on the response |
| 1833 | capture response header Cache-Control len 8 |
| 1834 | # note the URL location during a redirection |
| 1835 | capture response header Location len 20 |
| 1836 | |
| 1837 | Non-existant headers are logged as empty strings, and if one header appears more |
| 1838 | than once, only its last occurence will be kept. Request headers are grouped |
| 1839 | within braces '{' and '}' in the same order as they were declared, and delimited |
| 1840 | with a vertical bar '|' without any space. Response headers follow the same |
| 1841 | representation, but are displayed after a space following the request headers |
| 1842 | block. These blocks are displayed just before the HTTP request in the logs. |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1843 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1844 | Example : |
| 1845 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1846 | Config: |
| 1847 | |
| 1848 | capture request header Host len 20 |
| 1849 | capture request header Content-Length len 10 |
| 1850 | capture request header Referer len 20 |
| 1851 | capture response header Server len 20 |
| 1852 | capture response header Content-Length len 10 |
| 1853 | capture response header Cache-Control len 8 |
| 1854 | capture response header Via len 20 |
| 1855 | capture response header Location len 20 |
| 1856 | |
| 1857 | Log : |
| 1858 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1859 | Aug 9 20:26:09 localhost haproxy[2022]: 127.0.0.1:34014 [09/Aug/2004:20:26:09] relais-http netcache 0/0/0/162/+162 200 +350 - - ---- 0/0/0 0/0 {fr.adserver.yahoo.co||http://fr.f416.mail.} {|864|private||} "GET http://fr.adserver.yahoo.com/" |
| 1860 | Aug 9 20:30:46 localhost haproxy[2022]: 127.0.0.1:34020 [09/Aug/2004:20:30:46] relais-http netcache 0/0/0/182/+182 200 +279 - - ---- 0/0/0 0/0 {w.ods.org||} {Formilux/0.1.8|3495|||} "GET http://w.ods.org/sytadin.html HTTP/1.1" |
| 1861 | Aug 9 20:30:46 localhost haproxy[2022]: 127.0.0.1:34028 [09/Aug/2004:20:30:46] relais-http netcache 0/0/2/126/+128 200 +223 - - ---- 0/0/0 0/0 {www.infotrafic.com||http://w.ods.org/syt} {Apache/2.0.40 (Red H|9068|||} "GET http://www.infotrafic.com/images/live/cartesidf/grandes/idf_ne.png HTTP/1.1" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1862 | |
| 1863 | |
| 1864 | 4.2.7) Examples of logs |
| 1865 | ----------------------- |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1866 | - haproxy[674]: 127.0.0.1:33319 [15/Oct/2003:08:31:57] relais-http Srv1 6559/0/7/147/6723 200 243 - - ---- 1/3/5 0/0 "HEAD / HTTP/1.0" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1867 | => long request (6.5s) entered by hand through 'telnet'. The server replied |
| 1868 | in 147 ms, and the session ended normally ('----') |
| 1869 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1870 | - haproxy[674]: 127.0.0.1:33319 [15/Oct/2003:08:31:57] relais-http Srv1 6559/1230/7/147/6870 200 243 - - ---- 99/239/324 0/9 "HEAD / HTTP/1.0" |
| 1871 | => Idem, but the request was queued in the global queue behind 9 other |
| 1872 | requests, and waited there for 1230 ms. |
| 1873 | |
| 1874 | - haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/0/7/14/+30 200 +243 - - ---- 1/3/3 0/0 "GET /image.iso HTTP/1.0" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1875 | => request for a long data transfer. The 'logasap' option was specified, so |
| 1876 | the log was produced just before transfering data. The server replied in |
| 1877 | 14 ms, 243 bytes of headers were sent to the client, and total time from |
| 1878 | accept to first data byte is 30 ms. |
| 1879 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1880 | - haproxy[674]: 127.0.0.1:33320 [15/Oct/2003:08:32:17] relais-http Srv1 9/0/7/14/30 502 243 - - PH-- 0/2/3 0/0 "GET /cgi-bin/bug.cgi? HTTP/1.0" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1881 | => the proxy blocked a server response either because of an 'rspdeny' or |
| 1882 | 'rspideny' filter, or because it blocked sensible information which risked |
| 1883 | being cached. In this case, the response is replaced with a '502 bad |
| 1884 | gateway'. |
| 1885 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1886 | - haproxy[18113]: 127.0.0.1:34548 [15/Oct/2003:15:18:55] relais-http <NOSRV> -1/-1/-1/-1/8490 -1 0 - - CR-- 0/2/2 0/0 "" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1887 | => the client never completed its request and aborted itself ('C---') after |
| 1888 | 8.5s, while the proxy was waiting for the request headers ('-R--'). |
| 1889 | Nothing was sent to the server. |
| 1890 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1891 | - haproxy[18113]: 127.0.0.1:34549 [15/Oct/2003:15:19:06] relais-http <NOSRV> -1/-1/-1/-1/50001 408 0 - - cR-- 2/2 0/0 "" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1892 | => The client never completed its request, which was aborted by the time-out |
| 1893 | ('c---') after 50s, while the proxy was waiting for the request headers ('-R--'). |
| 1894 | Nothing was sent to the server, but the proxy could send a 408 return code |
| 1895 | to the client. |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1896 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1897 | - haproxy[18989]: 127.0.0.1:34550 [15/Oct/2003:15:24:28] relais-tcp Srv1 0/0/5007 0 cD 0/0/0 0/0 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1898 | => This is a 'tcplog' entry. Client-side time-out ('c----') occured after 5s. |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1899 | |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1900 | - haproxy[18989]: 10.0.0.1:34552 [15/Oct/2003:15:26:31] relais-http Srv1 3183/-1/-1/-1/11215 503 0 - - SC-- 115/202/205 0/0 "HEAD / HTTP/1.0" |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1901 | => The request took 3s to complete (probably a network problem), and the |
| 1902 | connection to the server failed ('SC--') after 4 attemps of 2 seconds |
| 1903 | (config says 'retries 3'), then a 503 error code was sent to the client. |
willy tarreau | 532bb55 | 2006-05-13 18:40:37 +0200 | [diff] [blame] | 1904 | There were 115 connections on this server, 202 connections on this proxy, |
| 1905 | and 205 on the global process. It is possible that the server refused the |
| 1906 | connection because of too many already established. |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1907 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1908 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1909 | 4.3) HTTP header manipulation |
| 1910 | ----------------------------- |
| 1911 | In HTTP mode, it is possible to rewrite, add or delete some of the request and |
| 1912 | response headers based on regular expressions. It is also possible to block a |
| 1913 | request or a response if a particular header matches a regular expression, |
| 1914 | which is enough to stops most elementary protocol attacks, and to protect |
| 1915 | against information leak from the internal network. But there is a limitation |
| 1916 | to this : since haproxy's HTTP engine knows nothing about keep-alive, only |
| 1917 | headers passed during the first request of a TCP session will be seen. All |
| 1918 | subsequent headers will be considered data only and not analyzed. Furthermore, |
| 1919 | haproxy doesn't touch data contents, it stops at the end of headers. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1920 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1921 | The syntax is : |
| 1922 | reqadd <string> to add a header to the request |
| 1923 | reqrep <search> <replace> to modify the request |
| 1924 | reqirep <search> <replace> same, but ignoring the case |
| 1925 | reqdel <search> to delete a header in the request |
| 1926 | reqidel <search> same, but ignoring the case |
| 1927 | reqallow <search> definitely allow a request if a header matches <search> |
| 1928 | reqiallow <search> same, but ignoring the case |
| 1929 | reqdeny <search> denies a request if a header matches <search> |
| 1930 | reqideny <search> same, but ignoring the case |
| 1931 | reqpass <search> ignore a header matching <search> |
| 1932 | reqipass <search> same, but ignoring the case |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1933 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1934 | rspadd <string> to add a header to the response |
| 1935 | rsprep <search> <replace> to modify the response |
| 1936 | rspirep <search> <replace> same, but ignoring the case |
| 1937 | rspdel <search> to delete the response |
| 1938 | rspidel <search> same, but ignoring the case |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1939 | rspdeny <search> replaces a response with a HTTP 502 if a header matches <search> |
| 1940 | rspideny <search> same, but ignoring the case |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1941 | |
| 1942 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1943 | <search> is a POSIX regular expression (regex) which supports grouping through |
| 1944 | parenthesis (without the backslash). Spaces and other delimiters must be |
| 1945 | prefixed with a backslash ('\') to avoid confusion with a field delimiter. |
| 1946 | Other characters may be prefixed with a backslash to change their meaning : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1947 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1948 | \t for a tab |
| 1949 | \r for a carriage return (CR) |
| 1950 | \n for a new line (LF) |
| 1951 | \ to mark a space and differentiate it from a delimiter |
| 1952 | \# to mark a sharp and differentiate it from a comment |
| 1953 | \\ to use a backslash in a regex |
| 1954 | \\\\ to use a backslash in the text (*2 for regex, *2 for haproxy) |
| 1955 | \xXX to write the ASCII hex code XX as in the C language |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1956 | |
| 1957 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1958 | <replace> containst the string to be used to replace the largest portion of text |
| 1959 | matching the regex. It can make use of the special characters above, and can |
| 1960 | reference a substring delimited by parenthesis in the regex, by the group |
| 1961 | numerical order from 1 to 9. In this case, you would write a backslah ('\') |
| 1962 | immediately followed by one digit indicating the group position. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1963 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1964 | <string> represents the string which will systematically be added after the last |
| 1965 | header line. It can also use special characters above. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1966 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1967 | Notes : |
| 1968 | ------- |
| 1969 | - the first line is considered as a header, which makes it possible to rewrite |
| 1970 | or filter HTTP requests URIs or response codes. |
| 1971 | - 'reqrep' is the equivalent of 'cliexp' in version 1.0, and 'rsprep' is the |
| 1972 | equivalent of 'srvexp' in 1.0. Those names are still supported but |
| 1973 | deprecated. |
| 1974 | - for performances reasons, the number of characters added to a request or to |
| 1975 | a response is limited to 4096 since version 1.1.5 (it was 256 before). This |
| 1976 | value is easy to modify in the code if needed (#define). If it is too short |
| 1977 | on occasional uses, it is possible to gain some space by removing some |
| 1978 | useless headers before adding new ones. |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 1979 | - a denied request will generate an "HTTP 403 forbidden" response, while a |
| 1980 | denied response will generate an "HTTP 502 Bad gateway" response. |
| 1981 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1982 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1983 | Examples : |
| 1984 | ---------- |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1985 | ###### a few examples ###### |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1986 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1987 | # rewrite 'online.fr' instead of 'free.fr' for GET and POST requests |
| 1988 | reqrep ^(GET\ .*)(.free.fr)(.*) \1.online.fr\3 |
| 1989 | reqrep ^(POST\ .*)(.free.fr)(.*) \1.online.fr\3 |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1990 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1991 | # force proxy connections to close |
| 1992 | reqirep ^Proxy-Connection:.* Proxy-Connection:\ close |
| 1993 | # rewrite locations |
| 1994 | rspirep ^(Location:\ )([^:]*://[^/]*)(.*) \1\3 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1995 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1996 | ###### A full configuration being used on production ###### |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1997 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 1998 | # Every header should end with a colon followed by one space. |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1999 | reqideny ^[^:\ ]*[\ ]*$ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2000 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2001 | # block Apache chunk exploit |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2002 | reqideny ^Transfer-Encoding:[\ ]*chunked |
| 2003 | reqideny ^Host:\ apache- |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2004 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2005 | # block annoying worms that fill the logs... |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2006 | reqideny ^[^:\ ]*\ .*(\.|%2e)(\.|%2e)(%2f|%5c|/|\\\\) |
| 2007 | reqideny ^[^:\ ]*\ ([^\ ]*\ [^\ ]*\ |.*%00) |
| 2008 | reqideny ^[^:\ ]*\ .*<script |
| 2009 | reqideny ^[^:\ ]*\ .*/(root\.exe\?|cmd\.exe\?|default\.ida\?) |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2010 | |
| 2011 | # allow other syntactically valid requests, and block any other method |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2012 | reqipass ^(GET|POST|HEAD|OPTIONS)\ /.*\ HTTP/1\.[01]$ |
| 2013 | reqipass ^OPTIONS\ \\*\ HTTP/1\.[01]$ |
| 2014 | reqideny ^[^:\ ]*\ |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2015 | |
| 2016 | # force connection:close, thus disabling HTTP keep-alive |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2017 | option httpclose |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2018 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2019 | # change the server name |
| 2020 | rspidel ^Server:\ |
| 2021 | rspadd Server:\ Formilux/0.1.8 |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2022 | |
| 2023 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2024 | Also, the 'forwardfor' option creates an HTTP 'X-Forwarded-For' header which |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2025 | contains the client's IP address. This is useful to let the final web server |
| 2026 | know what the client address was (eg for statistics on domains). |
| 2027 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2028 | Last, the 'httpclose' option removes any 'Connection' header both ways, and |
| 2029 | adds a 'Connection: close' header in each direction. This makes it easier to |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 2030 | disable HTTP keep-alive than the previous 4-rules block. |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2031 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2032 | Example : |
| 2033 | --------- |
| 2034 | listen http_proxy 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2035 | mode http |
| 2036 | log global |
| 2037 | option httplog |
| 2038 | option dontlognull |
| 2039 | option forwardfor |
| 2040 | option httpclose |
| 2041 | |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 2042 | Note that some HTTP servers do not necessarily close the connections when they |
| 2043 | receive the 'Connection: close', and if the client does not close either, then |
| 2044 | the connection will be maintained up to the time-out. This translates into high |
| 2045 | number of simultaneous sessions and high global session times in the logs. To |
| 2046 | workaround this, a new option 'forceclose' appeared in version 1.2.9 to enforce |
| 2047 | the closing of the outgoing server channel as soon as the server begins to |
| 2048 | reply and only if the request buffer is empty. Note that this should NOT be |
| 2049 | used if CONNECT requests are expected between the client and the server. The |
| 2050 | 'forceclose' option implies the 'httpclose' option. |
| 2051 | |
| 2052 | Example : |
| 2053 | --------- |
| 2054 | listen http_proxy 0.0.0.0:80 |
| 2055 | mode http |
| 2056 | log global |
| 2057 | option httplog |
| 2058 | option dontlognull |
| 2059 | option forwardfor |
| 2060 | option forceclose |
| 2061 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2062 | |
| 2063 | 4.4) Load balancing with persistence |
| 2064 | ------------------------------------ |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2065 | Combining cookie insertion with internal load balancing allows to transparently |
| 2066 | bring persistence to applications. The principle is quite simple : |
| 2067 | - assign a cookie value to each server |
| 2068 | - enable the load balancing between servers |
| 2069 | - insert a cookie into responses resulting from the balancing algorithm |
| 2070 | (indirect accesses), end ensure that no upstream proxy will cache it. |
| 2071 | - remove the cookie in the request headers so that the application never sees |
| 2072 | it. |
| 2073 | |
| 2074 | Example : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2075 | --------- |
| 2076 | listen application 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2077 | mode http |
| 2078 | cookie SERVERID insert nocache indirect |
| 2079 | balance roundrobin |
| 2080 | server srv1 192.168.1.1:80 cookie server01 check |
| 2081 | server srv2 192.168.1.2:80 cookie server02 check |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2082 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 2083 | The other solution brought by versions 1.1.30 and 1.2.3 is to reuse a cookie |
| 2084 | from the server, and prefix the server's name to it. In this case, don't forget |
| 2085 | to force "httpclose" mode so that you can be assured that every subsequent |
| 2086 | request will have its cookie fixed. |
| 2087 | |
| 2088 | listen application 0.0.0.0:80 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2089 | mode http |
| 2090 | cookie JSESSIONID prefix |
| 2091 | balance roundrobin |
| 2092 | server srv1 192.168.1.1:80 cookie srv1 check |
| 2093 | server srv2 192.168.1.2:80 cookie srv2 check |
| 2094 | option httpclose |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 2095 | |
| 2096 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2097 | 4.5) Protection against information leak from the servers |
| 2098 | --------------------------------------------------------- |
| 2099 | In versions 1.1.28/1.2.1, a new option 'checkcache' was created. It carefully |
| 2100 | checks 'Cache-control', 'Pragma' and 'Set-cookie' headers in server response |
| 2101 | to check if there's a risk of caching a cookie on a client-side proxy. When this |
| 2102 | option is enabled, the only responses which can be delivered to the client are : |
| 2103 | - all those without 'Set-Cookie' header ; |
| 2104 | - all those with a return code other than 200, 203, 206, 300, 301, 410, |
| 2105 | provided that the server has not set a 'Cache-control: public' header ; |
| 2106 | - all those that come from a POST request, provided that the server has not |
| 2107 | set a 'Cache-Control: public' header ; |
| 2108 | - those with a 'Pragma: no-cache' header |
| 2109 | - those with a 'Cache-control: private' header |
| 2110 | - those with a 'Cache-control: no-store' header |
| 2111 | - those with a 'Cache-control: max-age=0' header |
| 2112 | - those with a 'Cache-control: s-maxage=0' header |
| 2113 | - those with a 'Cache-control: no-cache' header |
| 2114 | - those with a 'Cache-control: no-cache="set-cookie"' header |
| 2115 | - those with a 'Cache-control: no-cache="set-cookie,' header |
| 2116 | (allowing other fields after set-cookie) |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2117 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2118 | If a response doesn't respect these requirements, then it will be blocked just |
| 2119 | as if it was from an 'rspdeny' filter, with an "HTTP 502 bad gateway". The |
| 2120 | session state shows "PH--" meaning that the proxy blocked the response during |
| 2121 | headers processing. Additionnaly, an alert will be sent in the logs so that |
| 2122 | admins are told that there's something to be done. |
| 2123 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2124 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2125 | 4.6) Customizing errors |
| 2126 | ----------------------- |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2127 | Some situations can make haproxy return an HTTP error code to the client : |
| 2128 | - invalid or too long request => HTTP 400 |
| 2129 | - request not completely sent in time => HTTP 408 |
| 2130 | - forbidden request (matches a deny filter) => HTTP 403 |
| 2131 | - internal error in haproxy => HTTP 500 |
| 2132 | - the server returned an invalid or incomplete response => HTTP 502 |
| 2133 | - no server was available to handle the request => HTTP 503 |
| 2134 | - the server failed to reply in time => HTTP 504 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2135 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2136 | A succint error message taken from the RFC accompanies these return codes. |
| 2137 | But depending on the clients knowledge, it may be better to return custom, user |
| 2138 | friendly, error pages. This is made possible through the use of the 'errorloc' |
| 2139 | command : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2140 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2141 | errorloc <HTTP_code> <location> |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2142 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2143 | Instead of generating an HTTP error <HTTP_code> among those above, the proxy |
| 2144 | will return a temporary redirection code (HTTP 302) towards the address |
| 2145 | specified in <location>. This address may be either relative to the site or |
| 2146 | absolute. Since this request will be handled by the client's browser, it's |
| 2147 | mandatory that the returned address be reachable from the outside. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2148 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2149 | Example : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2150 | --------- |
| 2151 | listen application 0.0.0.0:80 |
| 2152 | errorloc 400 /badrequest.html |
| 2153 | errorloc 403 /forbidden.html |
| 2154 | errorloc 408 /toolong.html |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2155 | errorloc 500 http://haproxy.domain.net/bugreport.html |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2156 | errorloc 502 http://192.168.114.58/error50x.html |
| 2157 | errorloc 503 http://192.168.114.58/error50x.html |
| 2158 | errorloc 504 http://192.168.114.58/error50x.html |
| 2159 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 2160 | Note: RFC2616 says that a client must reuse the same method to fetch the |
| 2161 | Location returned by a 302, which causes problems with the POST method. |
| 2162 | The return code 303 was designed explicitly to force the client to fetch the |
| 2163 | Location URL with the GET method, but there are some browsers pre-dating |
| 2164 | HTTP/1.1 which don't support it. Anyway, most browsers still behave with 302 as |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2165 | if it was a 303. In order to allow the user to chose, versions 1.1.31 and 1.2.5 |
| 2166 | bring two new keywords to replace 'errorloc' : 'errorloc302' and 'errorloc303'. |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 2167 | |
| 2168 | They are preffered over errorloc (which still does 302). Consider using |
| 2169 | errorloc303 everytime you know that your clients support HTTP 303 responses.. |
| 2170 | |
| 2171 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2172 | 4.7) Modifying default values |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2173 | ----------------------------- |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2174 | Version 1.1.22 introduced the notion of default values, which eliminates the |
| 2175 | pain of often repeating common parameters between many instances, such as |
| 2176 | logs, timeouts, modes, etc... |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2177 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2178 | Default values are set in a 'defaults' section. Each of these section clears |
| 2179 | all previously set default parameters, so there may be as many default |
| 2180 | parameters as needed. Only the last one before a 'listen' section will be |
| 2181 | used for this section. The 'defaults' section uses the same syntax as the |
| 2182 | 'listen' section, for the supported parameters. The 'defaults' keyword ignores |
| 2183 | everything on its command line, so that fake instance names can be specified |
| 2184 | there for better clarity. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2185 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2186 | In version 1.1.28/1.2.1, only those parameters can be preset in the 'default' |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2187 | section : |
| 2188 | - log (the first and second one) |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2189 | - mode { tcp, http, health } |
| 2190 | - balance { roundrobin } |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2191 | - disabled (to disable every further instances) |
| 2192 | - enabled (to enable every further instances, this is the default) |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2193 | - contimeout, clitimeout, srvtimeout, grace, retries, maxconn |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2194 | - option { redispatch, transparent, keepalive, forwardfor, logasap, httpclose, |
| 2195 | checkcache, httplog, tcplog, dontlognull, persist, httpchk } |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2196 | - redispatch, redisp, transparent, source { addr:port } |
| 2197 | - cookie, capture |
| 2198 | - errorloc |
| 2199 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2200 | As of 1.1.24, it is not possible to put certain parameters in a 'defaults' |
| 2201 | section, mainly regular expressions and server configurations : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2202 | - dispatch, server, |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2203 | - req*, rsp* |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2204 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2205 | Last, there's no way yet to change a boolean option from its assigned default |
| 2206 | value. So if an 'option' statement is set in a 'defaults' section, the only |
| 2207 | way to flush it is to redefine a new 'defaults' section without this 'option'. |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2208 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2209 | Examples : |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2210 | ---------- |
| 2211 | defaults applications TCP |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2212 | log global |
| 2213 | mode tcp |
| 2214 | balance roundrobin |
| 2215 | clitimeout 180000 |
| 2216 | srvtimeout 180000 |
| 2217 | contimeout 4000 |
| 2218 | retries 3 |
| 2219 | redispatch |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2220 | |
| 2221 | listen app_tcp1 10.0.0.1:6000-6063 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2222 | server srv1 192.168.1.1 check port 6000 inter 10000 |
| 2223 | server srv2 192.168.1.2 backup |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2224 | |
| 2225 | listen app_tcp2 10.0.0.2:6000-6063 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2226 | server srv1 192.168.2.1 check port 6000 inter 10000 |
| 2227 | server srv2 192.168.2.2 backup |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2228 | |
| 2229 | defaults applications HTTP |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2230 | log global |
| 2231 | mode http |
| 2232 | option httplog |
| 2233 | option forwardfor |
| 2234 | option dontlognull |
| 2235 | balance roundrobin |
| 2236 | clitimeout 20000 |
| 2237 | srvtimeout 20000 |
| 2238 | contimeout 4000 |
| 2239 | retries 3 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2240 | |
| 2241 | listen app_http1 10.0.0.1:80-81 |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2242 | cookie SERVERID postonly insert indirect |
| 2243 | capture cookie userid= len 10 |
| 2244 | server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000 |
| 2245 | server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000 |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2246 | |
| 2247 | defaults |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2248 | # this empty section voids all default parameters |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2249 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2250 | ========================= |
| 2251 | | System-specific setup | |
| 2252 | ========================= |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2253 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2254 | Linux 2.4 |
| 2255 | ========= |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2256 | |
| 2257 | -- cut here -- |
| 2258 | #!/bin/sh |
| 2259 | # set this to about 256/4M (16384 for 256M machine) |
| 2260 | MAXFILES=16384 |
| 2261 | echo $MAXFILES > /proc/sys/fs/file-max |
| 2262 | ulimit -n $MAXFILES |
| 2263 | |
| 2264 | if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2265 | echo 65536 > /proc/sys/net/ipv4/ip_conntrack_max |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2266 | fi |
| 2267 | |
| 2268 | if [ -e /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait ]; then |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2269 | # 30 seconds for fin, 15 for time wait |
| 2270 | echo 3000 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait |
| 2271 | echo 1500 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_time_wait |
| 2272 | echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_invalid_scale |
| 2273 | echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_out_of_window |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2274 | fi |
| 2275 | |
| 2276 | echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range |
| 2277 | echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout |
| 2278 | echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog |
| 2279 | echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets |
| 2280 | echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans |
| 2281 | echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time |
| 2282 | echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle |
| 2283 | echo 0 > /proc/sys/net/ipv4/tcp_timestamps |
| 2284 | echo 0 > /proc/sys/net/ipv4/tcp_ecn |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2285 | echo 1 > /proc/sys/net/ipv4/tcp_sack |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2286 | echo 0 > /proc/sys/net/ipv4/tcp_dsack |
| 2287 | |
| 2288 | # auto-tuned on 2.4 |
| 2289 | #echo 262143 > /proc/sys/net/core/rmem_max |
| 2290 | #echo 262143 > /proc/sys/net/core/rmem_default |
| 2291 | |
| 2292 | echo 16384 65536 524288 > /proc/sys/net/ipv4/tcp_rmem |
| 2293 | echo 16384 349520 699040 > /proc/sys/net/ipv4/tcp_wmem |
| 2294 | |
| 2295 | -- cut here -- |
| 2296 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2297 | |
| 2298 | FreeBSD |
| 2299 | ======= |
| 2300 | |
| 2301 | A FreeBSD port of HA-Proxy is now available and maintained, thanks to |
| 2302 | Clement Laforet <sheepkiller@cultdeadsheep.org>. |
| 2303 | |
| 2304 | For more information : |
| 2305 | http://www.freebsd.org/cgi/url.cgi?ports/net/haproxy/pkg-descr |
| 2306 | http://www.freebsd.org/cgi/cvsweb.cgi/ports/net/haproxy/ |
| 2307 | http://www.freshports.org/net/haproxy |
| 2308 | |
| 2309 | |
| 2310 | -- end -- |