haproxy中的Proxies段的配置

在Haproxy中的代理的配置包括4个模块,defaults、listen、frontend、backend。

defaults:用于为所有其它配置段提供默认参数,listen、frontend、backend将使用defaults中的设置,如果想通的参数被重复设置,那么listen、frontend、backend的优先级高于defaults.

frontend:监听客户端的链接。

backend:代理的后端服务器。

listen:包括监听客户端和代理的后端服务器,是一个完整的代理的模块,通常使用在tcp的模式中。

Tips 1:所有的代理名称是由大小字母、数字、:、-、_ 组成,且不区分大小写。

Tips 2:如果使用相同的代理名称,那么使用haproxy -f [confFile] -c 命令检查,会报错。

一、前后端连接模型

  1. KAL: keep alive( option http-keep-alive ),这是默认的模式,所有的请求和响应都会被 HAProxy 处理,且允许在没有请求和响应时保持空闲的连接
  2. TUN:tunnel( option http-tunnel ):这是 1.0 ~ 1.5-dev21 的默认模式,类似于隧道,HAProxy 仅处理第一个请求和响应,剩余的报文将直接转发而不进行处理。尽量不要使用这个模式,因为它在日志记录和 HTTP 处理上有很多问题。
  3. PCL:passive close( option httpclose ),这和 tunnel 模式类似,区别是 HAProxy 会在发往客户端的响应报文和发往服务器的请求报文中加入 “Connection: close” 首部,使得客户端和后端主机在完成与 HAProxy 的一次通信后主动的关闭连接。
  4. SCL:server close( option http-server-close ),HAProxy 在接收到后端服务器的响应后就立即断开与后端服务器的连接,而与客户端的连接则使用保持连接。
  5. FCL:forced close( option forceclose ),HAProxy 每完成一次与客户端/服务器的通信(请求+响应)后就主动关闭连接。

二、配置参数详解释

acl <aclname> <criterion> [flags] [operator] <value> …

参考:Haproxy中的acl的详解

appsession <cookie> len <length> timeout <holdtime> [request-learn] [prefix] [mode <path-parameters|query-string>] – 已废弃

backlog <conns>

防止SYN攻击,最大值是32768,这里可以查阅SYN Flood攻击的相关资料。这个参数仅在defaults、listen、frontend的模块内使用。

balance <algorithm> [ <arguments> ]
balance url_param <param> [check_post]

algorithm:英文释义算法,这里负载的算法,一共包括8种:

  1. roundrobin:表示简单的轮询,每个服务器根据权重轮流使用,在服务器的处理时间平均分配的情况下这是最流畅和公平的算法。该算法是动态的,对于实例启动慢的服务器权重会在运行中调整。
  2. static-rr:每个服务器根据权重轮流使用,类似roundrobin,但它是静态的,意味着运行时修改权限是无效的。另外,它对服务器的数量没有限制。该算法一般不用;
  3. first:可用连接槽的第一个服务器接收连接。并且在第一个服务器恢复之后,能够主动切回来,对于MySQL主主代理来说,绝对是一个福音。
  4. leastconn:连接数最少的服务器优先接收连接。leastconn建议用于长会话服务,例如LDAP、SQL、TSE等,而不适合短会话协议。如HTTP.该算法是动态的,对于实例启动慢的服务器权重会在运行中调整。
  5. source:对请求源IP地址进行哈希,用可用服务器的权重总数除以哈希值,根据结果进行分配。只要服务器正常,同一个客户端IP地址总是访问同一个服务器。如果哈希的结果随可用服务器数量而变化,那么客户端会定向到不同的服务器;该算法一般用于不能插入cookie的Tcp模式。它还可以用于广域网上为拒绝使用会话cookie的客户端提供最有效的粘连;该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
  6. uri:表示根据请求的URI左端(问号之前)进行哈希,用可用服务器的权重总数除以哈希值,根据结果进行分配。只要服务器正常,同一个URI地址总是访问同一个服务器。一般用于代理缓存和反病毒代理,以最大限度的提高缓存的命中率。该算法只能用于HTTP后端;该算法一般用于后端是缓存服务器;该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
  7. url_param:在HTTP GET请求的查询串中查找<param>中指定的URL参数,基本上可以锁定使用特制的URL到特定的负载均衡器节点的要求;该算法一般用于将同一个用户的信息发送到同一个后端服务器;该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
  8. hdr(name):在每个HTTP请求中查找HTTP头<name>,HTTP头<name>将被看作在每个HTTP请求,并针对特定的节点;如果缺少头或者头没有任何值,则用roundrobin代替;该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
  9. rdp-cookie(name):为每个进来的TCP请求查询并哈希RDP cookie<name>;该机制用于退化的持久模式,可以使同一个用户或者同一个会话ID总是发送给同一台服务器。如果没有cookie,则使用roundrobin算法代替;该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。

举例说明:

balance roundrobin
balance url_param userid
balance url_param session_id check_post 64
balance hdr(User-Agent)
balance hdr(host)
balance hdr(Host) use_domain_only

bind [<address>]:<port_range> [, …] [param*]
bind /<path> [, …] [param*]

举例说明

listen http_proxy
    bind :80,:443
    bind 10.0.0.1:10080,10.0.0.1:10443
    bind /var/run/ssl-frontend.sock user root mode 600 accept-proxy

listen http_https_proxy
    bind :80
    bind :443 ssl crt /etc/haproxy/site.pem

listen http_https_proxy_explicit
    bind ipv6@:80
    bind ipv4@public_ssl:443 ssl crt /etc/haproxy/site.pem
    bind unix@ssl-frontend.sock user root mode 600 accept-proxy

listen external_bind_app1
    bind "fd@${FD_APP1}"

 bind-process [ all | odd | even | <number 1-64>[-<number 1-64>] ] …

绑定进程。可以参考:Haproxy中 global 部分的配置,文章中关于cpu-map的介绍。

举例说明:

listen app_ip1
    bind 10.0.0.1:80
    bind-process odd

listen app_ip2
    bind 10.0.0.2:80
    bind-process even

listen management
    bind 10.0.0.3:80
    bind-process 1 2 3 4

listen management
    bind 10.0.0.4:80
    bind-process 1-4

block { if | unless } <condition> – 废弃

capture cookie <name> len <length>
capture request header <name> len <length>
capture response header <name> len <length>

捕捉相应的信息到日志中。

clitimeout <timeout>  – 废弃
contimeout <timeout> – 废弃

compression algo <algorithm> …
compression type <mime type> …
compression offload

启动响应压缩,压缩的算法:identity、gzip、deflate、raw-deflate。

例子:

compression algo deflate
compression type text/html text/plain

 cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ][ postonly ] [ preserve ] [ httponly ] [ secure ]
              [ domain <domain> ]* [ maxidle <idle> ] [ maxlife <life> ]

haproxy中cookie的控制。

declare capture [ request | response ] len <length>

default-server [param*]

default_backend <backend>
use_backend <backend> [{if | unless} <condition>]

use_backend     dynamic  if  url_dyn
use_backend     static   if  url_css url_img extension_img
default_backend dynamic

use-server <server> if <condition>
use-server <server> unless <condition>

# intercept incoming TLS requests based on the SNI field
use-server www if { req_ssl_sni -i www.example.com }
server     www 192.168.0.1:443 weight 0
use-server mail if { req_ssl_sni -i mail.example.com }
server     mail 192.168.0.1:587 weight 0
use-server imap if { req_ssl_sni -i imap.example.com }
server     mail 192.168.0.1:993 weight 0
# all the rest is forwarded to this server
server  default 192.168.0.2:443 check

description <string>

disabled
enabled

开启/关闭代理,frontend、backend、listen

dispatch <address>:<port>

errorfile <code> <file>
errorloc <code> <url>
errorloc302 <code> <url>
errorloc303 <code> <url>

可设置在globals、listen、frontend、backend
errorfile 400 /etc/haproxy/errorfiles/400badreq.http
errorfile 408 /dev/null  # work around Chrome pre-connect bug
errorfile 403 /etc/haproxy/errorfiles/403forbid.http
errorfile 503 /etc/haproxy/errorfiles/503sorry.http

email-alert from <emailaddr>
email-alert level <level>
email-alert mailers <mailersect>
email-alert myhostname <hostname>
email-alert to <emailaddr>

参考:Haproxy中 global 部分的配置 中的email部分。

force-persist { if | unless } <condition>

filter <name> [param*]

listen
  bind *:80
  filter trace name BEFORE-HTTP-COMP
  filter compression
  filter trace name AFTER-HTTP-COMP
  compression algo gzip
  compression offload
  server srv1 192.168.0.1:80

fullconn <conns>

后端服务器的最大链接。

# The servers will accept between 100 and 1000 concurrent connections each
# and the maximum of 1000 will be reached when the backend reaches 10000
# connections.
backend dynamic
   fullconn   10000
   server     srv1   dyn1:80 minconn 100 maxconn 1000
   server     srv2   dyn2:80 minconn 100 maxconn 1000

grace <time>

hash-balance-factor <factor>
hash-type <method> <function> <modifier>

http-check disable-on-404
http-check expect [!] <match> <pattern>
http-check send-state

http健康检测中,状态为404认为是合法的。

# only accept status 200 as valid
http-check expect status 200

# consider SQL errors as errors
http-check expect ! string SQL\ Error

# consider status 5xx only as errors
http-check expect ! rstatus ^5

# check that we have a correct hexadecimal tag before /html
http-check expect rstring <!--tag:[0-9a-f]*</html>

http-request { allow | tarpit | auth [realm <realm>] | redirect <rule> |
              deny [deny_status <status>] |
              add-header <name> <fmt> | set-header <name> <fmt> |
              capture <sample> [ len <length> | id <id> ] |
              del-header <name> | set-nice <nice> | set-log-level <level> |
              replace-header <name> <match-regex> <replace-fmt> |
              replace-value <name> <match-regex> <replace-fmt> |
              set-method <fmt> | set-path <fmt> | set-query <fmt> |
              set-uri <fmt> | set-tos <tos> | set-mark <mark> |
              add-acl(<file name>) <key fmt> |
              del-acl(<file name>) <key fmt> |
              del-map(<file name>) <key fmt> |
              set-map(<file name>) <key fmt> <value fmt> |
              set-var(<var name>) <expr> |
              unset-var(<var name>) |
              { track-sc0 | track-sc1 | track-sc2 } <key> [table <table>] |
              sc-inc-gpc0(<sc-id>) |
              sc-set-gpt0(<sc-id>) <int> |
              silent-drop |
             }
             [ { if | unless } <condition> ]

7层负载的请求控制。

http-response { allow | deny | add-header <name> <fmt> | set-nice <nice> |
                capture <sample> id <id> | redirect <rule> |
                set-header <name> <fmt> | del-header <name> |
                replace-header <name> <regex-match> <replace-fmt> |
                replace-value <name> <regex-match> <replace-fmt> |
                set-status <status> [reason <str>] |
                set-log-level <level> | set-mark <mark> | set-tos <tos> |
                add-acl(<file name>) <key fmt> |
                del-acl(<file name>) <key fmt> |
                del-map(<file name>) <key fmt> |
                set-map(<file name>) <key fmt> <value fmt> |
                set-var(<var-name>) <expr> |
                unset-var(<var-name>) |
                { track-sc0 | track-sc1 | track-sc2 } <key> [table <table>] |
                sc-inc-gpc0(<sc-id>) |
                sc-set-gpt0(<sc-id>) <int> |
                silent-drop |
              }
              [ { if | unless } <condition> ]

7层负载响应控制

http-reuse { never | safe | aggressive | always }
http-send-name-header [<header>]

id <value>

proxy的唯一之标识。

ignore-persist { if | unless } <condition>

load-server-state-from-file { global | local | none }

log global
log <address> [len <length>] <facility> [<level> [<minlevel>]]
no log
log-format <string>
log-format-sd <string>
log-tag <string>

日志的设置。

max-keep-alive-queue <value>

maxconn <conns>

mode { tcp|http|health }

monitor fail { if | unless } <condition>
monitor-net <source>
monitor-uri <uri>

Add a condition to report a failure to a monitor HTTP request.

frontend www
   mode http
   acl site_dead nbsrv(dynamic) lt 2
   acl site_dead nbsrv(static)  lt 2
   monitor-uri   /site_alive
   monitor fail  if site_dead
# addresses .252 and .253 are just probing us.
frontend www
    monitor-net 192.168.0.252/31
# Use /haproxy_test to report haproxy's status
frontend www
    mode http
    monitor-uri /haproxy_test

option abortonclose
no option abortonclose

option accept-invalid-http-request
no option accept-invalid-http-request
option accept-invalid-http-response
no option accept-invalid-http-response

option allbackups
no option allbackups

Use either all backup servers at a time or only the first one

option checkcache
no option checkcache

Analyze all server responses and block responses with cacheable cookies

option clitcpka
no option clitcpka

Enable or disable the sending of TCP keepalive packets on the client side

option contstats

Enable continuous traffic statistics updates

option dontlog-normal
no option dontlog-normal
option dontlognull
no option dontlognull

Enable or disable logging of normal, successful connections

option forceclose
no option forceclose

Enable or disable active connection closing after response is transferred.

haproxy中的Proxies段的配置》上有1条评论

  1. Pingback引用通告: Haproxy配置-新手入门篇 | 精彩每一天

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>