haproxy中的Proxies段的配置_haproxy server-template-程序员宅基地

技术标签: linux运维  

在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 [flags] [operator] …
    声明访问控制列表
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | yes
acl invalid_src src 0.0.0.0/7 224.0.0.0/3
acl invalid_src src_port 0:1023
acl local_dst hdr(host) -i localhost
  • backlog
    防止SYN洪水攻击,解决方案是加大SYN backlog值,默认情况haproxy传递frontend中的maxconn。最大值是32768,这里可以查阅SYN Flood攻击的相关资料。可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | no
    参数:
    conns 挂起连接数量,取决于操作系统。

  • balance [ ]

  • balance url_param [check_post]
    定义后端使用的负载算法。可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes

    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请求的查询串中查找中指定的URL参数,基本上可以锁定使用特制的URL到特定的负载均衡器节点的要求;该算法一般用于将同一个用户的信息发送到同一个后端服务器;该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
    8、hdr(name):在每个HTTP请求中查找HTTP头,HTTP头将被看作在每个HTTP请求,并针对特定的节点;如果缺少头或者头没有任何值,则用roundrobin代替;该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。
    9、rdp-cookie(name):为每个进来的TCP请求查询并哈希RDP cookie;该机制用于退化的持久模式,可以使同一个用户或者同一个会话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*]
    在frontend中定义一个或多个监听地址或端口
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | no
 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>] ] …:
    将实例绑定到相应的处理器
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    与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 }

  • capture cookie len
    在请求和响应中捕获并记录一个cookie。如果头中没有此cookie将会记录“-”。最大capture的长度是tune.http.cookielen。默认63个字符。
    参数说明:
    name 是cookie名称的开头,为了精确匹配cookie可使用“=”
    length 记录的最大长度,包括cookiename,如果长度超限将被截取。
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | no

capture cookie ASPSESSION len 32
  • capture request header len
    捕获并记录指定的请求头的最后一次发生。日志中该值将会被{}括起来,如果有多个值将会使用“|”分隔。如果不存在将会是空字符串,一般使用的有host记录虚拟头部信息,content-length上传的时候使用,user-agent能快速区分是用户还是爬虫,x-forwarded-for能识别用户 来源。
    参数说明:
    name 是捕获的头部名称,不区分大小写。名称将不会在日志中记录,只有值会被记录
    length 日志中记录的最大长度,如果超长将会被截取。
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | no
capture request header Host len 15
capture request header X-Forwarded-For len 15
capture request header Referer len 15
  • capture response header len
    捕获并记录指定响应头的最后一次。日志中该值将会被{}括起来,如果有多个值将会使用“|”分隔。如果不存在将会是空字符串,content-length 一般用于分析返回的bytes,location去追踪跳转。
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | no
    参数说明:
    name 是捕获的头部名称,不区分大小写。名称将不会在日志中记录,只有值会被记录
    length 日志中记录的最大长度,如果超长将会被截取。
capture response header Content-length len 9
capture response header Location len 15
  • compression algo
  • compression type …
  • compression offload
    启用 http压缩,压缩取决于请求中的accept-encoding头。如果后端使用了压缩,则haproxy将不会压缩,使用offload,将会把所有的压缩工作都由haproxy来完成,
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    参数说明:
    algo: 支持的压缩算法
    type: 哪些mime类型将会被压缩
    offload : 使得haproxy仅作为压缩卸载器工作
    压缩的算法:
    identity:通常用于debugging
    gzip:使用gzip压缩,仅当安装时设置了支持zlib或libzl时才可用
    deflate:类似与gzip,适用与很多的浏览器,但是最新浏览器不支持。
    raw-deflate:所有主流浏览器都支持。
    以下情形将不会被压缩:
    1、请求头中accept-encoding并没有明确指定压缩算法
    2、响应不是HTTP/1.1
    3、http状态码不是200
    4、响应中不包含content-length、transfer-encoding中的任何一个。
    5、响应头content-type值为multipart
    6、响应头中cache-control值为no-transform
    7、user-agent使用“Mozilla/4”
    8、响应已经被压缩
    9、响应包含ETag头
compression algo deflate
compression type text/html text/plain
  • cookie [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
    [ postonly ] [ preserve ] [ httponly ] [ secure ]
    [ domain ]* [ maxidle ] [ maxlife ]
    [ dynamic ]
    启用基于cookie的稳定连接后端
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    每一个后端只能有一个固定cookie,并且可以在defaults区域中定义,它的值在“server”声明语句中的“cookie”中定义。
cookie JSESSIONID prefix
cookie SRV insert indirect nocache
cookie SRV insert postonly indirect
cookie SRV insert indirect nocache maxidle 30m maxlife 8h
  • declare capture [ request | response ] len
    定义一个捕获槽
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | no
    参数说明:
    length: 允许捕获的长度
    该声明只能在frontend和listen中使用,但是它可在backend中进行访问。
  • default-server [param*]
    更改后台服务器的默认选项
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    参数说明:
    param*:此server的参数列表,
default-server inter 1000 weight 13
  • default_backend
    指定默认的后端服务器池,也就是指定一组后端真实服务器,而这些真实服务器组将在backend段进行定义。当use_backend规则中没有匹配的时候使用该规则
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | no
use_backend dynamic if url_dyn
use_backend static is usl_caa_url_img extension_img
default_backend dynamic
  • use_backend [{if | unless} ]
use_backend     dynamic  if  url_dyn
use_backend     static   if  url_css url_img extension_img
default_backend dynamic
  • use-server if
  • use-server unless
# 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
    描述字符串,将在haproxy stats 中展示相关的描述。
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | yes

  • disabled
    禁用代理,前端或后端。
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    “disable”关键字用于禁用实例,比如临时停用某个服务,实例会被创建,配置也会被检查,但是将会被标记为stopped。它将不会接收任何的流量和健康检查会日志。将所有都禁用则可在default区域中配置disable

  • enabled
    启用代理,前端或后端。
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    当defaults中设置为disable,可使用enable来启用

  • dispatch {address}:{port}
    设置默认服务器地址
    可用区域:
    defaults | frontend | listen | backend
    no | no | yes | yes
    “dispatch”定义了一个默认的服务,当其他的服务都不能进行连接,建议不要使用它。

  • dynamic-cookie-key
    给后端设置动态cookie 秘钥。
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    当动态cookie被启用,则每台服务都会使用IP地址的hash,port,和secret key创建动态cookie。

  • errorfile
    返回文件内容而不是由HAProxy生成的错误
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    参数说明:
    code: 是http状态码,目前支持:200,400,403,405,425,429,500,502,503,504
    file:文件包含完整的http响应内容,建议使用“.http”后缀,并使用绝对路径。

  • errorloc

  • errorloc302

  • errorloc303
    将HTTP重定向返回到URL而不是由HAProxy生成的错误。
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | yes

#可设置在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
  • force-persist { if | unless }
    当满足条件时强制分配连接到后端服务器,无论是否down。
    可用区域:
    defaults | frontend | listen | backend
    no | no | yes | yes
    默认请求是不会转发到down服务器,可使用“option persist”来强制转发,但是它是无条件的,
  • filter [param*]
    在附加到代理的过滤器列表中添加过滤器。
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | yes
    参数说明:
    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
    指定的后端负载服务器的minconn最小连接,和maxconn最大连接。
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    默认,haproxy将改值设置为maxconns的10%
backend dynamic
   fullconn   10000
   server     srv1   dyn1:80 minconn 100 maxconn 1000
   server     srv2   dyn2:80 minconn 100 maxconn 1000
  • grace

  • hash-balance-factor
    指定一致性散列hash负载,将设置为0(默认值)将禁用此功能。
    可用区域:
    defaults | frontend | listen | backend
    yes | no | no | yes

  • hash-type
    指定用于将哈希映射到服务器的方法
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    method可用值:map-based、consistent、
    function可用值:sdbm、djb2、wt6、crc32
    modifier可用值:avalanche、

  • http-check disable-on-404
    在HTTP/404响应健康检查时启用维护模式
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    如果配置此选项,检查后端返回404的服务将会被移出负载。但是仍然可以接收persistent 连接,它提供了一个很方便的方式去停用服务。

  • http-check expect [!]
    使HTTP健康检查考虑回应内容或特定状态代码
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    参数说明:
    match: 是从响应中查找模式的关键字,可用:status,rstatus,string,rstring
    pattern: 查找模式。
    默认情况下,“option httpchk”认为响应码为2xx和3xx表示正常,其他表示不可用,当启用“http-check expect”它会自定义什么是可用或不可用,

需要注意响应大小被限制为“tune.chksize”默认为16384 bytes。同时该模式不支持keep-alive,他会自动添加头“connection:close”

# 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-check send-state
    启用使用HTTP健康检查发送state 头
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    当设置该值,haproxy将会添加一个头“X-Haproxy-Server_State”
X-Haproxy-Server-State: UP 2/3; name=bck/srv2;node=lb1; weight=1/2;scur=13/22; qcur=0
  • http-request [options…] [ { if | unless } ]
    处理7层请求
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | yes
acl nagios src 192.168.129.3
acl local_net src 192.168.0.0/16
acl auth_ok http_auth(L1)

http-request allow if nagios
http-request allow if local_net auth_ok
http-request auth realm Gimme if local_net auth_ok
http-request deny

acl key req.hdr(X-Add-Acl-Key) -m found
acl add path /addacl
acl del path /delacl

acl myhost hdr(Host) -f myhost.lst

http-request add-acl(myhost.lst) %[req.hdr(X-Add-Acl-Key)] if key add
http-request del-acl(myhost.lst) %[req.hdr(X-Add-Acl-Key)] if key del

acl value  req.hdr(X-Value) -m found
acl setmap path /setmap
acl delmap path /delmap

use_backend bk_appli if {
     hdr(Host),map_str(map.lst) -m found }

http-request set-map(map.lst) %[src] %[req.hdr(X-Value)] if setmap value
http-request del-map(map.lst) %[src] if delmap                
  • http-request add-acl() [ { if | unless } ]
    它被用于项acl文件中添加新的行,参数key fmt 允许使用log-format规则去收集内容。在插入之前执行查找操作,避免重复值。
  • http-request add-header [ { if | unless } ]
    它添加头部到请求头中,允许使用log-format中的参数。在向服务器中传递特定值时很有用,此规则会立即生效。
  • http-request allow [ { if | unless } ]
    使请求通过检查。
  • http-request auth [realm ] [ { if | unless } ]
    让用户输入可用的用户名和密码。
 acl auth_ok http_auth_group(L1) G1
 http-request auth unless auth_ok
  • http-request cache-use [ { if | unless } ]
  • http-request capture [ len | id ][ { if | unless } ]
    这个捕获sample从请求buffer中得到,并且将其转化为length长度的string。这个结果将会保存到下一个请求的capture slot中,
  • http-request del-acl() [ { if | unless } ]
    从acl文件中删除行
  • http-request del-header [ { if | unless } ]
    删除头
  • http-request del-map() [ { if | unless } ]
    从map文件中删除
  • http-request deny [deny_status ] [ { if | unless } ]
    拒绝请求,并返回403错误。
  • http-request disable-l7-retry [ { if | unless } ]
    将禁止失败连接重试连接,这可用于让post请求失败后不能重试。
  • http-request do-resolve(,,[ipv4,ipv6])
    用于dns解析。
	resolvers mydns
      nameserver local 127.0.0.53:53
      nameserver google 8.8.8.8:53
      timeout retry   1s
      hold valid 10s
      hold nx 3s
      hold other 3s
      hold obsolete 0s
      accepted_payload_size 8192

    frontend fe
      bind 10.42.0.1:80
      http-request do-resolve(txn.myip,mydns,ipv4) hdr(Host),lower
      http-request capture var(txn.myip) len 40

      # return 503 when the variable is not set,
      # which mean DNS resolution error
      use_backend b_503 unless {
     var(txn.myip) -m found }

      default_backend be

    backend b_503
      # dummy backend used to return 503.
      # one can use the errorfile directive to send a nice
      # 503 error page to end users

    backend be
      # rule to prevent HAProxy from reconnecting to services
      # on the local network (forged DNS name used to scan the network)
      http-request deny if {
     var(txn.myip) -m ip 127.0.0.0/8 10.0.0.0/8 }
      http-request set-dst var(txn.myip)
      server clear 0.0.0.0:0
  • http-request early-hint [ { if | unless } ]
    它用于在任何其他响应之前构建HTTP 103早期提示响应。这将向此响应追加一个HTTP头字段,name在name参数,value在fmt参数。这在需要预先加载资源和渲染html文档时特别有用。
  • http-request redirect [ { if | unless } ]
    基于规则进行转发,这和redirect一样,
  • http-request reject [ { if | unless } ]
    立即关闭连接,并且无任何响应。
  • http-request replace-header [ { if | unless } ]
    替换头部值,大小写敏感,匹配项将会完全被replace-fmt替换。
http-request replace-header Cookie foo=([^;]*);(.*) foo=\1;ip=%bi;\2
# applied to:
    Cookie: foo=foobar; expires=Tue, 14-Jun-2016 01:40:45 GMT;

    # outputs:
    Cookie: foo=foobar;ip=192.168.1.20; expires=Tue, 14-Jun-2016 01:40:45 GMT;

http-request replace-header User-Agent curl foo

    # applied to:
    User-Agent: curl/7.47.0

    # outputs:
    User-Agent: foo
  • http-request replace-uri [ { if | unless } ]
    这和replace-header类似,uri部分包括optional scheme,authority or query string。
 # prefix /foo : turn /bar?q=1 into /foo/bar?q=1 :
    http-request replace-uri (.*) /foo\1

    # suffix /foo : turn /bar?q=1 into /bar/foo?q=1 :
    http-request replace-uri ([^?]*)(\?(.*))? \1/foo\2

    # strip /foo : turn /foo/bar?q=1 into /bar?q=1
    http-request replace-uri /foo/(.*) /\1
    # or more efficient if only some requests match :
    http-request replace-uri /foo/(.*) /\1 if {
     url_beg /foo/ }
  • http-request replace-value [ { if | unless } ]
    和replace-header类似,但是它只针对头部值。
 http-request replace-value X-Forwarded-For ^192\.168\.(.*)$ 172.16.\1

    # applied to:
    X-Forwarded-For: 192.168.10.1, 192.168.13.24, 10.0.0.37

    # outputs:
    X-Forwarded-For: 172.16.10.1, 172.16.13.24, 10.0.0.37
  • http-request sc-inc-gpc0() [ { if | unless } ]
  • http-request sc-inc-gpc1() [ { if | unless } ]
    这个操作会增加gpc0和gpc1计数器的值,
  • http-request sc-set-gpt0() {|}[{if |unless}]
    设置计数器gpt0的值,返回boolean
  • http-request set-dst [ { if | unless } ]
    设置目的地址,在haproxy中重写目的地址很有用。
http-request set-dst hdr(x-dst)
http-request set-dst dst,ipmask(24)
  • http-request set-dst-port [ { if | unless } ]
    设置目标端口
http-request set-dst-port hdr(x-port)
http-request set-dst-port int(4000)
  • http-request set-header [ { if | unless } ]
    如果头存在会先移除再添加。
		http-request set-header X-Haproxy-Current-Date %T
        http-request set-header X-SSL                  %[ssl_fc]
        http-request set-header X-SSL-Session_ID       %[ssl_fc_session_id,hex]
        http-request set-header X-SSL-Client-Verify    %[ssl_c_verify]
        http-request set-header X-SSL-Client-DN        %{
    +Q}[ssl_c_s_dn]
        http-request set-header X-SSL-Client-CN        %{
    +Q}[ssl_c_s_dn(cn)]
        http-request set-header X-SSL-Issuer           %{
    +Q}[ssl_c_i_dn]
        http-request set-header X-SSL-Client-NotBefore %{
    +Q}[ssl_c_notbefore]
        http-request set-header X-SSL-Client-NotAfter  %{
    +Q}[ssl_c_notafter]
  • http-request set-log-level [ { if | unless } ]
    改变日志级别
  • http-request set-map() [ { if | unless } ]
    添加新行到map文件中,添加前会查询是否重复。
  • http-request set-mark [ { if | unless } ]
  • http-request set-method [ { if | unless } ]
    重写请求方法。
  • http-request set-nice [ { if | unless } ]
    设置当前请求的亲和度,它只对当前进行的其他请求有影响,默认值为0 ,取值范围:-1024…1024。值越低,则当前请求更重要。可用于提高请求速度。
  • http-request set-path [{ if | unless } ]
    重写request请求path,其他部分将保持原样
http-request set-path /%[hdr(host)]%[path]
  • http-request set-priority-class [ { if | unless } ]
    设置当前请求队列的优先级。取值区域:-524287…524287、值越低优先级越高。
  • http-request set-query [ { if | unless } ]
    设置查询参数。
http-request set-query %[query,regsub(%3D,=,g)]
  • http-request set-src [ { if | unless } ]
    设置源地址。
http-request set-src hdr(x-forwarded-for)
http-request set-src src,ipmask(24)
  • http-request set-src-port [ { if | unless } ]
    设置源端口
http-request set-src-port hdr(x-port)
http-request set-src-port int(4000)
  • http-request set-tos [ { if | unless } ]
    设置TOS或DSCP值
  • http-request set-uri [ { if | unless } ]
    替换uri,包括scheme,authority,path,query。
  • http-request set-var() [ { if | unless } ]
    替换变量
    参数说明:
    varname:变量名称以区域开头,允许的区域包括:
  1. “proc”:进程内都可访问的变量
  2. “sess”:整个session都可访问的变量
  3. “txn”:整个transaction都可访问的变量
  4. “req”:整个请求内都可访问的变量
  5. “res”:整个响应内可访问的变量
http-request set-var(req.my_var) req.fhdr(user-agent),lower
  • http-request send-spoe-group [ { if | unless } ]
    发送spoe消息

  • http-request silent-drop [ { if | unless } ]
    立即停掉客户端请求,但是连接仍在。

  • http-request tarpit [deny_status ] [ { if | unless } ]
    阻止请求,无返回直到超时。可用于阻止爬虫

  • http-request track-sc0 [table

    ] [ { if | unless } ]

  • http-request track-sc1 [table

    ] [ { if | unless } ]

  • http-request track-sc2 [table

    ] [ { if | unless } ]
    追踪当前请求的计数器。

  • http-request unset-var() [ { if | unless } ]
    取消变量

http-request unset-var(req.my_var)
  • http-request use-service [ { if | unless } ]
    直接使用服务响应请求。这个服务可用lua定义的服务。
    或定义promerheus监控
http-request use_service prometheus-exporter if {
    path /metrics}
  • http-request wait-for-handshake [ { if | unless } ]

直到ssl握手完成才进行下一步

  • http-response <options…> [ { if | unless } ]
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | yes
	acl key_acl res.hdr(X-Acl-Key) -m found

    acl myhost hdr(Host) -f myhost.lst

    http-response add-acl(myhost.lst) %[res.hdr(X-Acl-Key)] if key_acl
    http-response del-acl(myhost.lst) %[res.hdr(X-Acl-Key)] if key_acl
    
    acl value  res.hdr(X-Value) -m found

    use_backend bk_appli if {
     hdr(Host),map_str(map.lst) -m found }

    http-response set-map(map.lst) %[src] %[res.hdr(X-Value)] if value
    http-response del-map(map.lst) %[src]                     if ! value
  • http-response add-acl() [ { if | unless } ]
  • http-response add-header [ { if | unless } ]
  • http-response allow [ { if | unless } ]
  • http-response cache-store [ { if | unless } ]
  • http-response capture id [ { if | unless } ]
  • http-response del-acl() [ { if | unless } ]
  • http-response del-header [ { if | unless } ]
  • http-response del-map() [ { if | unless } ]
  • http-response deny [ { if | unless } ]
  • http-response redirect [ { if | unless } ]
  • http-response replace-header [ { if | unless } ]
  • http-response replace-value [ { if | unless } ]
  • http-response sc-inc-gpc0() [ { if | unless } ]
  • http-response sc-inc-gpc1() [ { if | unless } ]
  • http-response sc-set-gpt0() { | } [ { if | unless } ]
  • http-response send-spoe-group [ { if | unless } ]
  • http-response set-header [ { if | unless } ]
  • http-response set-log-level [ { if | unless } ]
  • http-response set-map()
  • http-response set-mark [ { if | unless } ]
  • http-response set-nice [ { if | unless } ]
  • http-response set-status [reason ] [ { if | unless } ]
http-response set-status 503 
http-response set-status 503 reason "slow down"
  • http-response set-tos [ { if | unless } ]

  • http-response set-var() [ { if | unless } ]

  • http-response silent-drop [ { if | unless } ]

  • http-response track-sc0 [table

    ] [ { if | unless } ]

  • http-response track-sc1 [table

    ] [ { if | unless } ]

  • http-response track-sc2 [table

    ] [ { if | unless } ]

  • http-response unset-var() [ { if | unless } ]

  • http-reuse { never | safe | aggressive | always }
    声明请求之间可以共享闲置的HTTP连接
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    默认使用safe策略,
    可用策略:
    1、never: 空闲连接不共享,
    2、safe: 默认策略,一个session的请求只会使用它自己的连接,它的子请求可能分配到其他已存在的连接上,这确保了万一当请求发送后服务关闭了连接,客户端可尝试重试,这和keep-alive类似。
    3、aggressive:
    4、always

  • http-send-name-header [

    ]
    将服务器名称添加到请求。使用由
    给出的标题字符串
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • id :
    proxy 唯一标识
    可用区域:
    defaults | frontend | listen | backend
    no | yes | yes | yes

  • ignore-persist { if | unless }
    声明某种条件下忽略持久性
    可用区域:
    defaults | frontend | listen | backend
    no | no | yes | yes
    默认,当cookie persistent 开启,每个包含cookie的请求都会无条件的持久。

	  acl url_static  path_beg         /static /images /img /css
      acl url_static  path_end         .gif .png .jpg .css .js
      ignore-persist  if url_static
  • load-server-state-from-file { global | local | none }
    允许无缝重新加载HAProxy
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
   	  global
       stats socket /tmp/socket
       server-state-file /tmp/server_state

      defaults
       load-server-state-from-file global

      backend bk
       server s1 127.0.0.1:22 check weight 11
       server s2 127.0.0.1:22 check weight 12
  socat /tmp/socket - <<< "show servers state" > /tmp/server_state
  • log global
  • log
    [len ] [ []]
  • no log
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | yes
 log global
    log stdout format short daemon          # send log to systemd
    log stdout format raw daemon            # send everything to stdout
    log stderr format raw daemon notice     # send important events to stderr
    log 127.0.0.1:514 local0 notice         # only send important events
    log 127.0.0.1:514 local0 notice notice  # same but limit output level
    log "${LOCAL_SYSLOG}:514" local0 notice   # send to local server
  • log-format
    指定用于流量日志的日志格式字符串
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • log-format-sd
    指定RFC5424结构化数据日志格式字符串
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • log-tag
    指定要用于所有传出日志的日志标记
    可用区域:
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • max-keep-alive-queue
    设置维护保持连接的最大服务器队列大小
    可用区域:
    defaults | frontend | listen | backend
    yes | no | yes | yes
    保持会话将尝试复用服务器的连接,但有时候会适得其反,例如一个服务有很多的连接而其他的服务确很闲。
    默认值为-1,表示没有限制大小,

  • max-session-srv-conns
    连接客户端的最大闲置session连接数。默认5

  • maxconn
    设置最大的连接数,默认是2000
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • mode { tcp|http|health }
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • monitor fail { if | unless }
    添加条件以报告监视器HTTP请求的失败。
    defaults | frontend | listen | backend
    no | yes | yes | no

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
  • monitor-net
    声明一个限于监视请求的源网络
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • monitor-uri
    拦截外部组件的监视请求使用的URI
    defaults | frontend | listen | backend
    yes | yes | yes | no

# 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
    启用或禁用轻松解析HTTP请求
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option accept-invalid-http-response

  • no option accept-invalid-http-response
    启用或禁用缓解HTTP响应解析

  • option allbackups

  • no option allbackups
    一次使用所有备份服务器,或仅使用第一个备份服务器
    defaults | frontend | listen | backend
    yes | no | yes | yes
    默认当正常的服务器down掉以后使用第一个backup服务器。有时需要使用多个。

  • option checkcache

  • no option checkcache
    分析所有服务器响应,并使用可缓存的Cookie阻止响应
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option clitcpka

  • no option clitcpka
    启用或禁用在客户端发送TCP Keepalive报文
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option contstats
    启用连续流量统计更新
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option dontlog-normal

  • no option dontlog-normal
    启用或禁用日志记录正常,成功的连接
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option dontlognul

  • no option dontlognull
    启用或禁用日志记录的空连接
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option forceclose

  • no option forceclose
    响应传输后启用或禁用活动连接关闭。

  • option forwardfor [ except ] [ header ] [ if-none ]
    在请求头中插入x-forwarded-for
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    参数说明:
    except network :当匹配此源地址的时候不执行
    name:自定义头名称

frontend www
        mode http
        option forwardfor except 127.0.0.1
backend www
        mode http
        option forwardfor header X-Client
  • option h1-case-adjust-bogus-client

  • no option h1-case-adjust-bogus-client
    开启或关闭发送到伪客户端的头部大小写调整
    defaults | frontend | listen | backend
    yes | yes | yes | no
    头部名称并没有标准的大小写,它对大小写并不敏感,所以应用中必须处理这种情况,但是一些伪客户端的要求一致,尤其在HTTP/2中头部必须小写,所以haproxy中要求所有的头部都用小写传递,不用管http的版本。

  • option h1-case-adjust-bogus-server

  • no option h1-case-adjust-bogus-server
    开启或关闭发送到伪服务器的头部大小写调整
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option http-buffer-request

  • no option http-buffer-request
    启用或禁用等待整个HTTP请求正文,然后继续
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    有时候需要等待整个http请求的body内容再进行下一步。

  • option http-ignore-probes

  • no option http-ignore-probes
    启用或禁用日志记录空连接和请求超时
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option http-keep-alive

  • no option http-keep-alive
    启用或禁用从客户端到服务器的HTTP保持会话
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    默认使用该模式,

  • option http-no-delay

  • no option http-no-delay
    指示系统支持HTTP中的低性能交互延迟
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    不建议使用

  • option http-pretend-keepalive

  • no option http-pretend-keepalive
    定义haproxy是否会向服务器通告keepalive
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option http-server-close

  • no option http-server-close
    在服务器端启用或禁用HTTP连接
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    haproxy默认使用keep-alive,server-close将会开启服务端关闭连接,而客户端抱持会话,将有利于保留服务器端资源,

  • option http-use-proxy-header

  • no option http-use-proxy-header
    使用非标准的代理连接
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option httpchk

  • option httpchk

  • option httpchk

  • option httpchk
    此选项表示启用HTTP的服务状态检测功能。HAProxy作为一款专业的负载均衡器,它支持对backend部分指定的后端服务节点进行健康检查,以保证在后端backend中某个节点不能服务时,把从frontend端进来的客户端请求分配至backend中其他健康节点上,从而保证整体服务的可用性。
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option httpclose

  • no option httpclose
    此选项表示在客户端和服务器完成一次连接请求后,HAProxy将主动关闭http连接.
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    当完成request和response后haproxy关闭连接,并添加头“connection:close”

  • option httplog [clf]
    开启日志记录http请求,session状态和timers
    defaults | frontend | listen | backend
    yes | yes | yes | no
    参数说明:
    clf:如果给出此参数,将使用clf的格式代替默认的http格式
    在默认情况下,日志记录的项目很少,只记录source,destination和instance name,此选项将丰富记录内容,包括请求,连接时间,session状态,连接数,和捕获的头或cookie,前端,后端,和服务名,和源地址和端口。

  • option http_proxy

  • no option http_proxy
    启用或禁用普通HTTP代理模式
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    此模式只工作在传入纯ip地址

  • option independent-streams

  • no option independent-streams
    设置双向tcp读写单独的超时
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • option ldap-check
    使用LDAPv3运行状况检查进行服务器测试
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option external-check
    使用外部进程进行服务器健康检查
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option log-health-checks

  • no option log-health-checks
    启用或禁用日志记录健康检查状态更新
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option log-separate-errors

  • no option log-separate-errors
    为非完全成功的连接更改日志级别
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option logasap

  • no option logasap
    启用或禁用HTTP请求的早期记录
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option mysql-check [ user [ post-41 ] ]
    使用MySQL健康检查进行服务器测试
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option nolinger

  • no option nolinger
    关闭后启用或禁用即时会话资源清理
    defaults | frontend | listen | backend
    yes | yes | yes | yes
    当客户端和服务端不寻常的关闭了 连接
    sessio将会超时并关闭,该选项将强制系统回收资源。不建议使用。

  • option originalto [ except ] [ header ]
    启用将X-Original-To标头插入发送到服务器的请求
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • option persist

  • no option persist
    在下载服务器上启用或禁用强制持久性
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option pgsql-check [ user ]
    使用PostgreSQL运行状况检查进行服务器测试

  • option prefer-last-server

  • no option prefer-last-server
    允许多个负载均衡请求保留在同一台服务器上
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option redispatch

  • option redispatch

  • no option redispatch
    当连接失败的时候重新分配。
    defaults | frontend | listen | backend
    yes | no | yes | yes
    此参数用于cookie保持的环境中。在默认情况下,HAProxy会将其请求的后端服务器的ServerID插入到cookie中,以保证会话的Session持久性。而如果后端服务器出现故障,客户端的cookie是不会刷新的,这就出现了问题。此时,如果设置此参数,就会将客户端的请求强制定向到另外一个健康的后端服务器上,以保证服务的正常。

  • option redis-check
    使用redis健康检查进行服务器测试
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option socket-stats
    为每个套接字收集和提供单独的统计信息。
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • option splice-auto
    在两个方向上启用或禁用套接字上的自动内核加速
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • option splice-request
    套接字上的自动内核加速请求
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • option splice-response
    自动内核加速以进行响应
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • option spop-check
    使用SPOP健康检查进行服务器测试
    defaults | frontend | listen | backend
    no | no | no | yes

  • option srvtcpka
    在服务器端发送TCP Keepalive报文
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option ssl-hello-chk
    使用SSLv3客户端hello运行状况检查进行服务器测试
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • option tcp-check
    使用tcp-check send/expect序列进行健康检查
    defaults | frontend | listen | backend
    yes | no | yes | yes

option tcp-check
tcp-check expect string +OK\ POP3\ ready comment POP\ protocol

option tcp-check
         tcp-check comment PING\ phase
         tcp-check send PING\r\n
         tcp-check expect string +PONG
         tcp-check comment role\ check
         tcp-check send info\ replication\r\n
         tcp-check expect string role:master
         tcp-check comment QUIT\ phase
         tcp-check send QUIT\r\n
         tcp-check expect string +OK
  • option tcp-smart-accept
  • no option tcp-smart-accept
    在接受序列期间启用或禁用一个ACK数据包的保存
    defaults | frontend | listen | backend
    yes | yes | yes | no
  • option tcp-smart-connect
  • no option tcp-smart-connect
  • option tcpka
    启用或禁用双方发送TCP Keepalive报文
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • option tcplog
    启用具有会话状态和定时器的TCP连接的高级日志记录
    defaults | frontend | listen | backend
    yes | yes | yes | no
  • option transparent
    启用客户端透明代理
    defaults | frontend | listen | backend
    yes | no | yes | yes
  • external-check command
    外部检查命令
    defaults | frontend | listen | backend
    yes | no | yes | yes
    环境变量:
    HAPROXY_PROXY_ADDR 绑定的第一个地址
    HAPROXY_PROXY_ID :backend id
    HAPROXY_PROXY_NAME: BACKEND NAME
    HAPROXY_PROXY_PORT: 第一个绑定的端口
    HAPROXY_SERVER_ADDR:服务器地址
    HAPROXY_SERVER_CURCONN:服务器上面当前的连接数
    HAPROXY_SERVER_ID: 服务器id
    HAPROXY_SERVER_MAXCONN: 服务器最大连接数
    HAPROXY_SERVER_NAME :服务器名称
    HAPROXY_SERVER_PORT:服务器端口
    PATH :
  • external-check path
    defaults | frontend | listen | backend
    yes | no | yes | yes
external-check path "/usr/bin:/bin"
  • persist rdp-cookie
  • persist rdp-cookie()
    启用rdp基于cookie的保持会话
    defaults | frontend | listen | backend
    yes | no | yes | yes
    默认cookie名为“msts”
listen tse-farm
            bind :3389
            # wait up to 5s for an RDP cookie in the request
            tcp-request inspect-delay 5s
            tcp-request content accept if RDP_COOKIE
            # apply RDP cookie persistence
            persist rdp-cookie
            # if server is unknown, let's balance on the same cookie.
            # alternatively, "balance leastconn" may be useful too.
            balance rdp-cookie
            server srv1 1.1.1.1:3389
            server srv2 1.1.1.2:3389
  • rate-limit sessions
    限制frontend每秒接受的session数量
    defaults | frontend | listen | backend
    yes | yes | yes | no
    当达到该值,frontend将停止接受新的连接。
listen smtp
            mode tcp
            bind :25
            rate-limit sessions 10
            server smtp1 127.0.0.1:1025
  • redirect location [code ] [{if | unless} ]
  • redirect prefix [code ] [{if | unless} ]
  • redirect scheme [code ] [{if | unless} ]
    当条件满足时进行重定向
    defaults | frontend | listen | backend
    no | yes | yes | yes
    参数说明:
    loc:loc值将会替换头部Location的值,
    pfx:重定向到url
    sch:例如http跳转到https
    code:301,302,303,307,308
    option选项:
    1、drop-query:当应用于prefix 查询参数将会被丢弃。
    2、append-slash: 当取消掉查询参数后,在最后添加“/”
    3、set-cookie name=value:添加cookie
    4、clear-cookie name[=]:清理cookie
 acl clear      dst_port  80
        acl secure     dst_port  8080
        acl login_page url_beg   /login
        acl logout     url_beg   /logout
        acl uid_given  url_reg   /login?userid=[^&]+
        acl cookie_set hdr_sub(cookie) SEEN=1

        redirect prefix   https://mysite.com set-cookie SEEN=1 if !cookie_set
        redirect prefix   https://mysite.com           if login_page !secure
        redirect prefix   http://mysite.com drop-query if login_page !uid_given
        redirect location http://mysite.com/           if !login_page secure
        redirect location / clear-cookie USERID=       if logout

acl missing_slash path_reg ^/article/[^/]*$
redirect code 301 prefix / drop-query append-slash if missing_slash

redirect scheme https if !{
     ssl_fc }
  • retries
    设置连接后端服务器的失败重试次数,
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • retry-on [list of keywords]
    自动重连
    defaults | frontend | listen | backend
    yes | no | yes | yes
    参数说明:
    keyword:
    none:不重试
    conn-failure:当连接或ssl握手失败,默认值
    empty-response:无返回时
    junk-response:当返回并不是完整的http响应
    response-timeout:当返回超时
    0rtt-reject:当请求被拒绝
    status:当某个状态码时
    all-retryable-errors:所有错误发生时,包括以上所有

  • server

    [:port] [param*]
    这个关键字用来定义多个后端真实服务器,
    defaults | frontend | listen | backend
    no | no | yes | yes
    参数说明:
    name:服务器名称,如果设置“http-send-name-header
    ”将会向后端服务器发送该名称
    address:服务器地址
    port:端口号
    param:
    1、check – 表示启用对后端服务器执行健康检查
    2、inter – 设置监看检查的时间间隔,单位为毫秒
    3、rise – 设置从故障状态转换至正常状态需要成功检查的次数。例如: 'rise 2’表示2次检查成功就认为此服务器可用
    4、fall – 设置从正常状态转换为不可用状态需要检查的次数。例如: 'fall 3’表示3次检查失败就认为此服务器不可用
    5、cookie – 为指定的后端服务器设定cookie值,此处指定的值将在请求入站时被检查,第一次为此值挑选的后端服务器将在后面被选中
    6、redir – server srv1 192.168.1.1:80 redir http://test.com

server first  10.1.1.1:1080 cookie first  check inter 1000
        server second 10.1.1.2:1080 cookie second check inter 1000
        server transp ipv4@
        server backup "${SRV_BACKUP}:1080" backup
        server www1_dc1 "${LAN_DC1}.101:80"
        server www1_dc2 "${LAN_DC2}.101:80"
  • server-state-file-name []
    设置服务器状态文件,仅在设置了load-server-state-from-file local 生效
    如果没有给出文件则是后端名称,
 global
      server-state-file-base /etc/haproxy/states

   backend bk
      load-server-state-from-file
  • server-template <num | range> [:] [params*]
    设置一个模板去初始化服务器 ,服务器名称使用prefix和num|range参数来构建。
    defaults | frontend | listen | backend
    no | no | yes | yes
    参数说明:
    prefix:名称前缀
    num| range:名称取值
    fqdn:
    port:server port
    params:服务器中的其他参数
    server-template srv 1-3 google.com:80 check

    # or
    server-template srv 3 google.com:80 check

    # would be equivalent to:
    server srv1 google.com:80 check
    server srv2 google.com:80 check
    server srv3 google.com:80 check
  • source [:] [usesrc { [:] | client | clientip } ]
  • source [:] [usesrc { [:] | hdr_ip([,]) } ]
  • source [:] [interface ]
    为出连接设置源地址:
    defaults | frontend | listen | backend
    yes | no | yes | yes
backend private
            # Connect to the servers using our 192.168.1.200 source address
            source 192.168.1.200

        backend transparent_ssl1
            # Connect to the SSL farm from the client's source address
            source 192.168.1.200 usesrc clientip

        backend transparent_ssl2
            # Connect to the SSL farm from the client's source address and port
            # not recommended if IP conntrack is present on the local machine.
            source 192.168.1.200 usesrc client

        backend transparent_ssl3
            # Connect to the SSL farm from the client's source address. It
            # is more conntrack-friendly.
            source 192.168.1.200 usesrc clientip

        backend transparent_smtp
            # Connect to the SMTP farm from the client's source address/port
            # with Tproxy version 4.
            source 0.0.0.0 usesrc clientip

        backend transparent_http
            # Connect to the servers using the client's IP as seen by previous
            # proxy.
            source 0.0.0.0 usesrc hdr_ip(x-forwarded-for,-1)
  • stats admin { if | unless }
    启用监控页面
    defaults | frontend | listen | backend
    no | yes | yes | yes
backend stats_localhost
        stats enable
        stats admin if LOCALHOST
 backend stats_auth
        stats enable
        stats auth  admin:AdMiN123
        stats admin if TRUE
userlist stats-auth
        group admin    users admin
        user  admin    insecure-password AdMiN123
        group readonly users haproxy
        user  haproxy  insecure-password haproxy

    backend stats_auth
        stats enable
        acl AUTH       http_auth(stats-auth)
        acl AUTH_ADMIN http_auth_group(stats-auth) admin
        stats http-request auth unless AUTH
        stats admin if AUTH_ADMIN
  • stats auth :
    监控界面授权账号
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • stats enable
    默认启用监控界面
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • stats hide-version
    在监控界面上隐藏haproxy版本
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • stats http-request { allow | deny | auth [realm ] }[ { if | unless } ]
    监控界面的访问控制
    defaults | frontend | listen | backend
    no | no | yes | yes
  • stats realm
    开启监控密码界面欢迎语句
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • stats refresh
    定义监控界面自动刷新时间
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • stats scope { | “.” }
    监控界面限制access scope
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • stats show-desc [ ]
    启用监控界面的描述
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • stats show-node [ ]
    监控界面上报hostname
  • stats uri
    定义监控界面uri
    defaults | frontend | listen | backend
    yes | yes | yes | yes
  • stick match [table ] [{if | unless} ]
    当请求符合条件时将用户和服务器进行粘连
    defaults | frontend | listen | backend
    no | no | yes | yes
    一些应用或协议需要复杂的粘连规则,并且不能简单的使用cookie或hash,”stick match“ 可以定义响应的规则。
 backend pop
        mode tcp
        balance roundrobin
        stick store-request src
        stick-table type ip size 200k expire 30m
        server s1 192.168.1.1:110
        server s2 192.168.1.1:110

    backend smtp
        mode tcp
        balance roundrobin
        stick match src table pop
        server s1 192.168.1.1:25
        server s2 192.168.1.1:25
  • stick on [table ] [{if | unless} ]
    定义一个用于用户和服务器连接的请求模式
    defaults | frontend | listen | backend
    no | no | yes | yes
 # The following form ...
    stick on src table pop if !localhost

    # ...is strictly equivalent to this one :
    stick match src table pop if !localhost
    stick store-request src table pop if !localhost
  • stick store-request [table ] [{if | unless} ]
    定义一个请求pattern用于创建和进入stick table
    defaults | frontend | listen | backend
    no | no | yes | yes
    参数说明:
    pattern:请求元素,一旦服务器被选中则存如表中
    table:是stick table 名称。如果没有指定则使用后端表,使用”stick-table“定义粘连表
    该声明决定了什么样的请求去到了什么样的地方。存储到stick table中为了以后stick match 使用。
 backend pop
        mode tcp
        balance roundrobin
        stick store-request src
        stick-table type ip size 200k expire 30m
        server s1 192.168.1.1:110
        server s2 192.168.1.1:110

    backend smtp
        mode tcp
        balance roundrobin
        stick match src table pop
        server s1 192.168.1.1:25
        server s2 192.168.1.1:25
  • stick store-response [table ] [{if | unless} ]
    同上
  # Learn SSL session ID from both request and response and create affinity.
    backend https
        mode tcp
        balance roundrobin
        # maximum SSL session ID length is 32 bytes.
        stick-table type binary len 32 size 30k expire 30m

        acl clienthello req_ssl_hello_type 1
        acl serverhello rep_ssl_hello_type 2

        # use tcp content accepts to detects ssl client and server hello.
        tcp-request inspect-delay 5s
        tcp-request content accept if clienthello

        # no timeout on response inspect delay by default.
        tcp-response content accept if serverhello

        # SSL session ID (SSLID) may be present on a client or server hello.
        # Its length is coded on 1 byte at offset 43 and its value starts
        # at offset 44.

        # Match and learn on request if client hello.
        stick on payload_lv(43,1) if clienthello

        # Learn on response if server hello.
        stick store-response payload_lv(43,1) if serverhello

        server s1 192.168.1.1:443
        server s2 192.168.1.1:443
  • stick-table type {ip | integer | string [len ] | binary [len ]}size [expire ] [nopurge] [peers ][store <data_type>]*
    为当前section定义一张stick table
    defaults | frontend | listen | backend
    no | yes | yes | yes
    参数说明:
    ip:type ip 表示只会存储ipv4地址。通常用于存储客户端ip
    integer :type integer 将会存储32位的的整数,作为客户端的标识
    string:type string 存储length长度的字符串,如果没设置长度,默认32
    binary:type binary 存储length长度的二进制,如果没设置length 默认32bytes
    length:长度
    size:表的大小,这个值直接影响内存的使用,size支持k,m,g
    nopurge: 当表满的时候,默认会替换掉部分老的数据,此参数不会清理老的数据,一定要和expire一起使用
    peersect:是peer section的名称用于复制stick table,
    expire:定义过期时间,如果没有设置则不会过期。
    datatype:存储到stick table中的数据类型。类型包括:
    1、server_id:这是整数,可被用于stick match、stick store和stick on。
    2、gpc0:第一个通用计数器,是32位整数,通常被用于放置特殊的tag给一些entries。
    3、gpc0_rate():通常用于记录当前事件的发生频率。
    4、gpc1:第二个通用计数器,
    5、gpc1_rate():
    6、conn_cnt:连接技术器,接收到的连接数量,
    7、conn_cur:当前连接.
    8、conn_rate():频率计数器。指定间隔period间的连接数
    9、sess_cnt:session计数器。用于4层
    10、sess_rate()
    11、http_req_cnt:http请求计数器。
    12、http_req_rate():请求频率
    13、http_err_cnt:http错误计数器
    14、http_err_rate():错误频率
    15、bytes_in_cnt:客户端到服务端的byte 计数器
    16、bytes_in_rate():byte频率
    17、bytes_out_cnt:服务端到客户端byte计数器
    18、byte_out_rate():byte频率
    每一个proxy只有一张stick table。但是stickness有一些限制,每次重启将会丢失。还有就是内存的消耗。
# Keep track of counters of up to 1 million IP addresses over 5 minutes
        # and store a general purpose counter and the average connection rate
        # computed over a sliding window of 30 seconds.
stick-table type ip size 1m expire 5m store gpc0,conn_rate(30s)
  • tcp-check connect [params*]
    开启新的连接
    defaults | frontend | listen | backend
    no | no | yes | yes
    参数说明:
    port:如果没有设置使用server的port,它告诉haproxy去哪儿连接
    send-proxy:发送一个proxy 协议string
    ssl:开启一个ciphered 连接
option tcp-check
         tcp-check connect
         tcp-check send GET\ /\ HTTP/1.0\r\n
         tcp-check send Host:\ haproxy.1wt.eu\r\n
         tcp-check send \r\n
         tcp-check expect rstring (2..|3..)
         tcp-check connect port 443 ssl
         tcp-check send GET\ /\ HTTP/1.0\r\n
         tcp-check send Host:\ haproxy.1wt.eu\r\n
         tcp-check send \r\n
         tcp-check expect rstring (2..|3..)
         server www 10.0.0.1 check port 80

         # check both POP and IMAP from a single server:
         option tcp-check
         tcp-check connect port 110
         tcp-check expect string +OK\ POP3\ ready
         tcp-check connect port 143
         tcp-check expect string *\ OK\ IMAP4\ ready
         server mail 10.0.0.1 check
  • tcp-check expect [!]
  • tcp-check send
  • tcp-check send-binary
 # redis check in binary
         option tcp-check
         tcp-check send-binary 50494e470d0a # PING\r\n
         tcp-check expect binary 2b504F4e47 # +PONG
  • tcp-request connection [{if | unless} ]

defaults | frontend | listen | backend
no | yes | yes | no

  • tcp-request content [{if | unless} ]
    Several types of actions are supported :
  • accept : the request is accepted
  • do-resolve: perform a DNS resolution
  • reject : the request is rejected and the connection is closed
  • capture : the specified sample expression is captured
  • set-priority-class | set-priority-offset
  • { track-sc0 | track-sc1 | track-sc2 } [table ]
  • sc-inc-gpc0()
  • sc-inc-gpc1()
  • sc-set-gpt0() { | }
  • set-dst
  • set-dst-port
  • set-var()
  • unset-var()
  • silent-drop
  • send-spoe-group
  • use-service
tcp-request content use-service lua.deny {
     src -f /etc/haproxy/blacklist.lst }
tcp-request content set-var(sess.my_var) src
tcp-request content unset-var(sess.my_var2)
# Accept HTTP requests containing a Host header saying "example.com"
        # and reject everything else.
        acl is_host_com hdr(Host) -i example.com
        tcp-request inspect-delay 30s
        tcp-request content accept if is_host_com
        tcp-request content reject
        # reject SMTP connection if client speaks first
        tcp-request inspect-delay 30s
        acl content_present req_len gt 0
        tcp-request content reject if content_present

        # Forward HTTPS connection only if client speaks
        tcp-request inspect-delay 30s
        acl content_present req_len gt 0
        tcp-request content accept if content_present
        tcp-request content reject
        # Track the last IP(stick-table type string) from X-Forwarded-For
        tcp-request inspect-delay 10s
        tcp-request content track-sc0 hdr(x-forwarded-for,-1)
        # Or track the last IP(stick-table type ip|ipv6) from X-Forwarded-For
        tcp-request content track-sc0 req.hdr_ip(x-forwarded-for,-1)
# track request counts per "base" (concatenation of Host+URL)
        tcp-request inspect-delay 10s
        tcp-request content track-sc0 base table req-rate
         frontend http
            # Use General Purpose Counter 0 in SC0 as a global abuse counter
            # protecting all our sites
            stick-table type ip size 1m expire 5m store gpc0
            tcp-request connection track-sc0 src
            tcp-request connection reject if {
     sc0_get_gpc0 gt 0 }
            ...
            use_backend http_dynamic if {
     path_end .php }

        backend http_dynamic
            # if a source makes too fast requests to this dynamic site (tracked
            # by SC1), block it globally in the frontend.
            stick-table type ip size 1m expire 5m store http_req_rate(10s)
            acl click_too_fast sc1_http_req_rate gt 10
            acl mark_as_abuser sc0_inc_gpc0(http) gt 0
            tcp-request content track-sc1 src
            tcp-request content reject if click_too_fast mark_as_abuser
  • tcp-request inspect-delay
    检查数据的最长等待时间
    defaults | frontend | listen | backend
    no | yes | yes | yes

  • tcp-response content [{if | unless} ]

  • tcp-request session [{if | unless} ]

  • tcp-response inspect-delay

  • timeout connect
    设置成功连接到一台服务器的最长等待时间,默认单位是毫秒。但是也可以使用其他的时间单位后缀
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • timeout http-keep-alive
    设置等待新http请求出现的超时时间

  • timeout http-request
    设置完整的http 请求超时时间
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • timeout queue
    设置队列连接slot free 的超时时间
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • timeout client:
    设置连接客户端发送数据时最长等待时间,默认单位是毫秒。但是也可以使用其他的时间单位后缀
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • timeout client-fin
    为半关闭连接设置客户端超时时间
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • timeout server:
    设置服务器端回应客户端数据发送的最长等待时间,默认单位是毫秒。但是也可以使用其他的时间单位后缀
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • timeout server-fin

  • timeout tarpit
    tarpitted 连接保持时间
    defaults | frontend | listen | backend
    yes | yes | yes | yes

  • timeout tunnel
    设置客户端和服务端隧道超时时间

  • timeout check
    设置多后端服务器的检测超时时间,默认单位是毫秒。但是也可以使用其他的时间单位后缀
    defaults | frontend | listen | backend
    yes | no | yes | yes

  • unique-id-format
    产生一个unique id 为每一个请求
    defaults | frontend | listen | backend
    yes | yes | yes | no

  • unique-id-header
    为请求的header 中添加unique id

  • use-fcgi-app
    定义一个fasetcgi应用作为backend

  • use-server if

  • use-server unless

listen部分

stats refresh:设置 HAProxy 监控统计页面自动刷新的时间。

stats uri:设置 HAProxy 监控统计页面的URL 路径,可随意指定。例如:指定stats uri /haproxy-status,就可以过http://IP:9188/haproxy-status 查看。

stats realm:设置登录 HAProxy 统计页面时密码框上的文本提示信息。

stats auth:设置登录 HAProxy 统计页面的用户名和密码。用户名和密码通过冒号分割。可为监控页面设置多个用户名和密码,每行一个。

stats hide-version:用来隐藏统计页面上 HAProxy 的版本信息。

stats admin if TRUE:通过设置此选项,可以在监控页面上手工启用或禁用后端真实服务器,仅在 haproxy1.4.9 以后版本有效。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/vanexph/article/details/106719157

智能推荐

组合数学中的项链计数_组合数学项链问题-程序员宅基地

文章浏览阅读789次。给c种不同颜色宝石能穿成多少种长度为s的宝石项链(本质不同)Burnside定理的应用:当n为奇数时,有n种翻转,每种翻转都是以一个顶点和该顶点对边的中点对称。有k^(n/2+1)*n种。当n为偶数时,有n种翻转,其中一半是以两个对应顶点,另一半是以两条对边对称。有k^(n/2+1)*n/2+k^(n/2)*n/2种。..._组合数学项链问题

蓝桥杯第八届 省赛 c语言A组_第八届蓝桥杯c语言a组-程序员宅基地

文章浏览阅读575次,点赞2次,收藏7次。第一题标题:迷宫X星球的一处迷宫游乐场建在某个小山坡上。它是由10x10相互连通的小房间组成的。房间的地板上写着一个很大的字母。我们假设玩家是面朝上坡的方向站立,则:L表示走到左边的房间,R表示走到右边的房间,U表示走到上坡方向的房间,D表示走到下坡方向的房间。X星球的居民有点懒,不愿意费力思考。他们更喜欢玩运气类的游戏。这个游戏也是如此!开始的时候,直升机把10..._第八届蓝桥杯c语言a组

常用的抓包工具_tcap包-程序员宅基地

文章浏览阅读1.3k次。建议先了解不同协议和网络层的数据包结构。不然,你将无法了解如何做包分组工作,以及这些工具如何使用。出于学习目的,在使用这些工具列表之前,你必须了解不同协议的数据包,包中的不同字段,这些包字段的含义或目的,以及这些包在网络通信中的使用方式。一旦你知道这些事情,你将能够改变这些值,以便你在网络中看到你所需的效果。1. HpingHping是最受欢迎和免费的抓包工具之一。它允许你修改和发送自定义..._tcap包

解决linux指令“没有那个文件或目录”-程序员宅基地

文章浏览阅读1.5w次。本文介绍了一种面对linux指令“没有那个文件或目录”时的自查和解决方法_没有那个文件或目录

eslint配置大全_eslint 参数-程序员宅基地

文章浏览阅读462次。{"rules" : { // 定义对象的set存取器属性时,强制定义get "accessor-pairs": 2, // 指定数组的元素之间要以空格隔开(,后面), never参数:[ 之前和 ] 之后不能带空格,always参数:[ 之前和 ] 之后必须带空格 "array-bracket-spacing": [2, "never"], // 在块级作用域外访问块内定义的变量是否报错提示 "block-scoped-var": 0, // if while functi._eslint 参数

Mac安装anaconda后两个python版本共存,在终端pip无法为各自python版本倒入模块的解决办法_mac anaconda 配置两个 python-程序员宅基地

文章浏览阅读3.7k次。问题描述:使用anaconda来管理包,十分方便,也可以到达使得python2和python3共存的目的,但当导入anaconda以外的包时,可能会产生一些问题,在近期学习中,由于会用到scikit-learn模块,此模块为python2.7版本的模块,在使用终端通过pip安装时,ppip默认为为python3安装包。对此,可以尝试用pycharm导入此模块:打开pycharm,选择file菜_mac anaconda 配置两个 python

随便推点

可变模板参数_template<typename t, typename ...args>-程序员宅基地

文章浏览阅读1.1k次。背景在 C++11 之前,无论是类模板还是函数模板,都只能按其指定的样子,接受一组固定数量的模板参数;而C++11 加入了新的表示方法,允许任意个数、任意类别的模板参数,同时也不需要在定义时将参数的个数固定。可变参数模板和普通模板的语义是一样的,只是写法上稍有区别,声明可变参数模板时需要在typename或class后面带上省略号“...”template<typename....._template

css实现超出长度文字自动隐藏或用省略号表示_css a标签超出宽度自动隐藏-程序员宅基地

文章浏览阅读269次。HTML<a href="" target="_blank"> 业务需求分析书-系统营业日扎帐报表、收费汇总表及收费明细表 </a>cssa{overflow: hidden;text-overflow: ellipsis;white-space: nowrap;width: 100%;display: inline-block;}overflow:..._css a标签超出宽度自动隐藏

前向纠错(FEC)之纠删码原理(EC)_向前纠错的编码过程-程序员宅基地

文章浏览阅读2k次。References:纠删码解析_向前纠错的编码过程

(六)使用Transformers进行情感分析_基于transformer的情感分析-程序员宅基地

文章浏览阅读6.8k次,点赞12次,收藏53次。文章目录准备数据搭建模型训练模型推断完整代码在本笔记本中我们将使用在本论文中介绍的Transformer模型。具体来说,我们将使用来自这篇论文中的BERT(Bidirectional Encoder Representations from Transformers)模型。Transformer模型比之前笔记中介绍的任何模型都要大得多。因此,我们将使用transformers library获得预先训练的transformers,并使用它们作为我们的嵌入层。我们将冻结(而不是训练)transformer_基于transformer的情感分析

Computational Optimal Transport笔记——Introduction_最优运输理论 偏微分方程-程序员宅基地

文章浏览阅读1.2k次。Computational Optimal Transport笔记——Introduction最优运输可以看做是更一般的最短路问题最短路原则:当在给定的点处有一个商品、人或者一些信息,这些东西需要被送到目标点。我们希望使用最少的工作量完成任务。(我们可以沿着直线或者测地线移动)最忧运输理论将这个问题一般化,一个人不是一次移动一个物品而是同时移动多个物品。经证明,有计划地运输一堆个体到指定的位..._最优运输理论 偏微分方程

springboot2.0之配置spring security记住我(rememberMe功能)不起作用的原因_spring security记住我功能有令牌不实现-程序员宅基地

文章浏览阅读3.4k次。刚入门spring security,感觉东西有点多,不好好研究出了问题都不知道为什么。rememberMe功能配置核心代码(有问题的): protected void configure(HttpSecurity http) throws Exception { List<SysPermission> allPermission = permissionFe..._spring security记住我功能有令牌不实现

推荐文章

热门文章

相关标签