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 命令检查,会报错。
前后端连接模型
- KAL: keep alive( option http-keep-alive ),这是默认的模式,所有的请求和响应都会被 HAProxy 处理,且允许在没有请求和响应时保持空闲的连接
- TUN:tunnel( option http-tunnel ):这是 1.0 ~ 1.5-dev21 的默认模式,类似于隧道,HAProxy 仅处理第一个请求和响应,剩余的报文将直接转发而不进行处理。尽量不要使用这个模式,因为它在日志记录和 HTTP 处理上有很多问题。
- PCL:passive close( option httpclose ),这和 tunnel 模式类似,区别是 HAProxy 会在发往客户端的响应报文和发往服务器的请求报文中加入 “Connection: close” 首部,使得客户端和后端主机在完成与 HAProxy 的一次通信后主动的关闭连接。
- SCL:server close( option http-server-close ),HAProxy 在接收到后端服务器的响应后就立即断开与后端服务器的连接,而与客户端的连接则使用保持连接。
- 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 | yes1、 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_proxybind :80,:443bind 10.0.0.1:10080,10.0.0.1:10443bind /var/run/ssl-frontend.sock user root mode 600 accept-proxylisten http_https_proxybind :80bind :443 ssl crt /etc/haproxy/site.pemlisten http_https_proxy_explicitbind ipv6@:80bind ipv4@public_ssl:443 ssl crt /etc/haproxy/site.pembind unix@ssl-frontend.sock user root mode 600 accept-proxylisten external_bind_app1bind "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_ip1bind 10.0.0.1:80bind-process oddlisten app_ip2bind 10.0.0.2:80bind-process evenlisten managementbind 10.0.0.3:80bind-process 1 2 3 4listen managementbind 10.0.0.4:80bind-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 | yesdisabled
禁用代理,前端或后端。
可用区域:
defaults | frontend | listen | backend
yes | yes | yes | yes
“disable”关键字用于禁用实例,比如临时停用某个服务,实例会被创建,配置也会被检查,但是将会被标记为stopped。它将不会接收任何的流量和健康检查会日志。将所有都禁用则可在default区域中配置disableenabled
启用代理,前端或后端。
可用区域:
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*:过滤器参数列表。
listenbind *:80filter trace name BEFORE-HTTP-COMPfilter compressionfilter trace name AFTER-HTTP-COMPcompression algo gzipcompression offloadserver srv1 192.168.0.1:80
- fullconn
指定的后端负载服务器的minconn最小连接,和maxconn最大连接。
可用区域:
defaults | frontend | listen | backend
yes | no | yes | yes
默认,haproxy将改值设置为maxconns的10%
backend dynamicfullconn 10000server srv1 dyn1:80 minconn 100 maxconn 1000server srv2 dyn2:80 minconn 100 maxconn 1000
grace
在软停止后,维持一段时间的代理操作
可用区域:
defaults | frontend | listen | backend
yes | yes | yes | yes
该设置可用于确保后端服务以一个确定的顺序消失,hash-balance-factor
指定一致性散列hash负载,将设置为0(默认值)将禁用此功能。
可用区域:
defaults | frontend | listen | backend
yes | no | no | yeshash-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 denyacl key req.hdr(X-Add-Acl-Key) -m found
acl add path /addacl
acl del path /delaclacl myhost hdr(Host) -f myhost.lsthttp-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 delacl value req.hdr(X-Value) -m found
acl setmap path /setmap
acl delmap path /delmapuse_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) G1http-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 mydnsnameserver local 127.0.0.53:53nameserver google 8.8.8.8:53timeout retry 1shold valid 10shold nx 3shold other 3shold obsolete 0saccepted_payload_size 8192frontend febind 10.42.0.1:80http-request do-resolve(txn.myip,mydns,ipv4) hdr(Host),lowerhttp-request capture var(txn.myip) len 40# return 503 when the variable is not set,# which mean DNS resolution erroruse_backend b_503 unless { var(txn.myip) -m found }default_backend bebackend b_503# dummy backend used to return 503.# one can use the errorfile directive to send a nice# 503 error page to end usersbackend 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=1http-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 %Thttp-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:变量名称以区域开头,允许的区域包括:
- “proc”:进程内都可访问的变量
- “sess”:整个session都可访问的变量
- “txn”:整个transaction都可访问的变量
- “req”:整个请求内都可访问的变量
- “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 foundacl myhost hdr(Host) -f myhost.lsthttp-response add-acl(myhost.lst) %[res.hdr(X-Acl-Key)] if key_aclhttp-response del-acl(myhost.lst) %[res.hdr(X-Acl-Key)] if key_aclacl value res.hdr(X-Value) -m founduse_backend bk_appli if { hdr(Host),map_str(map.lst) -m found }http-response set-map(map.lst) %[src] %[res.hdr(X-Value)] if valuehttp-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、alwayshttp-send-name-header [
]
将服务器名称添加到请求。使用由给出的标题字符串
可用区域:
defaults | frontend | listen | backend
yes | no | yes | yes
id :
proxy 唯一标识
可用区域:
defaults | frontend | listen | backend
no | yes | yes | yesignore-persist { if | unless }
声明某种条件下忽略持久性
可用区域:
defaults | frontend | listen | backend
no | no | yes | yes
默认,当cookie persistent 开启,每个包含cookie的请求都会无条件的持久。
acl url_static path_beg /static /images /img /cssacl url_static path_end .gif .png .jpg .css .jsignore-persist if url_static
- load-server-state-from-file { global | local | none }
允许无缝重新加载HAProxy
可用区域:
defaults | frontend | listen | backend
yes | no | yes | yes
globalstats socket /tmp/socketserver-state-file /tmp/server_statedefaultsload-server-state-from-file globalbackend bkserver s1 127.0.0.1:22 check weight 11server s2 127.0.0.1:22 check weight 12socat /tmp/socket - <<< "show servers state" > /tmp/server_state
- log global
- log
[len ] [ []]
- no log
可用区域:
defaults | frontend | listen | backend
yes | yes | yes | yes
log globallog stdout format short daemon # send log to systemdlog stdout format raw daemon # send everything to stdoutlog stderr format raw daemon notice # send important events to stderrlog 127.0.0.1:514 local0 notice # only send important eventslog 127.0.0.1:514 local0 notice notice # same but limit output levellog "${LOCAL_SYSLOG}:514" local0 notice # send to local server
log-format
指定用于流量日志的日志格式字符串
可用区域:
defaults | frontend | listen | backend
yes | yes | yes | nolog-format-sd
指定RFC5424结构化数据日志格式字符串
可用区域:
defaults | frontend | listen | backend
yes | yes | yes | nolog-tag
指定要用于所有传出日志的日志标记
可用区域:
defaults | frontend | listen | backend
yes | yes | yes | yesmax-keep-alive-queue
设置维护保持连接的最大服务器队列大小
可用区域:
defaults | frontend | listen | backend
yes | no | yes | yes
保持会话将尝试复用服务器的连接,但有时候会适得其反,例如一个服务有很多的连接而其他的服务确很闲。
默认值为-1,表示没有限制大小,max-session-srv-conns
连接客户端的最大闲置session连接数。默认5maxconn
设置最大的连接数,默认是2000
defaults | frontend | listen | backend
yes | yes | yes | nomode { tcp|http|health }
defaults | frontend | listen | backend
yes | yes | yes | yesmonitor fail { if | unless }
添加条件以报告监视器HTTP请求的失败。
defaults | frontend | listen | backend
no | yes | yes | no
frontend wwwmode httpacl site_dead nbsrv(dynamic) lt 2acl site_dead nbsrv(static) lt 2monitor-uri /site_alivemonitor fail if site_dead
monitor-net
声明一个限于监视请求的源网络
defaults | frontend | listen | backend
yes | yes | yes | nomonitor-uri
拦截外部组件的监视请求使用的URI
defaults | frontend | listen | backend
yes | yes | yes | no
# addresses .252 and .253 are just probing us.
frontend wwwmonitor-net 192.168.0.252/31# Use /haproxy_test to report haproxy's status
frontend wwwmode httpmonitor-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 | nooption 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 | yesoption clitcpka
no option clitcpka
启用或禁用在客户端发送TCP Keepalive报文
defaults | frontend | listen | backend
yes | yes | yes | nooption contstats
启用连续流量统计更新
defaults | frontend | listen | backend
yes | yes | yes | nooption dontlog-normal
no option dontlog-normal
启用或禁用日志记录正常,成功的连接
defaults | frontend | listen | backend
yes | yes | yes | nooption dontlognul
no option dontlognull
启用或禁用日志记录的空连接
defaults | frontend | listen | backend
yes | yes | yes | nooption 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 wwwmode httpoption forwardfor except 127.0.0.1
backend wwwmode httpoption 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 | yesoption 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 | nooption 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 | yesoption 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 | nooption httpchk
option httpchk
option httpchk
option httpchk
此选项表示启用HTTP的服务状态检测功能。HAProxy作为一款专业的负载均衡器,它支持对backend部分指定的后端服务节点进行健康检查,以保证在后端backend中某个节点不能服务时,把从frontend端进来的客户端请求分配至backend中其他健康节点上,从而保证整体服务的可用性。
defaults | frontend | listen | backend
yes | no | yes | yesoption 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 | yesoption ldap-check
使用LDAPv3运行状况检查进行服务器测试
defaults | frontend | listen | backend
yes | no | yes | yesoption external-check
使用外部进程进行服务器健康检查
defaults | frontend | listen | backend
yes | no | yes | yesoption log-health-checks
no option log-health-checks
启用或禁用日志记录健康检查状态更新
defaults | frontend | listen | backend
yes | no | yes | yesoption log-separate-errors
no option log-separate-errors
为非完全成功的连接更改日志级别
defaults | frontend | listen | backend
yes | yes | yes | nooption logasap
no option logasap
启用或禁用HTTP请求的早期记录
defaults | frontend | listen | backend
yes | yes | yes | nooption mysql-check [ user [ post-41 ] ]
使用MySQL健康检查进行服务器测试
defaults | frontend | listen | backend
yes | no | yes | yesoption 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 | yesoption persist
no option persist
在下载服务器上启用或禁用强制持久性
defaults | frontend | listen | backend
yes | no | yes | yesoption pgsql-check [ user ]
使用PostgreSQL运行状况检查进行服务器测试option prefer-last-server
no option prefer-last-server
允许多个负载均衡请求保留在同一台服务器上
defaults | frontend | listen | backend
yes | no | yes | yesoption 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 | yesoption socket-stats
为每个套接字收集和提供单独的统计信息。
defaults | frontend | listen | backend
yes | yes | yes | nooption splice-auto
在两个方向上启用或禁用套接字上的自动内核加速
defaults | frontend | listen | backend
yes | yes | yes | yesoption splice-request
套接字上的自动内核加速请求
defaults | frontend | listen | backend
yes | yes | yes | yesoption splice-response
自动内核加速以进行响应
defaults | frontend | listen | backend
yes | yes | yes | yesoption spop-check
使用SPOP健康检查进行服务器测试
defaults | frontend | listen | backend
no | no | no | yesoption srvtcpka
在服务器端发送TCP Keepalive报文
defaults | frontend | listen | backend
yes | no | yes | yesoption ssl-hello-chk
使用SSLv3客户端hello运行状况检查进行服务器测试
defaults | frontend | listen | backend
yes | no | yes | yesoption 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\ protocoloption tcp-checktcp-check comment PING\ phasetcp-check send PING\r\ntcp-check expect string +PONGtcp-check comment role\ checktcp-check send info\ replication\r\ntcp-check expect string role:mastertcp-check comment QUIT\ phasetcp-check send QUIT\r\ntcp-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-farmbind :3389# wait up to 5s for an RDP cookie in the requesttcp-request inspect-delay 5stcp-request content accept if RDP_COOKIE# apply RDP cookie persistencepersist rdp-cookie# if server is unknown, let's balance on the same cookie.# alternatively, "balance leastconn" may be useful too.balance rdp-cookieserver srv1 1.1.1.1:3389server srv2 1.1.1.2:3389
- rate-limit sessions
限制frontend每秒接受的session数量
defaults | frontend | listen | backend
yes | yes | yes | no
当达到该值,frontend将停止接受新的连接。
listen smtpmode tcpbind :25rate-limit sessions 10server 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 80acl secure dst_port 8080acl login_page url_beg /loginacl logout url_beg /logoutacl uid_given url_reg /login?userid=[^&]+acl cookie_set hdr_sub(cookie) SEEN=1redirect prefix https://mysite.com set-cookie SEEN=1 if !cookie_setredirect prefix https://mysite.com if login_page !secureredirect prefix http://mysite.com drop-query if login_page !uid_givenredirect location http://mysite.com/ if !login_page secureredirect location / clear-cookie USERID= if logoutacl missing_slash path_reg ^/article/[^/]*$
redirect code 301 prefix / drop-query append-slash if missing_slashredirect scheme https if !{ ssl_fc }
retries
设置连接后端服务器的失败重试次数,
defaults | frontend | listen | backend
yes | no | yes | yesretry-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 1000server second 10.1.1.2:1080 cookie second check inter 1000server transp ipv4@server backup "${SRV_BACKUP}:1080" backupserver www1_dc1 "${LAN_DC1}.101:80"server www1_dc2 "${LAN_DC2}.101:80"
- server-state-file-name []
设置服务器状态文件,仅在设置了load-server-state-from-file local 生效
如果没有给出文件则是后端名称,
globalserver-state-file-base /etc/haproxy/statesbackend bkload-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# orserver-template srv 3 google.com:80 check# would be equivalent to:server srv1 google.com:80 checkserver srv2 google.com:80 checkserver 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 addresssource 192.168.1.200backend transparent_ssl1# Connect to the SSL farm from the client's source addresssource 192.168.1.200 usesrc clientipbackend 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 clientbackend transparent_ssl3# Connect to the SSL farm from the client's source address. It# is more conntrack-friendly.source 192.168.1.200 usesrc clientipbackend transparent_smtp# Connect to the SMTP farm from the client's source address/port# with Tproxy version 4.source 0.0.0.0 usesrc clientipbackend 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_localhoststats enablestats admin if LOCALHOSTbackend stats_authstats enablestats auth admin:AdMiN123stats admin if TRUE
userlist stats-authgroup admin users adminuser admin insecure-password AdMiN123group readonly users haproxyuser haproxy insecure-password haproxybackend stats_authstats enableacl AUTH http_auth(stats-auth)acl AUTH_ADMIN http_auth_group(stats-auth) adminstats http-request auth unless AUTHstats 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 popmode tcpbalance roundrobinstick store-request srcstick-table type ip size 200k expire 30mserver s1 192.168.1.1:110server s2 192.168.1.1:110backend smtpmode tcpbalance roundrobinstick match src table popserver s1 192.168.1.1:25server 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 !localhoststick 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 popmode tcpbalance roundrobinstick store-request srcstick-table type ip size 200k expire 30mserver s1 192.168.1.1:110server s2 192.168.1.1:110backend smtpmode tcpbalance roundrobinstick match src table popserver s1 192.168.1.1:25server 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 httpsmode tcpbalance roundrobin# maximum SSL session ID length is 32 bytes.stick-table type binary len 32 size 30k expire 30macl clienthello req_ssl_hello_type 1acl serverhello rep_ssl_hello_type 2# use tcp content accepts to detects ssl client and server hello.tcp-request inspect-delay 5stcp-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 serverhelloserver s1 192.168.1.1:443server 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-checktcp-check connecttcp-check send GET\ /\ HTTP/1.0\r\ntcp-check send Host:\ haproxy.1wt.eu\r\ntcp-check send \r\ntcp-check expect rstring (2..|3..)tcp-check connect port 443 ssltcp-check send GET\ /\ HTTP/1.0\r\ntcp-check send Host:\ haproxy.1wt.eu\r\ntcp-check send \r\ntcp-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-checktcp-check connect port 110tcp-check expect string +OK\ POP3\ readytcp-check connect port 143tcp-check expect string *\ OK\ IMAP4\ readyserver mail 10.0.0.1 check
- tcp-check expect [!]
- tcp-check send
- tcp-check send-binary
# redis check in binaryoption tcp-checktcp-check send-binary 50494e470d0a # PING\r\ntcp-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.comtcp-request inspect-delay 30stcp-request content accept if is_host_comtcp-request content reject# reject SMTP connection if client speaks firsttcp-request inspect-delay 30sacl content_present req_len gt 0tcp-request content reject if content_present# Forward HTTPS connection only if client speakstcp-request inspect-delay 30sacl content_present req_len gt 0tcp-request content accept if content_presenttcp-request content reject# Track the last IP(stick-table type string) from X-Forwarded-Fortcp-request inspect-delay 10stcp-request content track-sc0 hdr(x-forwarded-for,-1)# Or track the last IP(stick-table type ip|ipv6) from X-Forwarded-Fortcp-request content track-sc0 req.hdr_ip(x-forwarded-for,-1)
# track request counts per "base" (concatenation of Host+URL)tcp-request inspect-delay 10stcp-request content track-sc0 base table req-ratefrontend http# Use General Purpose Counter 0 in SC0 as a global abuse counter# protecting all our sitesstick-table type ip size 1m expire 5m store gpc0tcp-request connection track-sc0 srctcp-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 10acl mark_as_abuser sc0_inc_gpc0(http) gt 0tcp-request content track-sc1 srctcp-request content reject if click_too_fast mark_as_abuser
tcp-request inspect-delay
检查数据的最长等待时间
defaults | frontend | listen | backend
no | yes | yes | yestcp-response content [{if | unless} ]
tcp-request session [{if | unless} ]
tcp-response inspect-delay
timeout connect
设置成功连接到一台服务器的最长等待时间,默认单位是毫秒。但是也可以使用其他的时间单位后缀
defaults | frontend | listen | backend
yes | no | yes | yestimeout http-keep-alive
设置等待新http请求出现的超时时间timeout http-request
设置完整的http 请求超时时间
defaults | frontend | listen | backend
yes | yes | yes | yestimeout queue
设置队列连接slot free 的超时时间
defaults | frontend | listen | backend
yes | no | yes | yestimeout client:
设置连接客户端发送数据时最长等待时间,默认单位是毫秒。但是也可以使用其他的时间单位后缀
defaults | frontend | listen | backend
yes | yes | yes | notimeout client-fin
为半关闭连接设置客户端超时时间
defaults | frontend | listen | backend
yes | yes | yes | notimeout server:
设置服务器端回应客户端数据发送的最长等待时间,默认单位是毫秒。但是也可以使用其他的时间单位后缀
defaults | frontend | listen | backend
yes | no | yes | yestimeout server-fin
timeout tarpit
tarpitted 连接保持时间
defaults | frontend | listen | backend
yes | yes | yes | yestimeout tunnel
设置客户端和服务端隧道超时时间timeout check
设置多后端服务器的检测超时时间,默认单位是毫秒。但是也可以使用其他的时间单位后缀
defaults | frontend | listen | backend
yes | no | yes | yesunique-id-format
产生一个unique id 为每一个请求
defaults | frontend | listen | backend
yes | yes | yes | nounique-id-header
为请求的header 中添加unique iduse-fcgi-app
定义一个fasetcgi应用作为backenduse-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 以后版本有效。
haproxy中的Proxies段的配置相关推荐
- Linux环境中Visual Studio Code的配置使用----编译运行C/C++(良心教程)
之前的博文分享了下载安装[VS code]的详细教程, 有需要速戳–>Linux环境中Visual Studio Code 安装配置及其卸载(详细教程) 本篇博文分享本人初次使用[VS code ...
- vue-cli中的webpack的config配置详细说明
vue-cli中的webpack的config配置详细说明 打开我们的vue项目文件夹我们可以清楚的看到文件夹下的三个文件, "dev.env.js","index.js ...
- sql azure 语法_Azure Data Studio中SQL代码段
sql azure 语法 This article will fully cover the code snippet SQL developer productivity feature in Az ...
- Windows系统中vscode+MSVC的C++配置
Windows系统中vscode+MSVC的C++配置 在Windows上编译C++程序不能直接使用gcc和g++命令,一般来说如果非要使用的话可以用wsl或者MinGW.过去的很长一段时间我也确实是 ...
- Sql Server数据库中的用户及权限配置,保障数据库安全
最近有个朋友的网站被攻击,漏洞是比较过时的SQL注入,在帮修复的过程中,发现数据库用的是超级管理员用户sa,提高数据安全性和服务器安全性的措施之一便是降低web应用数据库用户的权限,回顾了一下Sql ...
- 【DB笔试面试682】在Oracle中,Undo段中区3种状态分别是什么(Undo表空间系列)?...
♣ 题目 部分 在Oracle中,Undo段中区3种状态分别是什么? ♣ 答案部分 Undo信息存储在Undo段中,Undo段又存储在Undo表空间中.Undo表空间仅用于Undo段(在Undo表空间 ...
- 如何得到数据库中所有表字段及字段中文描述
如何得到数据库中所有表字段及字段中文描述 以下资料,通过csdn的一位师兄从SQL版主那得到: sql中 SELECT (case when a.colorder=1 then d.name els ...
- 用Handler的post()方法来传递线程中的代码段到主线程中执行
自定义的线程中是不能更新UI的,但是如果遇到更新UI的事情,我们可以用handler的post()方法来将更新UI的方法体,直接传送到主线程中,这样就能直接更新UI了.Handler的post()方法 ...
- Linux环境中Visual Studio Code 安装配置及其卸载(详细教程)
两篇相关博文: 在VMware15中创建虚拟机安装ubuntu系统(超详细教程) Linux环境编译运行C/C++语言程序----配置gcc.g++(详细教程) 此篇分享我在linux环境下安装VS ...
最新文章
- f-free 查看系统中空闲和使用的内存
- 解决依赖资源同名的问题
- 【6】青龙面板系列教程之xdd-plus与nolanjdc的对接
- Android LinearLayout
- Lock的lockInterruptibly()
- 错误:使用printf()打印Hello world时未声明'Hello'/ Text
- 算法与数据结构(二)-数组、链表(Array、Linked List)
- 最小可分辨温差四杆靶空间频率选择
- Go学习——runtime.Caller()函数
- 全球顶尖的程序化交易模型
- java geohash_GitHub - GongDexing/Geohash: GeoHash是目前比较主流实现位置服务的技术,用最简洁的Java实现GeoHash算法...
- JAVA面向对象三大特征之一多态
- 灵活组合复合图元模板,电路图状态图业务定义可配置,浮云E绘图软件源码开发
- 新服务器如何装系统,新服务器怎么装系统
- 微信棋牌游戏域名防封最新解决方案
- Raft源码分析(一) - State
- 群晖6.XVideo Station不支持视频格式的解决方案
- 财报前瞻 | 开启餐饮业序幕,麦当劳Q4财报能否带来强劲指引?
- 靠近在水一方的伊人_16已是往昔的昨日
- 电脑一开机就蓝屏怎么解决?哪种蓝屏修复方法更方便?
热门文章
- 许永鑫 南京理工大学计算机学院,南京理工大学计算机科学与技术学院研究生导师简介-付安民...
- WinXP共享密码设置
- UVM TLM的实现方式
- python设置windows桌面壁纸
- 3D打印:亮光黑漆面效果
- 首富软件测试工资,测试你成为富豪 测试你天生是什么命
- 小傻蛋的妹妹跟随小甲鱼学习Python的第二十三、二十四节023、024
- iOS开发——播放系统音效、自定义音效
- 2022春季学期哈工大计算机系统大作业
- 配件查询网站用java,基于jsp的配件管理系统-JavaEE实现配件管理系统 - java项目源码...