layout: post
title: HAproxy简介和配置
date: 2018-03-13
tags: [“Haproxy”,”软件服务”]


一、HAproxy简介

HAProxy是一个免费的负载均衡软件,可以运行于大部分主流的Linux操作系统上。HAproxy可以实现4层和7层的两种负载能力,其稳定性和负载能力得到了广大的好评。

HAProxy的核心功能:

: - 通过静态的cookies来路由http请求
: - 在确保服务器持久性的同时在多个服务器之间传播负载
: -可以使用http的cookies
: - 主server宕机可以使用backupserver作为填充
: - 接受连接到专用于服务监控的特殊端口
: - 停止接受连接而不破坏现有连接
: - 可以直接添加、删除、修改请求和响应报文的报文头部
: - 通过正则表达式阻塞请求
: - 可以通过页面查看均衡器的状态和修改后端节点

二、HAproxy的global配置说明

1、配置段:

global:全局配置段

进程及安全配置相关的参数
性能调整相关参数
Debug参数
用户列表
peers

proxies:代理配置段

defaults:为frontend, listen, backend提供默认配置;
fronted:前端,相当于nginx, server {}
backend:后端,相当于nginx, upstream {}
listen:同时拥前端和后端,It is generally useful for TCP-only traffic.

2、global配置参数

  1. global
  2. chroot /var/lib/haproxy
  3. pidfile /var/run/haproxy.pid
  4. maxconn 4000
  5. user haproxy
  6. group haproxy
  7. daemon
  8. stats socket /var/lib/haproxy/stats
  9. nbproc <number>:要启动的haproxy的进程数量;官方建议保持单进程,当开启多进程时,部分问题的无法做错误追踪,而单进程模式也可以处理几万的并发。
  10. ulimit-n <number>:每个haproxy进程可打开的最大文件数;haproxy可以自行调整,所以这里无需设置
  11. log <address> [len <length>] <facility> [max level [min level]]

性能优化

  1. maxconn <number>:设定每个haproxy进程所能接受的最大并发连接数;Sets the maximum per-process number of concurrent connections to <number>. 总体的并发连接数:nbproc * maxconn
  2. maxconnrate <number>:每个进程每秒种所能创建的最大连接数量;
  3. maxsessrate <number>:将每秒会话数的最大值设置为<number>。当达到此限制时,代理将停止接受连接。 无论每个前端容量如何,它都可以用来限制全局容量。 值得注意的是,这只能用作服务保护措施,因为在达到限制时,前端之间不一定会有公平的份额,所以最好将每个前端限制在接近其前端的某个值 预期的份额。 另外,降低tune.maxaccept可以提高
  4. maxsslconn <number>:设定每个haproxy进程所能接受的ssl的最大并发连接数;
  5. spread-checks <0..50, in percent> 在两次连续检查之间的间隔延迟上添加一些随机性,以避免以过于规律的间隔发送健康检查。 如果未设置,则默认值为0。(当后端节点过多时,用于避免并发,当后端节点过多时,该项是必须启用的)

这里详细说一下log的配置(摘自官网)

  1. 参数: IPv4 address'IPv6 address'A filesystem path to a UNIX domain socket
  2. 参数: 大于此值的日志行在发送之前将被截断。 原因是系统日志服务器在日志行长度上的行为不同。 所有服务器都支持1024的默认值,但有些服务器只需放置更大的线路,而其他服务器则会记录它们。 如果服务器支持较长的行,则可以在此处设置此值以避免截断长行。 同样,如果一台服务器放弃长行,最好在发送它们之前截断它们。 接受的值是8065535(含)。 对于所有标准用法,默认值1024通常都很好。 长捕获或JSON格式日志的一些特定情况可能需要较大的值。
  3. 参数:must be one of the 24 standard syslog facilities
  4. for example:
  5. kern user mail daemon auth syslog lpr news
  6. uucp cron auth2 ftp ntp audit alert cron2
  7. local0 local1 local2 local3 local4 local5 local6 local7
  1. log-format
  2. Specifies the log format string to use for traffic logs
  3. contextdefaultsfrontendlisten

log-format参数见https://cbonte.github.io/haproxy-dconv/1.5/configuration.html#8.2

三、defaults配置段

配置后端的默认参数,可参考官方文档https://cbonte.github.io/haproxy-dconv/1.5/configuration.html#4.1zenggai增改

1、option forwardfor [ except ] [ header ] [ if-none ]

Enable insertion of the X-Forwarded-For header to requests sent to servers

except : 排除的网段或地址
header : 使用自定义的首部名称代替X-FORWARDED-FOR
if-none: 如果为空添加

2、timeout client Set the maximum inactivity time on the client side. 默认单位是毫秒;

3、timeout server Set the maximum inactivity time on the server side.

4、timeout http-keep-alive 持久连接的持久时长;

5、timeout http-request Set the maximum allowed time to wait for a complete HTTP request

6、timeout connect Set the maximum time to wait for a connection attempt to a server to succeed.

7、timeout client-fin Set the inactivity timeout on the client side for half-closed connections.

8、timeout server-fin Set the inactivity timeout on the server side for half-closed connections.

9、compression

对传输的压缩配置

syntax:

  1. compression algo ... #启用http协议的压缩机制,指明压缩算法gzip, deflate;
  2. compression type ... #指明压缩的MIME类型;常适用于压缩的类型为文本类型;
  3. compression offload #

for example:

  1. compression algo gzip
  2. compression type text/html text/plain

“compression offload” 的使用场景:

“卸offload”设置使haproxy删除Accept-Encoding标头toprevent后端服务器压缩响应。 强烈建议不要这样做,因为这意味着所有压缩工作将在haproxy所在的单个点上完成。 但是,在一些部署方案中,haproxy可能安装在有错误的HTTP压缩实现的错误网关之前,无法关闭。在这种情况下,haproxy可用于防止该网关发出无效的有效负载。 在这种情况下,简单地删除配置中的标题不起作用,因为它在解析标题之前应用,这样可以防止haproxy压缩。 这种情况下应该使用”卸载”设置。 注意:现在,在”默认值”部分设置时,”offload”设置将被忽略。

Compression is disabled when:

该请求不会在”Accept-Encoding”头中通告支持的压缩算法 响应消息不是HTTP / 1.1

  • HTTP状态码不是200
    响应标题”传输编码”包含”分块”(临时解决方法) 响应既不包含”Content-Length”标题,也不包含最后一个值为”chunked”的”Transfer-Encoding”
    响应包含一个”Content-Type”头,其首个值以”multipart” 响应包含”Cache-control”头中的”no-transform”值
    用户代理匹配”Mozilla / 4”,除非它是XP SP2或MSIE 7及更高版本的MSIE 6 响应包含一个”Content-Encoding”头,表明响应已经被压缩(请参阅Compression offload)

四、frontend配置段

1、bind:绑定地址和端口

context:frontend or listen

syntax:
bind [address]:port_range [, …] [param]
bind / [, …] [param
]

  1. for example
  1. frontend test
  2. bind :80,:443
  3. bind 10.0.0.1:10080,10.0.0.1:10443
  4. bind /var/run/ssl-frontend.sock user root mode 600 accept-proxy

2、 default_backend 设定默认的backend,用于frontend中;

3、maxconn

定义前端并发连接数,默认为2000

context : defaultsfrontendlisten

4、mode { tcp’http’health }

定义haproxy的工作模式;

context : defaultsfrontendlistenbackend

  1. tcp :基于layer4实现代理;可代理mysql, pgsql, ssh, ssl等协议;
  2. http :仅当代理的协议为http时使用;
  3. health :工作为健康状态检查的响应模式,当连接请求到达时回应"OK"后即断开连接;

5、reqadd [{if ‘ unless} ]

context:listenbackendfrontend

Add a header at the end of the HTTP request

6、reqdel [{if ‘ unless} ]
reqidel [{if ‘ unless} ] (ignore case)

context:listenbackendfrontend

Delete all headers matching a regular expression in an HTTP response

7、rspadd [{if ‘ unless} ]

context:listenbackendfrontend

Add a header at the end of the HTTP response

for example:

  1. rspadd X-Via:\ HAPorxy

8、rspdel [{if ‘ unless} ]
rspidel [{if ‘ unless} ] (ignore case)

Delete all headers matching a regular expression in an HTTP request

for example:

  1. rspidel Server.*

9、use_backend [{if ‘ unless} ]

Switch to a specific backend if/unless an ACL-based condition is matched.当符合指定的条件时使用特定的backend;

见第八节,ACL设定

五、backend配置段

1、balance

context:defaults、backend、listen

syntax:

balance [ ]
balance url_param [check_post]

Arguments :

  1. 算法
  1. roundrobinEach server is used in turns, according to their weights. server options weight #动态算法:支持权重的运行时调整,支持慢启动;每个后端中最多支持4095个server;
  2. static-rr 静态算法:不支持权重的运行时调整及慢启动;后端主机数量无上限;
  3.  
  4. leastconn 推荐使用在具有较长会话的场景中,例如MySQLLDAP等;
  5.  
  6. first 根据服务器在列表中的位置,自上而下进行调度;前面服务器的连接数达到上限,新请求才会分配给下一台服务;
  7.  
  8. source
  9. 源地址hash;(不建议使用,因为client访问internet一般都是通过NAT的方式访问的,也就是说,会有大量的用户使用同一个IP地址,这种方式会造成调度的不均匀分配,可以使用cookie的方式来代替。)
  10. 除权取余法:
  11. 一致性哈希:
  12.  
  13. uri URI的左半部分做hash计算,并由服务器总权重相除以后派发至某挑出的服务器;this can be changed using "hash-type"
  14.  
  15. ://:@:/;?#
  16. 左半部分:/;
  17. 整个uri:/;?#
  18. url_param:对用户请求的uri部分中的参数的值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个Backend Serverthis can be changed using "hash-type"
  19. hdr():对于每个http请求,此处由指定的http首部将会被取出做hash计算; 并由服务器总权重相除以后派发至某挑出的服务器;没有有效值的会被轮询调度; this can be changed using "hash-type"
  20. hdr(Cookie)
  21. hash-type:哈希算法
  22. hash-type
  23. map-based:除权取余法,哈希数据结构是静态的数组;
  24. consistent:一致性哈希,哈希数据结构是一个树;
  25. is the hash function to be used : 哈希函数 sdbmdjb2 wt6

2、server 定义后端主机的各服务器及其选项;

syntax:

  1. server
    [:port] [settings ...]
  2. default-server [settings ...]

:服务器在haproxy上的内部名称;出现在日志及警告信息;

:服务器地址,支持使用主机名;
[:[port]]:端口映射;省略时,表示同bind中绑定的端口;
[settings]:参数(常用参数如下)

  1. maxconn :当前server的最大并发连接数;
  2. backlog :当前server的连接数达到上限后的后援队列长度;
  3. backup:设定当前server为备用服务器;
  4. check:对当前server做健康状态检测;
  5. addr :检测时使用的IP地址;
  6. port :针对此端口进行检测;
  7. inter :连续两次检测之间的时间间隔,默认为2000ms;
  8. rise :连续多少次检测结果为"成功"才标记服务器为可用;默认为2
  9. fall :连续多少次检测结果为"失败"才标记服务器为不可用;默认为3
  10. cookie :为当前server指定其cookie值,用于实现基于cookie的会话黏性;
  11. disabled:标记为不可用;
  12. on-error :后端服务故障时的行动策略;
  13. - fastinter: force fastinter - fail-check: simulate a failed check, also forces fastinter (default)
  14. - sudden-death: simulate a pre-fatal failed health check, one more failed check will mark a server down, forces fastinter
  15. - mark-down: mark the server immediately down and force fastinter
  16. redir :将发往此server的所有GETHEAD类的请求重定向至指定的URL
  17. weight :权重,默认为1;

注意:httpchk,”smtpchk”, “mysql-check”, “pgsql-check” and “ssl-hello-chk” 用于定义应用层检测方法;

3、cookie

对于cookie的定义

syntax:cookie [ rewrite ‘ insert ‘ prefix ] [ indirect ] [ nocache ] [ postonly ] [ preserve ] [ httponly ] [ secure ] [ domain ]* [ maxidle ] [ maxlife ]

context:defaults、listen、backend

参数含义如下:

  1. is the name of the cookie which will be monitored, modified or inserted in order to bring persistence.
  2. rewirte:该关键字表示cookie将由服务器提供,haproxy将修改其值以在其中设置服务器的标识符。 当对"Set-cookie""Cache-control"头的复杂组合进行管理留给应用程序时,该模式非常方便。 应用程序可以决定是否适合发出持久性cookie 由于应该监视所有响应,因此该模式在HTTP隧道模式下不起作用。 除非应用程序行为非常复杂和/或中断,否则建议不要以新模式启动新部署。改param"insert","prefix"相冲突。
  3. insert:此关键字指示如果客户端尚未拥有允许其访问此服务器的cookie,则持久cookie将必须由haproxy插入到服务器响应中。 在没有"保存"选项的情况下使用时,如果服务器发出具有相同名称的cookie,则它在处理之前将被删除。 出于这个原因,可以使用此模式升级以"重写"模式运行的现有配置。 cookie只会是一个会话cookie,不会存储在客户端的磁盘上。 默认情况下,除非添加"间接"选项,否则服务器将看到客户端发出的Cookie 由于缓存效果,添加"nocache""postonly"关键字通常是明智的(见下文)。 "rewirte""prefix"关键字不兼容。
  4. prefix:该关键字表示不是依赖专用cookie来保存持久性,而是完成一个现有的cookie 这可能在客户端不支持多个单个cookie并且应用程序已经需要它的某些特定环境中需要。 在这种情况下,只要服务器设置一个名为cookie,它将以服务器的标识符和分隔符为前缀。 前缀将从所有客户端请求中删除,以便服务器仍然可以找到它发出的cookie 由于所有请求和响应都可能被修改,因此该模式不适用于隧道模式。 "insert""rewirte"关键字不兼容。 注意:强烈建议不要将"indirect""prefix"一起使用,否则服务器cookie更新不会发送给客户端。
  5. indirect:如果指定了此选项,则不会将Cookie发送给已有有效cookie的客户端(服务器端处理过的)。 如果服务器本身设置了这样的cookie,它将被删除,除非设置了"preserve"选项。 "insert"模式下,这将另外从发送到服务器的请求中删除cookie,从应用程序的角度使持久性机制完全透明。 注意:强烈建议不要将"indirect""prefix"一起使用,否则服务器cookie更新不会发送给客户端。
  6. nocache:当客户端与HAProxy之间存在缓存时,建议将此选项与insert模式结合使用,因为它确保如果需要插入cookie,则可缓存响应将被标记为不可缓存。 这一点很重要,因为如果所有持久性Cookie都添加到可缓存的主页上,则所有客户都将从外部缓存中获取页面,并且将共享相同的持久性cookie,从而导致一台服务器接收的流量远远超过其他客户端。 另见"insert""postonly"选项。
  7. postonly 此选项可确保仅在对POST请求的响应中执行cookie插入。 它是"nocache"选项的替代方案,因为POST响应不可缓存,所以这可确保持久性Cookie永远不会被缓存。 由于大多数站点在第一次POST之前不需要任何类型的持久性,通常是登录请求,所以这是一种非常高效的优化缓存的方法,无需在缓存中找到持久性cookie
  8. preserve:该选项只能用于"insert""indirect" 它允许服务器本身发出持久性cookie 在这种情况下,如果在响应中找到cookiehaproxy将保持不变。 这对于在实例的注销请求之后结束持久性很有用。 为此,服务器只需发出一个无效值的cookie(例如:空)或过去的日期。 通过将此机制与"禁用404"检查选项相结合,可以执行完全正常关闭,因为用户在注销后肯定会离开服务器。
  9. httponly:该选项告诉haproxy在插入cookie时添加"HttpOnly"cookie属性。 此属性用于使用户代理不会与非HTTP组件共享该Cookie
  10. secure:该选项告诉haproxy在插入cookie时添加"安全"Cookie属性。 该属性用于使用户代理永远不会通过非安全通道发送此cookie,这意味着通过此标志学习的cookie将仅通过SSL / TLS连接呈现。
  11. domain:该选项允许指定插入cookie的域。 它只需要一个参数:一个有效的域名。 如果域名以点开头,则允许浏览器将其用于任何以该名称结尾的主机。 也可以通过多次调用此选项来指定多个域名。 有些浏览器可能对域的数量有小的限制,所以在做这些时要小心。
  12. maxidle:该选项允许插入的cookie在空闲时间后被忽略。它只适用于插入模式的Cookie。当一个cookie被发送到客户端时,这个cookie被发送的日期也被发送。在进一步演示此cookie时,如果日期比参数指示的延迟更早(以秒为单位),则会被忽略。否则,如果需要将响应发送到客户端,它将被刷新。这对于防止永不关闭浏览器的用户在同一台​​服务器上保留时间过长(例如:在更改服务器场大小后)特别有用。当这个选项被设置并且cookie没有日期时,它总是被接受,但在响应中被刷新。这保持了管理员访问他们的网站的能力。未来的日期超过24小时的Cookie将被忽略。这样做可以让管理员修复时区问题,而不会冒用户离开网站的风险。
  13. maxlife:此选项允许插入的Cookie在一段时间后被忽略,无论它们是否正在使用。它只适用于插入模式cookie。当cookie首次发送给客户端时,发送此cookie的日期也会发送。在进一步演示此cookie时,如果日期比参数指示的延迟更早(以秒为单位),则它将被忽略。如果请求中的Cookie没有日期,则会被接受并设置日期。未来的日期超过24小时的Cookie将被忽略。这样做可以让管理员修复时区问题,而不会冒用户离开网站的风险。与maxidle相反,这个值不会刷新,只有第一次访问的日期才算。当时可能会使用maxidlemaxlife。这对于防止永不关闭浏览器的用户在同一台​​服务器上保留时间过长(例如:在更改服务器场大小后)特别有用。这比maxidle方法更强,因为它会在一定的绝对延迟后强制执行重新调度。

示例:

  1. 实现session sticky
  2. backend websrvs
  3. cookie WEBSRV insert nocache indirect
  4. server srv1 172.16.100.6:80 weight 2 check rise 1 fall 2 maxconn 3000 cookie srv1
  5. server srv2 172.16.100.7:80 weight 1 check rise 1 fall 2 maxconn 3000 cookie srv2

4、errorfile

Return a file contents instead of errors generated by HAProxy

context:all

  1. codeHTTP status code
  2. fileBe recommended error file

for example:

  1. errorfile 400 /etc/haproxy/errorfiles/400badreq.http
  2. errorfile 408 /dev/null # workaround Chrome pre-connect bug
  3. errorfile 403 /etc/haproxy/errorfiles/403forbid.http
  4. errorfile 503 /etc/haproxy/errorfiles/503sorry.http

5、errorloc
errorloc302

  1. Return an HTTP redirection to a URL instead of errors generated by HAProxy
  1. codeHTTP status code
  2. urlBe recommended error uri

6、option httpchk

syntax:

  1. option httpchk
  2. option httpchk
  3. option httpchk

定义基于http协议的7层健康状态检测机制;

syntax:http-check expect [!]

  1. Make HTTP health checks consider response contents or specific status codes

六、listen配置段

包括frontend和backend的配置,listen端主要用作tcp的转发,这里不详说

七、状态页stats

大多数stats配置可以用于defaultslistenbackendfrontend上下文

1、stats enable

基于默认的参数启用stats page有如下配置

  1. - stats uri : /haproxy?stats
  2. - stats realm : "HAProxy Statistics"
  3. - stats auth : no authentication
  4. - stats scope : no restriction 状态统计:没有限制

2、stats auth :认证时的账号和密码,可使用多次;

3、stats realm 认证时的realm;

4、stats uri 自定义stats page uri stats refresh 设定自动刷新时间间隔;

5、stats admin { if ‘ unless } 启用stats page中的管理功能(默认不启用,不安全,不可用于defaults上下文)、

6、stats refresh 页面数据刷新间隔

7、stats scope { ‘ “.” } 启用统计信息并限制访问范围

总体配置示例1:

  1. listen stats
  2. bind :9099
  3. stats enable
  4. stats realm HAPorxy\ Stats\ Page
  5. stats auth admin:admin
  6. stats admin if TRUE

总体配置示例2:

  1. listen stats
  2. bind :9099
  3. stats enable
  4. stats hide-version
  5. stats scope .
  6. stats uri /admin?stats
  7. stats realm Haproxy\ Statistics
  8. stats auth admin1:AdMiN123
  9. stats auth admin2:AdMiN321

八、ACL访问控制

1、acl

acl [flags] [operator]

Declare or complete an access list.

context : defaultsfrontendlisten

for example:

  1. acl invalid_src src 0.0.0.0/7 224.0.0.0/3
  2. acl invalid_src src_port 0:1023
  3. acl local_dst hdr(host) -i localhost

的类型:

  1. - boolean
  2. - integer or integer range
  3. - IP address / network
  4. - string (exact, substring, suffix, prefix, subdir, domain)
  5. - regular expression
  6. - hex block
  1. -i : ignore case during matching of all subsequent patterns.
  2. -m : use a specific pattern matching method
  3. -n : forbid the DNS resolutions
  4. -u : force the unique id of the ACL
  5. -- : force end of flags. Useful when a string looks like one of the flags.

[operator]

匹配整数值:

  1. eqgegtlelt

匹配字符串:

  1. - exact match (-m str) : the extracted string must exactly match the patterns ;
  2. - substring match (-m sub) : the patterns are looked up inside the extracted string, and the ACL matches if any of them is found inside ;
  3. - prefix match (-m beg) : the patterns are compared with the beginning of the extracted string, and the ACL matches if any of them matches.
  4. - suffix match (-m end) : the patterns are compared with the end of the extracted string, and the ACL matches if any of them matches.
  5. - subdir match (-m dir) : the patterns are looked up inside the extracted string, delimited with slashes ("/"), and the ACL matches if any of them matches.
  6. - domain match (-m dom) : the patterns are looked up inside the extracted string, delimited with dots ("."), and the ACL matches if any of them matches.

acl作为条件时的逻辑关系:

  1. - AND (implicit)
  2. - OR (explicit with the "or" keyword or the "''" operator)
  3. - Negation with the exclamation mark ("!")

for example:

  1. if invalid_src invalid_port
  2. if invalid_src '' invalid_port
  3. if ! invalid_src invalid_port

  1. dst : ip
  2. dst_port : integer
  3. src : ip
  4. src_port : integer

for example:

  1. acl invalid_src src 172.16.200.2

path : string

This extracts the request’s URL path, which starts at the first slash and ends before the question mark (without the host part).

for example:

  1. /path;

syntax:

  1. path : exact string match
  2. path_beg : prefix match
  3. path_dir : subdir match
  4. path_dom : domain match
  5. path_end : suffix match
  6. path_len : length match
  7. path_reg : regex match
  8. path_sub : substring match

for example:

  1. path_beg /images/
  2. path_end .jpg .jpeg .png .gif
  3. path_reg ^/images.*\.jpeg$
  4. path_sub image
  5. path_dir jpegs
  6. path_dom ilinux
  7. /images/jpegs/20180312/logo.jpg

url : string

This extracts the request’s URL as presented in the request. A typical use is with prefetch-capable caches, and with portals which need to aggregate multiple information from databases and keep them in caches.

syntax:

  1. url : exact string match
  2. url_beg : prefix match
  3. url_dir : subdir match
  4. url_dom : domain match
  5. url_end : suffix match
  6. url_len : length match
  7. url_reg : regex match
  8. url_sub : substring match

req.hdr([[,]]) : string

This extracts the last occurrence of header in an HTTP request.

  1. hdr([[,]]) : exact string match
  2. hdr_beg([[,]]) : prefix match
  3. hdr_dir([[,]]) : subdir match
  4. hdr_dom([[,]]) : domain match
  5. hdr_end([[,]]) : suffix match
  6. hdr_len([[,]]) : length match
  7. hdr_reg([[,]]) : regex match
  8. hdr_sub([[,]]) : substring match

for example:

  1. acl bad_curl hdr_sub(User-Agent) -i curl
  2. block if bad_curl
status : integer
返回HTTP响应中包含HTTP状态码的整数,例如302.它主要用于ACL和整数范围内,例如,如果响应不是3xx,则删除任何位置标头。
2、内建ACL
  1. ACL name Equivalent to Usage
  2. FALSE always_false never match
  3. HTTP req_proto_http match if protocol is valid HTTP
  4. HTTP_1.0 req_ver 1.0 match HTTP version 1.0
  5. HTTP_1.1 req_ver 1.1 match HTTP version 1.1
  6. HTTP_CONTENT hdr_val(content-length) gt 0 match an existing content-length
  7. HTTP_URL_ABS url_reg ^[^/:]*:// match absolute URL with scheme
  8. HTTP_URL_SLASH url_beg / match URL beginning with "/"
  9. HTTP_URL_STAR url * match URL equal to "*"
  10. LOCALHOST src 127.0.0.1/8 match connection from local host
  11. METH_CONNECT method CONNECT match HTTP CONNECT method
  12. METH_GET method GET HEAD match HTTP GET or HEAD method
  13. METH_HEAD method HEAD match HTTP HEAD method
  14. METH_OPTIONS method OPTIONS match HTTP OPTIONS method
  15. METH_POST method POST match HTTP POST method
  16. METH_TRACE method TRACE match HTTP TRACE method
  17. RDP_COOKIE req_rdp_cookie_cnt gt 0 match presence of an RDP cookie
  18. REQ_CONTENT req_len gt 0 match data in the request buffer
  19. TRUE always_true always match
  20. WAIT_END wait_end wait for end of content analysis

3、ACL调用之block(阻塞acl列表访问)

syntax:

block { if ‘ unless }

for example:

  1. acl invalid_src src 0.0.0.0/7 224.0.0.0/3
  2. acl invalid_src src_port 0:1023
  3. acl local_dst hdr(host) -i localhost
  4. block if invalid_src '' local_dst

4、use_backend

Switch to a specific backend if/unless an ACL-based condition is matched.

context:frontend、listen

for example:

  1. frontend
  2. bind 80
  3. use_backend staticsrvs if url_static
  4. default_backend webserver

5、 http-request { allow ‘ deny } [ { if ‘ unless } ]

Access control for Layer 7 requests

context:frontend、backend、listen

6、tcp-request connection {accept’reject} [{if ‘ unless} ]

Perform an action on an incoming connection depending on a layer 4 condition

context:frontend、backend、listen

for example:

  1. listen ssh
  2. bind :22022
  3. balance leastconn
  4. acl invalid_src src 172.16.200.2
  5. tcp-request connection reject if invalid_src
  6. mode tcp
  7. server sshsrv1 172.16.100.6:22 check
  8. server sshsrv2 172.16.100.7:22 check backup

九、SSL配置

1、http —> https

必须在mod http的模式下进行

1) 支持ssl会话;

  1. bind *:443 ssl crt /PATH/TO/SOME_PEM_FILE

crt后的证书文件要求PEM格式,且同时包含证书和与之匹配的所有私钥;

  1. cat demo.crt demo.key > demo.pem

2) 把80端口的请求重向定443;

  1. bind *:80
  2. redirect scheme https if !{ ssl_fc }

另一种配置:对非ssl的任何url的访问统统定向至https主机的主页;

  1. redirect location https://172.16.0.67/ if !{ ssl_fc }

3) 如何向后端传递用户请求的协议和端口

  1. http_request set-header X-Forwarded-Port %[dst_port]
    http_request add-header X-Forwared-Proto https if { ssl_fc }

文档更新时间: 2018-12-20 10:56   作者:张尚