willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame^] | 1 | |
| 2 | H A - P r o x y |
| 3 | --------------- |
| 4 | version 1.1.23 |
| 5 | willy tarreau |
| 6 | 2003/09/20 |
| 7 | |
| 8 | ============ |
| 9 | | Abstract | |
| 10 | ============ |
| 11 | |
| 12 | HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high |
| 13 | availability environments. Indeed, it can : |
| 14 | - route HTTP requests depending on statically assigned cookies ; |
| 15 | - spread the load among several servers while assuring server persistence |
| 16 | through the use of HTTP cookies ; |
| 17 | - switch to backup servers in the event a main one fails ; |
| 18 | - accept connections to special ports dedicated to service monitoring ; |
| 19 | - stop accepting connections without breaking existing ones ; |
| 20 | - add/modify/delete HTTP headers both ways ; |
| 21 | - block requests matching a particular pattern ; |
| 22 | |
| 23 | It needs very little resource. Its event-driven architecture allows it to easily |
| 24 | handle thousands of simultaneous connections on hundreds of instances without |
| 25 | risking the system's stability. |
| 26 | |
| 27 | ==================== |
| 28 | | Start parameters | |
| 29 | ==================== |
| 30 | |
| 31 | There are only a few command line options : |
| 32 | |
| 33 | -f <configuration file> |
| 34 | -n <high limit for the total number of simultaneous connections> |
| 35 | -N <high limit for the per-proxy number of simultaneous connections> |
| 36 | -d starts in foregreound with debugging mode enabled |
| 37 | -D starts in daemon mode |
| 38 | -s shows statistics (only if compiled in) |
| 39 | -l shows even more statistics (implies '-s') |
| 40 | |
| 41 | |
| 42 | The maximal number of connections per proxy is used as the default parameter for |
| 43 | each instance for which the 'maxconn' paramter is not set in the 'listen' section. |
| 44 | |
| 45 | The maximal number of total connections limits the number of connections used by |
| 46 | the whole process if the 'maxconn' parameter is not set in the 'global' section. |
| 47 | |
| 48 | The debugging mode has the same effect as the 'debug' option in the 'global' |
| 49 | section. When the proxy runs in this mode, it dumps every connections, |
| 50 | disconnections, timestamps, and HTTP headers to stdout. This should NEVER |
| 51 | be used in an init script since it will prevent the system from starting up. |
| 52 | |
| 53 | Statistics are only available if compiled in with the 'STATTIME' option. It's |
| 54 | only used during code optimization phases. |
| 55 | |
| 56 | ====================== |
| 57 | | Configuration file | |
| 58 | ====================== |
| 59 | |
| 60 | Structure |
| 61 | ========= |
| 62 | |
| 63 | The configuration file parser ignores empty lines, spaces, tabs. Anything |
| 64 | between a sharp ('#') not following a backslash ('\'), and the end of a line |
| 65 | constitutes a comment and is ignored too. |
| 66 | |
| 67 | The configuration file is segmented in sections. A section begins whenever |
| 68 | one of these 3 keywords are encountered : |
| 69 | |
| 70 | - 'global' |
| 71 | - 'listen' |
| 72 | - 'defaults' |
| 73 | |
| 74 | Every parameter refer to the section beginning at the last one of these 3 |
| 75 | keywords. |
| 76 | |
| 77 | |
| 78 | 1) Global parameters |
| 79 | ==================== |
| 80 | |
| 81 | Global parameters affect the whole process behaviour. They are all set in the |
| 82 | 'global' section. There may be several 'global' sections if needed, but their |
| 83 | parameters will only be merged. Allowed parameters in 'global' section include |
| 84 | the following ones : |
| 85 | |
| 86 | - log <address> <facility> [max_level] |
| 87 | - maxconn <number> |
| 88 | - uid <user id> |
| 89 | - gid <group id> |
| 90 | - chroot <directory> |
| 91 | - nbproc <number> |
| 92 | - daemon |
| 93 | - debug |
| 94 | - quiet |
| 95 | |
| 96 | 1.1) Event logging |
| 97 | ------------------ |
| 98 | Most events are logged : start, stop, servers going up and down, connections and |
| 99 | errors. Each event generates a syslog message which can be sent to up to 2 |
| 100 | servers. The syntax is : |
| 101 | |
| 102 | log <ip_address> <facility> [max_level] |
| 103 | |
| 104 | Connections are logged at level "info". Services initialization and servers |
| 105 | going up are logged at level "notice", termination signals are logged at |
| 106 | "warning", and definitive service termination, as well as loss of servers are |
| 107 | logged at level "alert". The optional parameter <max_level> specifies above |
| 108 | what level messages should be sent. Level can take one of these 8 values : |
| 109 | |
| 110 | emerg, alert, crit, err, warning, notice, info, debug |
| 111 | |
| 112 | For backwards compatibility with versions 1.1.16 and earlier, the default level |
| 113 | value is "debug" if not specified. |
| 114 | |
| 115 | Permitted facilities are : |
| 116 | kern, user, mail, daemon, auth, syslog, lpr, news, |
| 117 | uucp, cron, auth2, ftp, ntp, audit, alert, cron2, |
| 118 | local0, local1, local2, local3, local4, local5, local6, local7 |
| 119 | |
| 120 | According to RFC3164, messages are truncated to 1024 bytes before being emitted. |
| 121 | |
| 122 | Example : |
| 123 | --------- |
| 124 | global |
| 125 | log 192.168.2.200 local3 |
| 126 | log 127.0.0.1 local4 notice |
| 127 | |
| 128 | 1.2) limiting the number of connections |
| 129 | --------------------------------------- |
| 130 | It is possible and recommended to limit the global number of per-process |
| 131 | connections. Since one connection includes both a client and a server, it |
| 132 | means that the max number of TCP sessions will be about the double of this |
| 133 | number. It's important to understand this when trying to find best values |
| 134 | for 'ulimit -n' before starting the proxy. To anticipate the number of |
| 135 | sockets needed, all these parameters must be counted : |
| 136 | |
| 137 | - 1 socket per incoming connection |
| 138 | - 1 socket per outgoing connection |
| 139 | - 1 socket per address/port/proxy tuple. |
| 140 | - 1 socket per server being health-checked |
| 141 | - 1 socket for all logs |
| 142 | |
| 143 | In simple configurations where each proxy only listens one one address/port, |
| 144 | set the limit of file descriptors (ulimit -n) to |
| 145 | (2 * maxconn + nbproxies + nbservers + 1). In a future release, haproxy may |
| 146 | be able to set this value itself. |
| 147 | |
| 148 | 1.3) Drop of priviledges |
| 149 | ------------------------ |
| 150 | In order to reduce the risk and consequences of attacks, in the event where a |
| 151 | yet non-identified vulnerability would be successfully exploited, it's possible |
| 152 | to lower the process priviledges and even isolate it in a riskless directory. |
| 153 | |
| 154 | In the 'global' section, the 'uid' parameter sets a numerical user identifier |
| 155 | which the process will switch to after binding its listening sockets. The value |
| 156 | '0', which normally represents the super-user, here indicates that the UID must |
| 157 | not change during startup. It's the default behaviour. The 'gid' parameter does |
| 158 | the same for the group identifier. It's particularly advised against use of |
| 159 | generic accounts such as 'nobody' because it has the same consequences as using |
| 160 | 'root' if other services use them. |
| 161 | |
| 162 | The 'chroot' parameter makes the process isolate itself in an empty directory |
| 163 | just before switching its UID. This type of isolation (chroot) can sometimes |
| 164 | be worked around on certain OS (Linux, Solaris), provided that the attacker |
| 165 | has gained 'root' priviledges and has the ability to use or create a directory. |
| 166 | For this reason, it's capital to use a dedicated directory and not to share one |
| 167 | between several services of different nature. To make isolation more resistant, |
| 168 | it's recommended to use an empty directory without any right, and to change the |
| 169 | UID of the process so that it cannot do anything there. |
| 170 | |
| 171 | Note: in the event where such a vulnerability would be exploited, it's most |
| 172 | likely that first attempts would kill the process due to 'Segmentation Fault', |
| 173 | 'Bus Error' or 'Illegal Instruction' signals. Eventhough it's true that |
| 174 | isolating the server reduces the risks of intrusion, it's sometimes useful to |
| 175 | find why a process dies, via the analysis of a 'core' file, although very rare |
| 176 | (the last bug of this sort was fixed in 1.1.9). For security reasons, most |
| 177 | systems disable the generation of core file when a process changes its UID. So |
| 178 | the two workarounds are either to start the process from a restricted user |
| 179 | account, which will not be able to chroot itself, or start it as root and not |
| 180 | change the UID. In both cases the core will be either in the start or the chroot |
| 181 | directories. Do not forget to allow core dumps prior to start the process : |
| 182 | |
| 183 | # ulimit -c unlimited |
| 184 | |
| 185 | Example : |
| 186 | --------- |
| 187 | |
| 188 | global |
| 189 | uid 30000 |
| 190 | gid 30000 |
| 191 | chroot /var/chroot/haproxy |
| 192 | |
| 193 | 1.4) Startup modes |
| 194 | ------------------ |
| 195 | The service can start in several different : |
| 196 | - foreground / background |
| 197 | - quiet / normal / debug |
| 198 | |
| 199 | The default mode is normal, foreground, which means that the program doesn't |
| 200 | return once started. NEVER EVER use this mode in a system startup script, or |
| 201 | the system won't boot. It needs to be started in background, so that it |
| 202 | returns immediately after forking. That's accomplished by the 'daemon' option |
| 203 | in the 'global' section, which is the equivalent of the '-D' command line |
| 204 | argument. |
| 205 | |
| 206 | Moreover, certain alert messages are still sent to the standard output even |
| 207 | in 'daemon' mode. To make them disappear, simply add the 'quiet' option in the |
| 208 | 'global' section. This option has no command-line equivalent. |
| 209 | |
| 210 | Last, the 'debug' mode, enabled with the 'debug' option in the 'global' section, |
| 211 | and which is equivalent of the '-d' option, allows deep TCP/HTTP analysis, with |
| 212 | timestamped display of each connection, disconnection, and HTTP headers for both |
| 213 | ways. This mode is incompatible with 'daemon' and 'quiet' modes for obvious |
| 214 | reasons. |
| 215 | |
| 216 | 1.5) Increasing the overall processing power |
| 217 | -------------------------------------------- |
| 218 | On multi-processor systems, it may seem to be a shame to use only one processor, |
| 219 | eventhough the load needed to saturate a recent processor are far above common |
| 220 | usage. Anyway, for very specific needs, the proxy can start several processes |
| 221 | between which the operating system will spread the incoming connections. The |
| 222 | number of processes is controlled by the 'nbproc' parameter in the 'global' |
| 223 | section. It defaults to 1, and obviously works only in 'daemon' mode. |
| 224 | |
| 225 | Example : |
| 226 | --------- |
| 227 | |
| 228 | global |
| 229 | daemon |
| 230 | quiet |
| 231 | nbproc 2 |
| 232 | |
| 233 | |
| 234 | 2) Declaration of a listening service |
| 235 | ===================================== |
| 236 | |
| 237 | Service sections start with the 'listen' keyword : |
| 238 | |
| 239 | listen <instance_name> [ <IP_address>:<port_range>[,...] ] |
| 240 | |
| 241 | - <instance_name> is the name of the instance. This name will be reported in |
| 242 | logs, so it is good to have it reflect the proxied service. No unicity test |
| 243 | is done on this name, and it's not mandatory for it to be unique, but highly |
| 244 | recommended. |
| 245 | |
| 246 | - <IP_address> is the IP address the proxy binds to. Empty address, '*' and |
| 247 | '0.0.0.0' all mean that the proxy listens to all valid addresses on the |
| 248 | system. |
| 249 | |
| 250 | - <port_range> is either a unique port, or a port range for which the proxy will |
| 251 | accept connections for the IP address specified above. This range can be : |
| 252 | - a numerical port (ex: '80') |
| 253 | - a dash-delimited ports range explicitly stating the lower and upper bounds |
| 254 | (ex: '2000-2100') which are included in the range. |
| 255 | |
| 256 | Particular care must be taken against port ranges, because every <addr:port> |
| 257 | couple consumes one socket (=a file descriptor), so it's easy to eat lots of |
| 258 | descriptors with a simple range. The <addr:port> couple must be used only once |
| 259 | among all instances running on a same system. Please note that attaching to |
| 260 | ports lower than 1024 need particular priviledges to start the program, which |
| 261 | are independant of the 'uid' parameter. |
| 262 | |
| 263 | - the <IP_address>:<port_range> couple may be repeated indefinitely to require |
| 264 | the proxy to listen to other addresses and/or ports. To achieve this, simply |
| 265 | separate them with a coma. |
| 266 | |
| 267 | Examples : |
| 268 | --------- |
| 269 | listen http_proxy :80 |
| 270 | listen x11_proxy 127.0.0.1:6000-6009 |
| 271 | listen smtp_proxy 127.0.0.1:25,127.0.0.1:587 |
| 272 | listen ldap_proxy :389,:663 |
| 273 | |
| 274 | In the event that all addresses do not fit line width, it's preferable to |
| 275 | detach secondary addresses on other lines with the 'bind' keyword. If this |
| 276 | keyword is used, it's not even necessary to specify the first address on the |
| 277 | 'listen' line, which sometimes makes multiple configuration handling easier : |
| 278 | |
| 279 | bind [ <IP_address>:<port_range>[,...] ] |
| 280 | |
| 281 | Examples : |
| 282 | ---------- |
| 283 | listen http_proxy |
| 284 | bind :80,:443 |
| 285 | bind 10.0.0.1:10080,10.0.0.1:10443 |
| 286 | |
| 287 | 2.1) Inhibiting a service |
| 288 | ------------------------- |
| 289 | A service may be disabled for maintenance reasons, without needing to comment |
| 290 | out the whole section, simply by specifying the 'disabled' keyword in the |
| 291 | section to be disabled : |
| 292 | |
| 293 | listen smtp_proxy 0.0.0.0:25 |
| 294 | disabled |
| 295 | |
| 296 | Note: the 'enabled' keyword allows to enable a service which has been disabled |
| 297 | previously by a default configuration. |
| 298 | |
| 299 | 2.2) Modes of operation |
| 300 | ----------------------- |
| 301 | A service can work in 3 different distinct modes : |
| 302 | - TCP |
| 303 | - HTTP |
| 304 | - monitoring |
| 305 | |
| 306 | TCP mode |
| 307 | -------- |
| 308 | In this mode, the service relays TCP connections as soon as they're established, |
| 309 | towards one or several servers. No processing is done on the stream. It's only |
| 310 | an association of source(addr:port) -> destination(addr:port). To use this mode, |
| 311 | you must specify 'mode tcp' in the 'listen' section. This is the default mode. |
| 312 | |
| 313 | Example : |
| 314 | --------- |
| 315 | listen smtp_proxy 0.0.0.0:25 |
| 316 | mode tcp |
| 317 | |
| 318 | HTTP mode |
| 319 | --------- |
| 320 | In this mode, the service relays TCP connections towards one or several servers, |
| 321 | when it has enough informations to decide, which normally means that all HTTP |
| 322 | headers have been read. Some of them may be scanned for a cookie or a pattern |
| 323 | matching a regex. To use this mode, specify 'mode http' in the 'listen' section. |
| 324 | |
| 325 | Example : |
| 326 | --------- |
| 327 | listen http_proxy 0.0.0.0:80 |
| 328 | mode http |
| 329 | |
| 330 | Health-checking mode |
| 331 | -------------------- |
| 332 | This mode provides a way for external components to check the proxy's health. |
| 333 | It is meant to be used with intelligent load-balancers which can use send/expect |
| 334 | scripts to check for all of their servers' availability. This one simply accepts |
| 335 | the connection, returns the word 'OK' and closes it. To enable it, simply |
| 336 | specify 'health' as the working mode : |
| 337 | |
| 338 | Example : |
| 339 | --------- |
| 340 | listen health_check 0.0.0.0:60000 |
| 341 | mode health |
| 342 | |
| 343 | |
| 344 | 2.3) Limiting the number of simultaneous connections |
| 345 | ---------------------------------------------------- |
| 346 | The 'maxconn' parameter allows a proxy to refuse connections above a certain |
| 347 | amount of simultaneous ones. When the limit is reached, it simply stops |
| 348 | listening, but the system may still be accepting them because of the back log |
| 349 | queue. These connections will be processed further when other ones have freed |
| 350 | some slots. This provides a serialization effect which helps very fragile |
| 351 | servers resist to high loads. Se further for system limitations. |
| 352 | |
| 353 | Example : |
| 354 | --------- |
| 355 | listen tiny_server 0.0.0.0:80 |
| 356 | maxconn 10 |
| 357 | |
| 358 | |
| 359 | 2.4) Soft stop |
| 360 | -------------- |
| 361 | It is possible to stop services without breaking existing connections by the |
| 362 | sending of the SIG_USR1 signal to the process. All services are then put into |
| 363 | soft-stop state, which means that they will refuse to accept new connections, |
| 364 | except for those which have a non-zero value in the 'grace' parameter, in which |
| 365 | case they will still accept connections for the specified amount of time, in |
| 366 | milliseconds. This allows to tell a load-balancer that the service is failing, |
| 367 | while still doing the job during the time it needs to detect it. |
| 368 | |
| 369 | Note: active connections are never killed. In the worst case, the user will have |
| 370 | to wait for all of them to close or to time-out, or simply kill the process |
| 371 | normally (SIG_TERM). The default 'grace' value is '0'. |
| 372 | |
| 373 | Example : |
| 374 | --------- |
| 375 | # enter soft stop after 'killall -USR1 haproxy' |
| 376 | # the service will still run 10 seconds after the signal |
| 377 | listen http_proxy 0.0.0.0:80 |
| 378 | mode http |
| 379 | grace 10000 |
| 380 | |
| 381 | # this port is dedicated to a load-balancer, and must fail immediately |
| 382 | listen health_check 0.0.0.0:60000 |
| 383 | mode health |
| 384 | grace 0 |
| 385 | |
| 386 | |
| 387 | 2.5) Connections expiration time |
| 388 | -------------------------------- |
| 389 | It is possible (and recommended) to configure several time-outs on TCP |
| 390 | connections. Three independant timers are adjustable with values specified |
| 391 | in milliseconds. A session will be terminated if either one of these timers |
| 392 | expire. |
| 393 | |
| 394 | - the time we accept to wait for data from the client, or for the client to |
| 395 | accept data : 'clitimeout' : |
| 396 | |
| 397 | # client time-out set to 2mn30. |
| 398 | clitimeout 150000 |
| 399 | |
| 400 | - the time we accept to wait for data from the server, or for the server to |
| 401 | accept data : 'srvtimeout' : |
| 402 | |
| 403 | # server time-out set to 30s. |
| 404 | srvtimeout 30000 |
| 405 | |
| 406 | - the time we accept to wait for a connection to establish on a server : |
| 407 | 'contimeout' : |
| 408 | |
| 409 | # we give up if the connection does not complete within 4 seconds |
| 410 | contimeout 4000 |
| 411 | |
| 412 | Notes : |
| 413 | ------- |
| 414 | - 'contimeout' and 'srvtimeout' have no sense on 'health' mode servers ; |
| 415 | - under high loads, or with a saturated or defective network, it's possible |
| 416 | that some packets get lost. Since the first TCP retransmit only happens |
| 417 | after 3 seconds, a time-out equal to, or lower than 3 seconds cannot |
| 418 | compensate for a packet loss. A 4 seconds time-out seems a reasonable |
| 419 | minimum which will considerably reduce connection failures. |
| 420 | |
| 421 | 2.6) Attempts to reconnect |
| 422 | -------------------------- |
| 423 | After a connection failure to a server, it is possible to retry, potentially |
| 424 | on another server. This is useful if health-checks are too rare and you don't |
| 425 | want the clients to see the failures. The number of attempts to reconnect is |
| 426 | set by the 'retries' paramter. |
| 427 | |
| 428 | Example : |
| 429 | --------- |
| 430 | # we can retry 3 times max after a failure |
| 431 | retries 3 |
| 432 | |
| 433 | |
| 434 | 2.7) Address of the dispatch server (deprecated) |
| 435 | ------------------------------------------------ |
| 436 | The server which will be sent all new connections is defined by the 'dispatch' |
| 437 | parameter, in the form <address>:<port>. It generally is dedicated to unknown |
| 438 | connections and will assign them a cookie, in case of HTTP persistence mode, |
| 439 | or simply is a single server in case of generic TCP proxy. This old mode is only |
| 440 | provided for backwards compatibility, but doesn't allow to check remote servers |
| 441 | state, and has a rather limited usage. All new setups should switch to 'balance' |
| 442 | mode. The principle of the dispatcher is to be able to perform the load |
| 443 | balancing itself, but work only on new clients so that the server doesn't need |
| 444 | to be a big machine. |
| 445 | |
| 446 | Example : |
| 447 | --------- |
| 448 | # all new connections go there |
| 449 | dispatch 192.168.1.2:80 |
| 450 | |
| 451 | Note : |
| 452 | ------ |
| 453 | This parameter has no sense for 'health' servers, and is incompatible with |
| 454 | 'balance' mode. |
| 455 | |
| 456 | |
| 457 | 2.8) Outgoing source address |
| 458 | ---------------------------- |
| 459 | It is often necessary to bind to a particular address when connecting to some |
| 460 | remote hosts. This is done via the 'source' parameter which is a per-proxy |
| 461 | parameter. A newer version may allow to fix different sources to reach different |
| 462 | servers. The syntax is 'source <address>[:<port>]', where <address> is a valid |
| 463 | local address (or '0.0.0.0' or '*' or empty to let the system choose), and |
| 464 | <port> is an optional parameter allowing the user to force the source port for |
| 465 | very specific needs. If the port is not specified or is '0', the system will |
| 466 | choose a free port. Note that as of version 1.1.18, the servers health checks |
| 467 | are also performed from the same source. |
| 468 | |
| 469 | Examples : |
| 470 | ---------- |
| 471 | listen http_proxy *:80 |
| 472 | # all connections take 192.168.1.200 as source address |
| 473 | source 192.168.1.200:0 |
| 474 | |
| 475 | listen rlogin_proxy *:513 |
| 476 | # use address 192.168.1.200 and the reserved port 900 (needs to be root) |
| 477 | source 192.168.1.200:900 |
| 478 | |
| 479 | |
| 480 | 2.9) Setting the cookie name |
| 481 | ---------------------------- |
| 482 | In HTTP mode, it is possible to look for a particular cookie which will contain |
| 483 | a server identifier which should handle the connection. The cookie name is set |
| 484 | via the 'cookie' parameter. |
| 485 | |
| 486 | Example : |
| 487 | --------- |
| 488 | listen http_proxy :80 |
| 489 | mode http |
| 490 | cookie SERVERID |
| 491 | |
| 492 | It is possible to change the cookie behaviour to get a smarter persistence, |
| 493 | depending on applications. It is notably possible to delete or modify a cookie |
| 494 | emitted by a server, insert a cookie identifying the server in an HTTP response |
| 495 | and even add a header to tell upstream caches not to cache this response. |
| 496 | |
| 497 | Examples : |
| 498 | ---------- |
| 499 | |
| 500 | To remove the cookie for direct accesses (ie when the server matches the one |
| 501 | which was specified in the client cookie) : |
| 502 | |
| 503 | cookie SERVERID indirect |
| 504 | |
| 505 | To replace the cookie value with the one assigned to the server if any (no |
| 506 | cookie will be created if the server does not provide one, nor if the |
| 507 | configuration does not provide one). This lets the application put the cookie |
| 508 | exactly on certain pages (eg: successful authentication) : |
| 509 | |
| 510 | cookie SERVERID rewrite |
| 511 | |
| 512 | To create a new cookie and assign the server identifier to it (in this case, all |
| 513 | servers should be associated with a valid cookie, since no cookie will simply |
| 514 | delete the cookie from the client's browser) : |
| 515 | |
| 516 | cookie SERVERID insert |
| 517 | |
| 518 | To insert a cookie and ensure that no upstream cache will store it, add the |
| 519 | 'nocache' option : |
| 520 | |
| 521 | cookie SERVERID insert nocache |
| 522 | |
| 523 | To insert a cookie only after a POST request, add 'postonly' after 'insert'. |
| 524 | This has the advantage that there's no risk of caching, and that all pages |
| 525 | seen before the POST one can still be cached : |
| 526 | |
| 527 | cookie SERVERID insert postonly |
| 528 | |
| 529 | Notes : |
| 530 | ----------- |
| 531 | - it is possible to combine 'insert' with 'indirect' or 'rewrite' to adapt to |
| 532 | applications which already generate the cookie with an invalid content. |
| 533 | |
| 534 | - in the case where 'insert' and 'indirect' are both specified, the cookie is |
| 535 | never transmitted to the server, since it wouldn't understand it. This is |
| 536 | the most application-transparent mode. |
| 537 | |
| 538 | - it is particularly recommended to use 'nocache' in 'insert' mode if any |
| 539 | upstream HTTP/1.0 cache is susceptible to cache the result, because this may |
| 540 | lead to many clients going to the same server, or even worse, some clients |
| 541 | having their server changed while retrieving a page from the cache. |
| 542 | |
| 543 | - when the application is well known and controlled, the best method is to |
| 544 | only add the persistence cookie on a POST form because it's up to the |
| 545 | application to select which page it wants the upstream servers to cache. |
| 546 | In this case, you would use 'insert postonly indirect'. |
| 547 | |
| 548 | 2.10) Associating a cookie value with a server |
| 549 | ---------------------------------------------- |
| 550 | In HTTP mode, it's possible to associate a cookie value to each server. This |
| 551 | was initially used in combination with 'dispatch' mode to handle direct accesses |
| 552 | but it is now the standard way of doing the load balancing. The syntax is : |
| 553 | |
| 554 | server <identifier> <address>:<port> cookie <value> |
| 555 | |
| 556 | - <identifier> is any name which can be used to identify the server in the logs. |
| 557 | - <address>:<port> specifies where the server is bound. |
| 558 | - <value> is the value to put in or to read from the cookie. |
| 559 | |
| 560 | Example : the 'SERVERID' cookie can be either 'server01' or 'server02' |
| 561 | --------- |
| 562 | listen http_proxy :80 |
| 563 | mode http |
| 564 | cookie SERVERID |
| 565 | dispatch 192.168.1.100:80 |
| 566 | server web1 192.168.1.1:80 cookie server01 |
| 567 | server web2 192.168.1.2:80 cookie server02 |
| 568 | |
| 569 | Warning : the syntax has changed since version 1.0 ! |
| 570 | --------- |
| 571 | |
| 572 | 3) Autonomous load balancer |
| 573 | =========================== |
| 574 | |
| 575 | The proxy can perform the load-balancing itself, both in TCP and in HTTP modes. |
| 576 | This is the most interesting mode which obsoletes the old 'dispatch' mode |
| 577 | described above. It has advantages such as server health monitoring, multiple |
| 578 | port binding and port mapping. To use this mode, the 'balance' keyword is used, |
| 579 | followed by the selected algorithm. As of version 1.1.23, only 'roundrobin' is |
| 580 | available, which is also the default value if unspecified. In this mode, there |
| 581 | will be no dispatch address, but the proxy needs at least one server. |
| 582 | |
| 583 | Example : same as the last one, with internal load balancer |
| 584 | --------- |
| 585 | |
| 586 | listen http_proxy :80 |
| 587 | mode http |
| 588 | cookie SERVERID |
| 589 | balance roundrobin |
| 590 | server web1 192.168.1.1:80 cookie server01 |
| 591 | server web2 192.168.1.2:80 cookie server02 |
| 592 | |
| 593 | |
| 594 | Since version 1.1.22, it is possible to automatically determine on which port |
| 595 | the server will get the connection, depending on the port the client connected |
| 596 | to. Indeed, there now are 4 possible combinations for the server's <port> field: |
| 597 | |
| 598 | - unspecified or '0' : |
| 599 | the connection will be sent to the same port as the one on which the proxy |
| 600 | received the client connection itself. |
| 601 | |
| 602 | - numerical value (the only one supported in versions earlier than 1.1.22) : |
| 603 | the connection will always be sent to the specified port. |
| 604 | |
| 605 | - '+' followed by a numerical value : |
| 606 | the connection will be sent to the same port as the one on which the proxy |
| 607 | received the connection, plus this value. |
| 608 | |
| 609 | - '-' followed by a numerical value : |
| 610 | the connection will be sent to the same port as the one on which the proxy |
| 611 | received the connection, minus this value. |
| 612 | |
| 613 | Examples : |
| 614 | ---------- |
| 615 | |
| 616 | # same as previous example |
| 617 | |
| 618 | listen http_proxy :80 |
| 619 | mode http |
| 620 | cookie SERVERID |
| 621 | balance roundrobin |
| 622 | server web1 192.168.1.1 cookie server01 |
| 623 | server web2 192.168.1.2 cookie server02 |
| 624 | |
| 625 | # simultaneous relaying of ports 80, 81 and 8080-8089 |
| 626 | |
| 627 | listen http_proxy :80,:81,:8080-8089 |
| 628 | mode http |
| 629 | cookie SERVERID |
| 630 | balance roundrobin |
| 631 | server web1 192.168.1.1 cookie server01 |
| 632 | server web2 192.168.1.2 cookie server02 |
| 633 | |
| 634 | # relaying of TCP ports 25, 389 and 663 to ports 1025, 1389 and 1663 |
| 635 | |
| 636 | listen http_proxy :25,:389,:663 |
| 637 | mode tcp |
| 638 | balance roundrobin |
| 639 | server srv1 192.168.1.1:+1000 |
| 640 | server srv2 192.168.1.2:+1000 |
| 641 | |
| 642 | |
| 643 | 3.1) Servers monitoring |
| 644 | ----------------------- |
| 645 | |
| 646 | It is possible to check the servers status by trying to establish TCP |
| 647 | connections or even sending HTTP requests to them. A server which fails to |
| 648 | reply to health checks as expected will not be used by the load balancing |
| 649 | algorithms. To enable monitoring, add the 'check' keyword on a server line. |
| 650 | It is possible to specify the interval between tests (in milliseconds) with |
| 651 | the 'inter' parameter, the number of failures supported before declaring that |
| 652 | the server has fallen down with the 'fall' parameter, and the number of valid |
| 653 | checks needed for the server to fully get up with the 'rise' parameter. Since |
| 654 | version 1.1.22, it is also possible to send checks to a different port |
| 655 | (mandatory when none is specified) with the 'port' parameter. The default |
| 656 | values are the following ones : |
| 657 | |
| 658 | - inter : 2000 |
| 659 | - rise : 2 |
| 660 | - fall : 3 |
| 661 | - port : default server port |
| 662 | |
| 663 | The default mode consists in establishing TCP connections only. But in certain |
| 664 | types of application failures, it is often that the server continues to accept |
| 665 | connections because the system does it itself while the application is running |
| 666 | an endless loop, or is completely stuck. So in version 1.1.16 were introduced |
| 667 | HTTP health checks which only performed simple lightweight requests and analysed |
| 668 | the response. Now, as of version 1.1.23, it is possible to change the HTTP |
| 669 | method, the URI, and the HTTP version string (which even allows to send headers |
| 670 | with a dirty trick). To enable HTTP health-checks, use 'option httpchk'. |
| 671 | |
| 672 | By default, requests use the 'OPTIONS' method because it's very light and easy |
| 673 | to filter from logs, and does it on '/'. Only HTTP responses 2xx and 3xx are |
| 674 | considered valid ones, and only if they come before the time to send a new |
| 675 | request is reached ('inter' parameter). If some servers block this type of |
| 676 | request, 3 other forms help to forge a request : |
| 677 | |
| 678 | - option httpchk -> OPTIONS / HTTP/1.0 |
| 679 | - option httpchk URI -> OPTIONS <URI> HTTP/1.0 |
| 680 | - option httpchk METH URI -> <METH> <URI> HTTP/1.0 |
| 681 | - option httpchk METH URI VER -> <METH> <URI> <VER> |
| 682 | |
| 683 | See examples below. |
| 684 | |
| 685 | Since version 1.1.17, it is possible to specify backup servers. These servers |
| 686 | are only sollicited when no other server is available. This may only be useful |
| 687 | to serve a maintenance page, or define one active and one backup server (seldom |
| 688 | used in TCP mode). To make a server a backup one, simply add the 'backup' option |
| 689 | on its line. These servers also support cookies, so if a cookie is specified for |
| 690 | a backup server, clients assigned to this server will stick to it even when the |
| 691 | other ones come back. Conversely, if no cookie is assigned to such a server, |
| 692 | the clients will get their cookies removed (empty cookie = removal), and will |
| 693 | be balanced against other servers once they come back. Please note that there |
| 694 | is no load-balancing among backup servers. If there are several backup servers, |
| 695 | the second one will only be used when the first one dies, and so on. |
| 696 | |
| 697 | Since version 1.1.17, it is also possible to visually check the status of all |
| 698 | servers at once. For this, you just have to send a SIGHUP signal to the proxy. |
| 699 | The servers status will be dumped into the logs at the 'notice' level, as well |
| 700 | as on <stderr> if not closed. For this reason, it's always a good idea to have |
| 701 | one local log server at the 'notice' level. |
| 702 | |
| 703 | Examples : |
| 704 | ---------- |
| 705 | # same setup as in paragraph 3) with TCP monitoring |
| 706 | listen http_proxy 0.0.0.0:80 |
| 707 | mode http |
| 708 | cookie SERVERID |
| 709 | balance roundrobin |
| 710 | server web1 192.168.1.1:80 cookie server01 check |
| 711 | server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 |
| 712 | |
| 713 | # same with HTTP monitoring via 'OPTIONS / HTTP/1.0' |
| 714 | listen http_proxy 0.0.0.0:80 |
| 715 | mode http |
| 716 | cookie SERVERID |
| 717 | balance roundrobin |
| 718 | option httpchk |
| 719 | server web1 192.168.1.1:80 cookie server01 check |
| 720 | server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 |
| 721 | |
| 722 | # same with HTTP monitoring via 'OPTIONS /index.html HTTP/1.0' |
| 723 | listen http_proxy 0.0.0.0:80 |
| 724 | mode http |
| 725 | cookie SERVERID |
| 726 | balance roundrobin |
| 727 | option httpchk /index.html |
| 728 | server web1 192.168.1.1:80 cookie server01 check |
| 729 | server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 |
| 730 | |
| 731 | # same with HTTP monitoring via 'HEAD /index.jsp? HTTP/1.1\r\nHost: www' |
| 732 | listen http_proxy 0.0.0.0:80 |
| 733 | mode http |
| 734 | cookie SERVERID |
| 735 | balance roundrobin |
| 736 | option httpchk HEAD /index.jsp? HTTP/1.1\r\nHost:\ www |
| 737 | server web1 192.168.1.1:80 cookie server01 check |
| 738 | server web2 192.168.1.2:80 cookie server02 check inter 500 rise 1 fall 2 |
| 739 | |
| 740 | # automatic insertion of a cookie in the server's response, and automatic |
| 741 | # deletion of the cookie in the client request, while asking upstream caches |
| 742 | # not to cache replies. |
| 743 | listen web_appl 0.0.0.0:80 |
| 744 | mode http |
| 745 | cookie SERVERID insert nocache indirect |
| 746 | balance roundrobin |
| 747 | server web1 192.168.1.1:80 cookie server01 check |
| 748 | server web2 192.168.1.2:80 cookie server02 check |
| 749 | |
| 750 | # same with off-site application backup and local error pages server |
| 751 | listen web_appl 0.0.0.0:80 |
| 752 | mode http |
| 753 | cookie SERVERID insert nocache indirect |
| 754 | balance roundrobin |
| 755 | server web1 192.168.1.1:80 cookie server01 check |
| 756 | server web2 192.168.1.2:80 cookie server02 check |
| 757 | server web-backup 192.168.2.1:80 cookie server03 check backup |
| 758 | server web-excuse 192.168.3.1:80 check backup |
| 759 | |
| 760 | # SMTP+TLS relaying with heakth-checks and backup servers |
| 761 | |
| 762 | listen http_proxy :25,:587 |
| 763 | mode tcp |
| 764 | balance roundrobin |
| 765 | server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2 |
| 766 | server srv2 192.168.1.2 backup |
| 767 | |
| 768 | |
| 769 | 3.2) Redistribute connections in case of failure |
| 770 | ------------------------------------------------ |
| 771 | In HTTP mode, if a server designated by a cookie does not respond, the clients |
| 772 | may definitely stick to it because they cannot flush the cookie, so they will |
| 773 | not be able to access the service anymore. Specifying 'redispatch' will allow |
| 774 | the proxy to break their persistence and redistribute them to working servers. |
| 775 | |
| 776 | Example : |
| 777 | --------- |
| 778 | listen http_proxy 0.0.0.0:80 |
| 779 | mode http |
| 780 | cookie SERVERID |
| 781 | dispatch 192.168.1.100:80 |
| 782 | server web1 192.168.1.1:80 cookie server01 |
| 783 | server web2 192.168.1.2:80 cookie server02 |
| 784 | redispatch # send back to dispatch in case of connection failure |
| 785 | |
| 786 | Up to, and including version 1.1.16, this parameter only applied to connection |
| 787 | failures. Since version 1.1.17, it also applies to servers which have been |
| 788 | detected as failed by the health check mechanism. Indeed, a server may be broken |
| 789 | but still accepting connections, which would not solve every case. But it is |
| 790 | possible to conserve the old behaviour, that is, make a client insist on trying |
| 791 | to connect to a server even if it is said to be down, by setting the 'persist' |
| 792 | option : |
| 793 | |
| 794 | listen http_proxy 0.0.0.0:80 |
| 795 | mode http |
| 796 | option persist |
| 797 | cookie SERVERID |
| 798 | dispatch 192.168.1.100:80 |
| 799 | server web1 192.168.1.1:80 cookie server01 |
| 800 | server web2 192.168.1.2:80 cookie server02 |
| 801 | redispatch # send back to dispatch in case of connection failure |
| 802 | |
| 803 | |
| 804 | 4) Additionnal features |
| 805 | ======================= |
| 806 | |
| 807 | Other features are available. They are transparent mode, event logging and |
| 808 | header rewriting/filtering. |
| 809 | |
| 810 | 4.1) Transparent mode |
| 811 | --------------------- |
| 812 | In HTTP mode, the 'transparent' keyword allows to intercept sessions which are |
| 813 | routed through the system hosting the proxy. This mode was implemented as a |
| 814 | replacement for the 'dispatch' mode, since connections without cookie will be |
| 815 | sent to the original address while known cookies will be sent to the servers. |
| 816 | This mode implies that the system can redirect sessions to a local port. |
| 817 | |
| 818 | Example : |
| 819 | --------- |
| 820 | listen http_proxy 0.0.0.0:65000 |
| 821 | mode http |
| 822 | transparent |
| 823 | cookie SERVERID |
| 824 | server server01 192.168.1.1:80 |
| 825 | server server02 192.168.1.2:80 |
| 826 | |
| 827 | # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \ |
| 828 | --dport 80 -j REDIRECT --to-ports 65000 |
| 829 | |
| 830 | Note : |
| 831 | ------ |
| 832 | If the port is left unspecified on the server, the port the client connected to |
| 833 | will be used. This allows to relay a full port range without using transparent |
| 834 | mode nor thousands of file descriptors, provided that the system can redirect |
| 835 | sessions to local ports. |
| 836 | |
| 837 | Example : |
| 838 | --------- |
| 839 | # redirect all ports to local port 65000, then forward to the server on the |
| 840 | # original port. |
| 841 | listen http_proxy 0.0.0.0:65000 |
| 842 | mode tcp |
| 843 | server server01 192.168.1.1 check port 60000 |
| 844 | server server02 192.168.1.2 check port 60000 |
| 845 | |
| 846 | # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \ |
| 847 | -j REDIRECT --to-ports 65000 |
| 848 | |
| 849 | |
| 850 | 4.2) Event logging |
| 851 | ------------------ |
| 852 | |
| 853 | |
| 854 | - 8< - - - 8< - - - 8< - - - 8< - - - 8< - - - 8< - - - |
| 855 | |
| 856 | |
| 857 | |
| 858 | Les connexions TCP et HTTP peuvent donner lieu à une journalisation sommaire ou |
| 859 | détaillée indiquant, pour chaque connexion, la date, l'heure, l'adresse IP |
| 860 | source, le serveur destination, la durée de la connexion, les temps de réponse, |
| 861 | la requête HTTP, le code de retour, la quantité de données transmises, et même |
| 862 | dans certains cas, la valeur d'un cookie permettant de suivre les sessions. |
| 863 | Tous les messages sont envoyés en syslog vers un ou deux serveurs. Se référer à |
| 864 | la section 1.1 pour plus d'information sur les catégories de logs. La syntaxe |
| 865 | est la suivante : |
| 866 | |
| 867 | log <adresse_ip_1> <catégorie_1> [niveau_max_1] |
| 868 | log <adresse_ip_2> <catégorie_2> [niveau_max_2] |
| 869 | ou |
| 870 | log global |
| 871 | |
| 872 | Remarque : |
| 873 | ---------- |
| 874 | La syntaxe spécifique 'log global' indique que l'on souhaite utiliser les |
| 875 | paramètres de journalisation définis dans la section 'global'. |
| 876 | |
| 877 | Exemple : |
| 878 | --------- |
| 879 | listen http_proxy 0.0.0.0:80 |
| 880 | mode http |
| 881 | log 192.168.2.200 local3 |
| 882 | log 192.168.2.201 local4 |
| 883 | |
| 884 | Par défaut, les informations contenues dans les logs se situent au niveau TCP |
| 885 | uniquement. Il faut préciser l'option 'httplog' pour obtenir les détails du |
| 886 | protocole HTTP. Dans les cas où un mécanisme de surveillance effectuant des |
| 887 | connexions et déconnexions fréquentes, polluerait les logs, il suffit d'ajouter |
| 888 | l'option 'dontlognull', pour ne plus obtenir une ligne de log pour les sessions |
| 889 | n'ayant pas donné lieu à un échange de données (requête ou réponse). |
| 890 | |
| 891 | Exemple : |
| 892 | --------- |
| 893 | listen http_proxy 0.0.0.0:80 |
| 894 | mode http |
| 895 | option httplog |
| 896 | option dontlognull |
| 897 | log 192.168.2.200 local3 |
| 898 | |
| 899 | Depuis la version 1.1.18, un indicateur de complétude de la session a été ajouté |
| 900 | dans les logs HTTP. C'est un champ de 4 caractères précédant la requête HTTP, |
| 901 | indiquant : |
| 902 | - sur le premier caractère, un code précisant le premier événement qui a causé |
| 903 | la terminaison de la session : |
| 904 | |
| 905 | C : fermeture de la session TCP de la part du client |
| 906 | S : fermeture de la session TCP de la part du serveur, ou refus de connexion |
| 907 | P : terminaison prématurée des sessions par le proxy, pour cas d'erreur |
| 908 | interne ou de configuration (ex: filtre d'URL) |
| 909 | c : expiration du délai d'attente côté client : clitimeout |
| 910 | s : expiration du délai d'attente côté serveur: srvtimeout et contimeout |
| 911 | - : terminaison normale. |
| 912 | |
| 913 | - sur le second caractère, l'état d'avancement de la session HTTP lors de la |
| 914 | fermeture : |
| 915 | |
| 916 | R : terminaison en attendant la réception totale de la requête du client |
| 917 | C : terminaison en attendant la connexion vers le serveur |
| 918 | H : terminaison en attendant la réception totale des entêtes du serveur |
| 919 | D : terminaison durant le transfert des données du serveur vers le client |
| 920 | L : terminaison durant le transfert des dernières données du proxy vers |
| 921 | le client, alors que le serveur a déjà fini. |
| 922 | - : terminaison normale, après fin de transfert des données |
| 923 | |
| 924 | - le troisième caractère indique l'éventuelle identification d'un cookie de |
| 925 | persistence : |
| 926 | |
| 927 | N : aucun cookie de persistence n'a été présenté. |
| 928 | I : le client a présenté un cookie ne correspondant à aucun serveur |
| 929 | connu. |
| 930 | D : le client a présenté un cookie correspondant à un serveur hors |
| 931 | d'usage. Suivant l'option 'persist', il a été renvoyé vers un |
| 932 | autre serveur ou a tout de même tenté de se connecter sur celui |
| 933 | correspondant au cookie. |
| 934 | V : le client a présenté un cookie valide et a pu se connecter au |
| 935 | serveur correspondant. |
| 936 | - : non appliquable |
| 937 | |
| 938 | - le dernier caractère indique l'éventuel traitement effectué sur un cookie de |
| 939 | persistence retrourné par le serveur : |
| 940 | |
| 941 | N : aucun cookie de persistence n'a été fourni par le serveur. |
| 942 | P : un cookie cookie de persistence n'a été fourni par le serveur. |
| 943 | I : aucun cookie n'a été fourni par le serveur, il a été inséré par le |
| 944 | proxy. |
| 945 | D : le cookie présenté par le serveur a été supprimé par le proxy pour |
| 946 | ne pas être retourné au client. |
| 947 | R : le cookie retourné par le serveur a été modifié par le proxy. |
| 948 | - : non appliquable |
| 949 | |
| 950 | Le mot clé "capture" permet d'ajouter dans des logs HTTP des informations |
| 951 | capturées dans les échanges. La version 1.1.17 supporte uniquement une capture |
| 952 | de cookies client et serveur, ce qui permet dans bien des cas, de reconstituer |
| 953 | la session d'un utilisateur. La syntaxe est la suivante : |
| 954 | |
| 955 | capture cookie <préfixe_cookie> len <longueur_capture> |
| 956 | |
| 957 | Le premier cookie dont le nom commencera par <préfixe_cookie> sera capturé, et |
| 958 | transmis sous la forme "NOM=valeur", sans toutefois, excéder <longueur_capture> |
| 959 | caractères (64 au maximum). Lorsque le nom du cookie est fixe et connu, on peut |
| 960 | le suffixer du signe "=" pour s'assurer qu'aucun autre cookie ne prendra sa |
| 961 | place dans les logs. |
| 962 | |
| 963 | Exemples : |
| 964 | ---------- |
| 965 | # capture du premier cookie dont le nom commence par "ASPSESSION" |
| 966 | capture cookie ASPSESSION len 32 |
| 967 | |
| 968 | # capture du premier cookie dont le nom est exactement "vgnvisitor" |
| 969 | capture cookie vgnvisitor= len 32 |
| 970 | |
| 971 | Dans les logs, le champ précédant l'indicateur de complétude contient le cookie |
| 972 | positionné par le serveur, précédé du cookie positionné par le client. Chacun de |
| 973 | ces champs est remplacé par le signe "-" lorsqu'aucun cookie n'est fourni par le |
| 974 | client ou le serveur. |
| 975 | |
| 976 | Enfin, l'option 'forwardfor' ajoute l'adresse IP du client dans un champ |
| 977 | 'X-Forwarded-For' de la requête, ce qui permet à un serveur web final de |
| 978 | connaître l'adresse IP du client initial. |
| 979 | |
| 980 | Exemple : |
| 981 | --------- |
| 982 | listen http_proxy 0.0.0.0:80 |
| 983 | mode http |
| 984 | log global |
| 985 | option httplog |
| 986 | option dontlognull |
| 987 | option forwardfor |
| 988 | capture cookie userid= len 20 |
| 989 | |
| 990 | |
| 991 | 4.3) Modification des entêtes HTTP |
| 992 | ---------------------------------- |
| 993 | En mode HTTP uniquement, il est possible de remplacer certains en-têtes dans la |
| 994 | requête et/ou la réponse à partir d'expressions régulières. Il est également |
| 995 | possible de bloquer certaines requêtes en fonction du contenu des en-têtes ou de |
| 996 | la requête. Une limitation cependant : les en-têtes fournis au milieu de |
| 997 | connexions persistentes (keep-alive) ne sont pas vus car ils sont considérés |
| 998 | comme faisant partie des échanges de données consécutifs à la première requête. |
| 999 | Les données ne sont pas affectées, ceci ne s'applique qu'aux en-têtes. |
| 1000 | |
| 1001 | La syntaxe est : |
| 1002 | reqadd <string> pour ajouter un en-tête dans la requête |
| 1003 | reqrep <search> <replace> pour modifier la requête |
| 1004 | reqirep <search> <replace> idem sans distinction majuscules/minuscules |
| 1005 | reqdel <search> pour supprimer un en-tête dans la requête |
| 1006 | reqidel <search> idem sans distinction majuscules/minuscules |
| 1007 | reqallow <search> autoriser la requête si un entête valide <search> |
| 1008 | reqiallow <search> idem sans distinction majuscules/minuscules |
| 1009 | reqdeny <search> interdire la requête si un entête valide <search> |
| 1010 | reqideny <search> idem sans distinction majuscules/minuscules |
| 1011 | reqpass <search> inhibe ces actions sur les entêtes validant <search> |
| 1012 | reqipass <search> idem sans distinction majuscules/minuscules |
| 1013 | |
| 1014 | rspadd <string> pour ajouter un en-tête dans la réponse |
| 1015 | rsprep <search> <replace> pour modifier la réponse |
| 1016 | rspirep <search> <replace> idem sans distinction majuscules/minuscules |
| 1017 | rspdel <search> pour supprimer un en-tête dans la réponse |
| 1018 | rspidel <search> idem sans distinction majuscules/minuscules |
| 1019 | |
| 1020 | |
| 1021 | <search> est une expression régulière compatible POSIX regexp supportant le |
| 1022 | groupage par parenthèses (sans les '\'). Les espaces et autres séparateurs |
| 1023 | doivent êtres précédés d'un '\' pour ne pas être confondus avec la fin de la |
| 1024 | chaîne. De plus, certains caractères spéciaux peuvent être précédés d'un |
| 1025 | backslach ('\') : |
| 1026 | |
| 1027 | \t pour une tabulation |
| 1028 | \r pour un retour charriot |
| 1029 | \n pour un saut de ligne |
| 1030 | \ pour différencier un espace d'un séparateur |
| 1031 | \# pour différencier un dièse d'un commentaire |
| 1032 | \\ pour utiliser un backslash dans la regex |
| 1033 | \\\\ pour utiliser un backslash dans le texte |
| 1034 | \xXX pour un caractère spécifique XX (comme en C) |
| 1035 | |
| 1036 | |
| 1037 | <replace> contient la chaîne remplaçant la portion vérifiée par l'expression. |
| 1038 | Elle peut inclure les caractères spéciaux ci-dessus, faire référence à un |
| 1039 | groupe délimité par des parenthèses dans l'expression régulière, par sa |
| 1040 | position numérale. Les positions vont de 1 à 9, et sont codées par un '\' |
| 1041 | suivi du chiffre désiré. Il est également possible d'insérer un caractère non |
| 1042 | imprimable (utile pour le saut de ligne) inscrivant '\x' suivi du code |
| 1043 | hexadécimal de ce caractère (comme en C). |
| 1044 | |
| 1045 | <string> représente une chaîne qui sera ajoutée systématiquement après la |
| 1046 | dernière ligne d'en-tête. |
| 1047 | |
| 1048 | Remarques : |
| 1049 | --------- |
| 1050 | - la première ligne de la requête et celle de la réponse sont traitées comme |
| 1051 | des en-têtes, ce qui permet de réécrire des URL et des codes d'erreur. |
| 1052 | - 'reqrep' est l'équivalent de 'cliexp' en version 1.0, et 'rsprep' celui de |
| 1053 | 'srvexp'. Ces noms sont toujours supportés mais déconseillés. |
| 1054 | - pour des raisons de performances, le nombre total de caractères ajoutés sur |
| 1055 | une requête ou une réponse est limité à 4096 depuis la version 1.1.5 (cette |
| 1056 | limite était à 256 auparavant). Cette valeur est modifiable dans le code. |
| 1057 | Pour un usage temporaire, on peut gagner de la place en supprimant quelques |
| 1058 | entêtes inutiles avant les ajouts. |
| 1059 | |
| 1060 | Exemples : |
| 1061 | -------- |
| 1062 | reqrep ^(GET.*)(.free.fr)(.*) \1.online.fr\3 |
| 1063 | reqrep ^(POST.*)(.free.fr)(.*) \1.online.fr\3 |
| 1064 | reqirep ^Proxy-Connection:.* Proxy-Connection:\ close |
| 1065 | rspirep ^Server:.* Server:\ Tux-2.0 |
| 1066 | rspirep ^(Location:\ )([^:]*://[^/]*)(.*) \1\3 |
| 1067 | rspidel ^Connection: |
| 1068 | rspadd Connection:\ close |
| 1069 | |
| 1070 | |
| 1071 | 4.4) Répartition avec persistence |
| 1072 | --------------------------------- |
| 1073 | |
| 1074 | La combinaison de l'insertion de cookie avec la répartition de charge interne |
| 1075 | permet d'assurer une persistence dans les sessions HTTP d'une manière |
| 1076 | pratiquement transparente pour les applications. Le principe est simple : |
| 1077 | - attribuer une valeur d'un cookie à chaque serveur |
| 1078 | - effectuer une répartition interne |
| 1079 | - insérer un cookie dans les réponses issues d'une répartition uniquement, |
| 1080 | et faire en sorte que des caches ne mémorisent pas ce cookie. |
| 1081 | - cacher ce cookie à l'application lors des requêtes ultérieures. |
| 1082 | |
| 1083 | Exemple : |
| 1084 | --------- |
| 1085 | listen application 0.0.0.0:80 |
| 1086 | mode http |
| 1087 | cookie SERVERID insert nocache indirect |
| 1088 | balance roundrobin |
| 1089 | server 192.168.1.1:80 cookie server01 check |
| 1090 | server 192.168.1.2:80 cookie server02 check |
| 1091 | |
| 1092 | 4.5) Personalisation des erreurs |
| 1093 | -------------------------------- |
| 1094 | |
| 1095 | Certaines situations conduisent à retourner une erreur HTTP au client : |
| 1096 | - requête invalide ou trop longue => code HTTP 400 |
| 1097 | - requête mettant trop de temps à venir => code HTTP 408 |
| 1098 | - requête interdite (bloquée par un reqideny) => code HTTP 403 |
| 1099 | - erreur interne du proxy => code HTTP 500 |
| 1100 | - le serveur a retourné une réponse incomplète ou invalide => code HTTP 502 |
| 1101 | - aucun serveur disponible pour cette requête => code HTTP 503 |
| 1102 | - le serveur n'a pas répondu dans le temps imparti => code HTTP 504 |
| 1103 | |
| 1104 | Un message d'erreur succint tiré de la RFC accompagne ces codes de retour. |
| 1105 | Cependant, en fonction du type de clientèle, on peut préférer retourner des |
| 1106 | pages personnalisées. Ceci est possible par le biais de la commande "errorloc" : |
| 1107 | |
| 1108 | errorloc <code_HTTP> <location> |
| 1109 | |
| 1110 | Au lieu de générer une erreur HTTP <code_HTTP> parmi les codes cités ci-dessus, |
| 1111 | le proxy génèrera un code de redirection temporaire (HTTP 302) vers l'adresse |
| 1112 | d'une page précisée dans <location>. Cette adresse peut être relative au site, |
| 1113 | ou absolue. Comme cette réponse est traîtée par le navigateur du client |
| 1114 | lui-même, il est indispensable que l'adresse fournie lui soit accessible. |
| 1115 | |
| 1116 | Exemple : |
| 1117 | --------- |
| 1118 | listen application 0.0.0.0:80 |
| 1119 | errorloc 400 /badrequest.html |
| 1120 | errorloc 403 /forbidden.html |
| 1121 | errorloc 408 /toolong.html |
| 1122 | errorloc 500 http://haproxy.domain.net/bugreport.html |
| 1123 | errorloc 502 http://192.168.114.58/error50x.html |
| 1124 | errorloc 503 http://192.168.114.58/error50x.html |
| 1125 | errorloc 504 http://192.168.114.58/error50x.html |
| 1126 | |
| 1127 | 4.6) Changement des valeurs par défaut |
| 1128 | -------------------------------------- |
| 1129 | |
| 1130 | Dans la version 1.1.22 est apparue la notion de valeurs par défaut, ce qui évite |
| 1131 | de répéter des paramètres communs à toutes les instances, tels que les timeouts, |
| 1132 | adresses de log, modes de fonctionnement, etc. |
| 1133 | |
| 1134 | Les valeurs par défaut sont positionnées dans la dernière section 'defaults' |
| 1135 | précédent l'instance qui les utilisera. On peut donc mettre autant de sections |
| 1136 | 'defaults' que l'on veut. Il faut juste se rappeler que la présence d'une telle |
| 1137 | section implique une annulation de tous les paramètres par défaut positionnés |
| 1138 | précédemment, dans le but de les remplacer. |
| 1139 | |
| 1140 | La section 'defaults' utilise la même syntaxe que la section 'listen', aux |
| 1141 | paramètres près qui ne sont pas supportés. Le mot clé 'defaults' peut accepter |
| 1142 | un commentaire en guise paramètre. |
| 1143 | |
| 1144 | Dans la version 1.1.22, seuls les paramètres suivants peuvent être positionnés |
| 1145 | dans une section 'defaults' : |
| 1146 | - log (le premier et le second) |
| 1147 | - mode { tcp, http, health } |
| 1148 | - balance { roundrobin } |
| 1149 | - disabled (pour désactiver toutes les instances qui suivent) |
| 1150 | - enabled (pour faire l'opération inverse, mais c'est le cas par défaut) |
| 1151 | - contimeout, clitimeout, srvtimeout, grace, retries, maxconn |
| 1152 | - option { redispatch, transparent, keepalive, forwardfor, httplog, |
| 1153 | dontlognull, persist, httpchk } |
| 1154 | - redispatch, redisp, transparent, source { addr:port } |
| 1155 | - cookie, capture |
| 1156 | - errorloc |
| 1157 | |
| 1158 | Ne sont pas supportés dans cette version, les adresses de dispatch et les |
| 1159 | configurations de serveurs, ainsi que tous les filtres basés sur les |
| 1160 | expressions régulières : |
| 1161 | - dispatch, server, |
| 1162 | - req*, rsp*, |
| 1163 | |
| 1164 | Enfin, il n'y a pas le moyen, pour le moment, d'invalider un paramètre booléen |
| 1165 | positionné par défaut. Donc si une option est spécifiée dans les paramètres par |
| 1166 | défaut, le seul moyen de la désactiver pour une instance, c'est de changer les |
| 1167 | paramètres par défaut avant la déclaration de l'instance. |
| 1168 | |
| 1169 | Exemples : |
| 1170 | ---------- |
| 1171 | defaults applications TCP |
| 1172 | log global |
| 1173 | mode tcp |
| 1174 | balance roundrobin |
| 1175 | clitimeout 180000 |
| 1176 | srvtimeout 180000 |
| 1177 | contimeout 4000 |
| 1178 | retries 3 |
| 1179 | redispatch |
| 1180 | |
| 1181 | listen app_tcp1 10.0.0.1:6000-6063 |
| 1182 | server srv1 192.168.1.1 check port 6000 inter 10000 |
| 1183 | server srv2 192.168.1.2 backup |
| 1184 | |
| 1185 | listen app_tcp2 10.0.0.2:6000-6063 |
| 1186 | server srv1 192.168.2.1 check port 6000 inter 10000 |
| 1187 | server srv2 192.168.2.2 backup |
| 1188 | |
| 1189 | defaults applications HTTP |
| 1190 | log global |
| 1191 | mode http |
| 1192 | option httplog |
| 1193 | option forwardfor |
| 1194 | option dontlognull |
| 1195 | balance roundrobin |
| 1196 | clitimeout 20000 |
| 1197 | srvtimeout 20000 |
| 1198 | contimeout 4000 |
| 1199 | retries 3 |
| 1200 | |
| 1201 | listen app_http1 10.0.0.1:80-81 |
| 1202 | cookie SERVERID postonly insert indirect |
| 1203 | capture cookie userid= len 10 |
| 1204 | server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000 |
| 1205 | server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000 |
| 1206 | |
| 1207 | defaults |
| 1208 | # section vide qui annule tous les paramètes par défaut. |
| 1209 | |
| 1210 | ======================= |
| 1211 | | Paramétrage système | |
| 1212 | ======================= |
| 1213 | |
| 1214 | Sous Linux 2.4 |
| 1215 | ============== |
| 1216 | |
| 1217 | -- cut here -- |
| 1218 | #!/bin/sh |
| 1219 | # set this to about 256/4M (16384 for 256M machine) |
| 1220 | MAXFILES=16384 |
| 1221 | echo $MAXFILES > /proc/sys/fs/file-max |
| 1222 | ulimit -n $MAXFILES |
| 1223 | |
| 1224 | if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then |
| 1225 | echo 65536 > /proc/sys/net/ipv4/ip_conntrack_max |
| 1226 | fi |
| 1227 | |
| 1228 | if [ -e /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait ]; then |
| 1229 | # 30 seconds for fin, 15 for time wait |
| 1230 | echo 3000 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_fin_wait |
| 1231 | echo 1500 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_timeout_time_wait |
| 1232 | echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_invalid_scale |
| 1233 | echo 0 > /proc/sys/net/ipv4/netfilter/ip_ct_tcp_log_out_of_window |
| 1234 | fi |
| 1235 | |
| 1236 | echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range |
| 1237 | echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout |
| 1238 | echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog |
| 1239 | echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets |
| 1240 | echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans |
| 1241 | echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time |
| 1242 | echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle |
| 1243 | echo 0 > /proc/sys/net/ipv4/tcp_timestamps |
| 1244 | echo 0 > /proc/sys/net/ipv4/tcp_ecn |
| 1245 | echo 0 > /proc/sys/net/ipv4/tcp_sack |
| 1246 | echo 0 > /proc/sys/net/ipv4/tcp_dsack |
| 1247 | |
| 1248 | # auto-tuned on 2.4 |
| 1249 | #echo 262143 > /proc/sys/net/core/rmem_max |
| 1250 | #echo 262143 > /proc/sys/net/core/rmem_default |
| 1251 | |
| 1252 | echo 16384 65536 524288 > /proc/sys/net/ipv4/tcp_rmem |
| 1253 | echo 16384 349520 699040 > /proc/sys/net/ipv4/tcp_wmem |
| 1254 | |
| 1255 | -- cut here -- |
| 1256 | |
| 1257 | -- fin -- |