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