HAProxy 高级配置选项

一、HAProxy利用cookie实现会话保持

在没有缓存服务器做session共享的情况下,可以使用haproxy插入cookie实现session共享。
在这里插入图片描述

#配置选项
cookie name insert indirect nocache

name:cookie的key名称,⽤于实现持久连接。
insert:如果客户端请求报⽂没有cookie就插⼊新的cookie到响应报⽂,如第⼀次访问HAProxy。
indirect:不会向客户端发送服务器已经处理过请求的cookie信息,⽐如后端服务器宕机后HAProxy将客户端请求强制转发⾄real server则会涉及修改cookie,不建议配置。
nocache:当client和hapoxy之间有缓存时,haproxy不缓存客户端cookie,因为客户端浏览器会缓存cookie并携带cookie访问haproxy。
#配置示例
listen web_host
	bind 10.0.0.100:80
	mode http
	log global
	balance roundrobin
	cookie SERVER-COOKIE insert indirect nocache
	server web1 192.168.7.103:80 cookie web1 check inter 3000 fall 3 rise 5
	server web2 192.168.7.104:80 cookie web2 check inter 3000 fall 3 rise 5

我们会发现转发到服务器192.168.7.104的cookie的Name为SERVER-COOKIE,Value为web2,和我们所配置的一样。
最终实现只要cookie一样,就能转发到同一个后端服务器。
在这里插入图片描述

二、HAProxy状态页

2.1 状态页配置实现及配置说明

#配置示例
listen stats
	mode http
	bind 0.0.0.0:9999
	stats enable
	log global
	stats uri /haproxy-status
	stats auth haadmin:123456
#配置说明
stats enable    #基于默认的参数启⽤stats page
stats hide-version    #隐藏版本
stats refresh <delay>    #设定⾃动刷新时间间隔
stats uri <prefix>    #⾃定义stats page uri,默认值:/haproxy?stats
stats realm <realm>    #账户认证时的提示信息,示例:stats realm : HAProxy\ Statistics
stats auth <user>:<passwd>    #认证时的账号和密码,可使⽤多次,默认:no authentication
stats admin { if | unless } <cond>    #启⽤stats page中的管理功能,不建议开启

如果配置了状态页管理功能,可以实现在状态页上线或下线服务器
在这里插入图片描述

2.2 状态页显示字段说明

在这里插入图片描述

pid = 3698 (process #2, nbproc = 2, nbthread = 1)
#pid为当前pid号,process为当前进程号,nbproc和nbthread为⼀共多少进程和每个进程多少个线程

uptime = 0d 0h00m08s 
#启动了多⻓时间

system limits: memmax = unlimited; ulimit-n = 131124
#系统资源限制:内存最⼤打开⽂件数,可以通过ulimit -n 1048576设置

maxsock = 131124; maxconn = 65536; maxpipes = 0 
#最⼤socket连接数/单进程最⼤连接数/最⼤管道数maxpipes

current conns = 1; current pipes = 0/0; conn rate = 1/sec 
#当前连接数/当前管道数/当前连接速率

Running tasks: 1/9; idle = 100 % 
#运⾏的任务/当前空闲率

服务器当前状态:
active UP:#绿色,在线服务器
active UP, going down:#浅黄色,监测未通过正在进⼊down过程
active DOWN, going up:#浅橙色,down的服务器正在进⼊up过程
active or backup DOWN:#深橙色,在线的服务器或者是backup的服务器已经转换成了down状态
active or backup DOWN for maintenance (MAINT)#咖啡色,active或者backup服务器⼈为下线的
active or backup SOFT STOPPED for maintenance#深蓝色,active或者backup被⼈为软下线(⼈为将weight改成0)
backup UP:#浅蓝色,标记为backup的服务器
backup UP, going down:#紫色,备份服务器正在进⼊down过程
backup DOWN, going up:#粉色,备份服务器正在进⼊up过程
not checked:#灰色,标记为不监测的服务器

手动下线服务器

#手动下线服务器
echo "disable server web1/10.0.0.13" /var/lib/haproxy/haproxy.sock

#如果haproxy有四个进程,配置示例
nbproc 4
cpu-map 1 0
cpu-map 2 1
cpu-map 3 2
cpu-map 4 3
stats socket /var/ tib/haproxy/haproxy1.sock mode 600 Level admin process 1
stats socket /var/ lib/haproxy/haproxy2.sock mode 600 level admin process 2
stats socket /var/ tib/haproxy/haproxy3.sock mode 600 Level admin process 3
stats socket /var/ tib/haproxy/haproxy4.sock mode 600 level admin process 4

#关闭四个进程
echo "disable server web1/10.0.0.13" /var/lib/haproxy/haproxy1.sock
echo "disable server web1/10.0.0.13" /var/lib/haproxy/haproxy2.sock
echo "disable server web1/10.0.0.13" /var/lib/haproxy/haproxy3.sock
echo "disable server web1/10.0.0.13" /var/lib/haproxy/haproxy4.sock

三、修改HTTP的请求报文和响应报文

在http模式下,基于实际需求修改客户端的请求报⽂与响应报⽂,通过reqadd和reqdel在请求报⽂添加删除字段,通过rspadd与rspidel在响应报⽂中添加与删除字段。

#在请求报⽂尾部添加指定⾸部
reqadd <string> [{if | unless} <cond>]
 
#从请求报⽂中删除匹配正则表达式的⾸部
reqdel <search> [{if | unless} <cond>]
reqidel <search> [{if | unless} <cond>]
 
#在响应报⽂尾部添加指定⾸部
rspadd <string> [{if | unless} <cond>] 
示例:
rspadd X-Via:\ HAPorxy #2.1版本之前
http-response add-header magedu n50 #2.1版本之后


#从响应报⽂中删除匹配正则表达式的⾸部
rspidel <search> [{if | unless} <cond>]
rspidel <search> [{if | unless} <cond>]
示例:
rspidel server.* #2.1版本之前,从响应报⽂删除server信息
http-response del-header server #2.1版本之后,从响应报⽂删除server信息
rspidel X-Powered-By:.* #从响应报⽂删除X-Powered-By信息

四、压缩功能

对响应给客户端的报⽂进⾏压缩,以节省⽹络带宽,但是会占⽤部分CPU性能。一般haproxy不做压缩,只在nginx做压缩。

4.1 配置选项

compression algo #启⽤http协议中的压缩机制,常⽤算法有gzip、deflate
	identity #调试使⽤的压缩⽅式
	gzip #常⽤的压缩⽅式,与各浏览器兼容较好
	deflate #有些浏览器不⽀持
	raw-deflate #新出的压缩⽅式
compression type #要压缩的⽂件类型

4.2 配置示例

listen web_host
	bind 192.168.7.101:80
	mode http
	balance roundrobin
	log global
	option httplog
	compression algo gzip deflate
	compression type compression type text/plain text/html text/css text/xml text/javascript application/javascript
	cookie SERVER-COOKIE insert indirect nocache
	server web1 192.168.7.103:80 cookie web1 check inter 3000 fall 3 rise 5
	server web2 192.168.7.104:80 cookie web2 check inter 3000 fall 3 rise 5

五、三种方式实现对后端WEB服务器进行状态检测

5.1 基于IP+Port做状态检测

#inter 3s表示每次检测间隔3s,一般在3-5s之间
#fall 3表示连续检测三次挂了之后,服务器down
#rise 5表示在down的情况下,连续检测五次成功之后,服务器up
#check addr指定检测IP,check port指定检测端口
server 172.31.7.104 172.31.7.104:80 cookie web2 weight 2 check inter 3s fall 3 rise 5
server 172.31.7.104 172.31.7.104:80 cookie web2 weight 2 check addr 172.31.7.103 port 80 inter 3s fall 3 rise 5

5.2 指定URI做状态检测

#相当于在haproxy上使用curl去访问URL:curl http://172.31.7.104:80/app/monitor/check.html
#一般这个URI只对服务器做监控,不对业务进行处理
listen web_host
	bind 192.168.7.101:80
	mode http
	balance roundrobin
	log global
	option httpchk GET /app/monitor/check.html HTTP/1.0
	server 172.31.7.104 172.31.7.104:80 cookie web2 weight 2 check inter 3s fall 3 rise 5

5.3 指定URI的request请求头部内容做状态检测

listen web_host
	bind 192.168.7.101:80
	mode http
	balance roundrobin
	log global
	option httpchk HEAD /app/monitor/check.html HTTP/1.0\r\nHost:\ 192.168.7.102
	server 172.31.7.104 172.31.7.104:80 cookie web2 weight 2 check inter 3s fall 3 rise 5

六、ACL

访问控制列表(ACL,Access Control Lists)是⼀种基于包过滤的访问控制技术,它可以根据设定的条件对经过服务器传输的数据包进⾏过滤(条件匹配),即对接收到的报⽂进⾏匹配和过滤,基于请求报⽂头部中的源地址、源端⼝、⽬标地址、⽬标端⼝、请求⽅法、URL、⽂件后缀等信息内容进⾏匹配并执⾏进⼀步操作,⽐如允许其通过或丢弃。

6.1 配置选项

acl <aclname> <criterion> [flags] [operator] [<value>]
acl    名称      匹配规范   匹配模式  具体操作符  操作对象类型
#示例:
#ACL模板:
acl image_service hdr_dom(host) -i img.magedu.com
        ACL名称     ACL匹配规范 匹配模式   操作对象
#调用:
use_backend img_site_host if image_service
             后端服务器名称       ACL-Name

ACL-name:ACL的名字

ACL名称,可以使⽤⼤字⺟A-Z、⼩写字⺟a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分⼤⼩写
⽐如Image_site和image_site就是两个完全不同的acl

ACL-criterion:定义ACL匹配规范

hdr([<name> [<occ>]]):完全匹配字符串,header的指定信息
hdr_beg([<name> [<occ>]]):前缀匹配,header中指定匹配内容的begin
hdr_end([<name> [<occ>]]):后缀匹配,header中指定匹配内容end
hdr_dom([<name> [<occ>]]):域匹配,header中的domain name
 
hdr_dir([<name> [<occ>]]):路径匹配,header的uri路 径
hdr_len([<name> [<occ>]]):⻓度匹配,header的⻓度匹配
hdr_reg([<name> [<occ>]]):正则表达式匹配,⾃定义表达式(regex)模糊匹配
hdr_sub([<name> [<occ>]]):⼦串匹配,header中的uri模糊匹配

dst:⽬标IP
dst_port:⽬标PORT
src:源IP
src_port:源PORT

#示例:
hdr(<string>) ⽤于测试请求头部⾸部指定内容
hdr_dom(host) 请求的host名称,如 www.magedu.com
hdr_beg(host) 请求的host开头,如 www. img. video. download. ftp.
hdr_end(host) 请求的host结尾,如 .com .net .cn
path_beg 请求的URL开头,如/static、/images、/img、/css
path_end 请求的URL中资源的结尾,如 .gif .png .css .js .jpg .jpeg

有些功能是类似的,⽐如以下⼏个都是匹配⽤户请求报⽂中host的开头是不是www:
acl short_form hdr_beg(host) www.
acl alternate1 hdr_beg(host) -m beg www.
acl alternate2 hdr_dom(host) -m beg www.
acl alternate3 hdr(host) -m beg www.

ACL-flags:ACL匹配模式

-i 不区分⼤⼩写
-m 使⽤指定的pattern匹配⽅法
-n 不做DNS解析
-u 禁⽌acl重名,否则多个同名ACL匹配或关系

ACL-operator:ACL操作符(几乎不用)

整数⽐较:eq、ge、gt、le、lt
字符⽐较:
- exact match (-m str) :字符串必须完全匹配模式
- substring match (-m sub) :在提取的字符串中查找模式,如果其中任何⼀个被发现,ACL将匹配
- prefix match (-m beg) :在提取的字符串⾸部中查找模式,如果其中任何⼀个被发现,ACL将匹配
- suffix match (-m end) :将模式与提取字符串的尾部进⾏⽐较,如果其中任何⼀个匹配,则ACL进⾏匹配
- subdir match (-m dir) :查看提取出来的⽤斜线分隔(“/”)的字符串,如果其中任何⼀个匹配,则ACL进⾏匹配
- domain match (-m dom) :查找提取的⽤点(“.”)分隔字符串,如果其中任何⼀个匹配,则ACL进⾏匹配

ACL-value:value的类型

The ACL engine can match these types against
patterns of the following types :
- Boolean #布尔值
- integer or integer range #整数或整数范围,⽐如⽤于匹配端⼝范围
- IP address / network #IP地址或IP范围, 192.168.0.1,192.168.0.1/24
- string--> www.magedu.com
	exact –精确⽐较
	substring—⼦串
	suffix-后缀⽐较
	prefix-前缀⽐较
	subdir-路径, /wp-includes/js/jquery/jquery.js
	domain-域名,www.magedu.com
- regular expression #正则表达式
- hex block #16进制

6.2 ACL的调用

use_backend img_site_host if valid_src valid_port    #与关系,A和B都要满⾜为true
use_backend img_site_host if invalid_src || invalid_port    #或,A或者B满⾜⼀个为true
use_backend img_site_host if ! invalid_src    #⾮,取反,A和B哪个也不满⾜为true

6.3 ACL示例—根据域名转发后端指定服务器

listen web_host
	bind 100.0.0.100:80
	mode http
	balance roundrobin
	log global
	option httplog
####################acl setting#######################
	acl web_host1 hdr_dom(host) -i www.lck.net
	acl web_host2 hdr_dom(host) -i www.lck.com
########################use acl#######################
	use_backend lck_host_1 if web_host
	use_backend lck_host_2 if web_host
###################default backend####################
	default_backend default_web 

######################backend#########################
backend lck_host_1
	mode http
	server web1 192.168.7.103 check inter 2000 fall 3 rise 5

backend lck_host_2
	mode http
	server web1 192.168.7.104 check inter 2000 fall 3 rise 5

backend default_web
	mode http
	server web1 192.168.7.104:80 check inter 2000 fall 3 rise 5

使用场景:当公网地址不够用的时候,把所有的请求都发往haproxy的frontend的bind监听地址,然后通过acl判断用户所请求的域名,然后根据不同域名转发到后端不同服务器。

6.4 ACL示例—基于源IP—permit

listen web_host
	bind 100.0.0.100:80
	mode http
	balance roundrobin
	log global
	option httplog
####################acl setting#######################
#172.18.0.0/16 或 192.168.7.103访问haproxy,则调用后端服务器lck_host
	acl ip_range_test src 172.18.0.0/16 192.168.7.103
########################use acl#######################
	use_backend lck_host if ip_range_test
###################default backend####################
	default_backend default_web 

######################backend#########################
backend lck_host 
	mode http
	server web1 192.168.7.103 check inter 2000 fall 3 rise 55

backend default_web
	mode http
	server web1 192.168.7.104:80 check inter 2000 fall 3 rise 5

6.5 ACL示例—基于源IP—deny

listen web_host
	bind 100.0.0.100:80
	mode http
	balance roundrobin
	log global
	option httplog
####################acl setting#######################
#172.18.0.0/16 或 192.168.7.103访问haproxy,直接deny
	acl block_test src 172.18.0.0/16 192.168.7.103
########################use acl#######################
#2.1版本之前指令是block,2.1版本之后指令是http-request deny
	http-request deny if block_test
###################default backend####################
	default_backend default_web 

######################backend#########################
backend lck_host 
	mode http
	server web1 192.168.7.103 check inter 2000 fall 3 rise 55

backend default_web
	mode http
	server web1 192.168.7.104:80 check inter 2000 fall 3 rise 5

6.6 ACL示例—根据浏览器类型转发指定后端服务器

listen web_host
	bind 192.168.7.101:80
	mode http
	balance roundrobin
	log global
	option httplog
	####################acl setting#######################
	acl redirect_test hdr(User-Agent) -m sub -i "Mozilla/5.0 (Windows NT 6.1; WOW64;Trident/7.0;rv:11.0) like Gecko"
	########################use acl#######################
	#redirect prefix http://192.168.7.103 if redirect_test
	use_backend lck_host if redirect_test
	
	default_backend default_web

backend lck_host
	mode http
	server web1 192.168.7.103 check inter 2000 fall 3 rise 5

backend default_web
	mode http
	server web1 192.168.7.104:80 check inter 2000 fall 3 rise 5

6.7 ACL示例—基于文件后缀名实现动静分离

listen web_host
	bind 192.168.7.101:80
	mode http
	balance roundrobin
	log global
	option httplog
	acl php_server path_end -i .php
	use_backend php_server_host if php_server
	acl image_server path_end -i .jpg .png .jpeg .gif
	use_backend image_server_host if image_server
 	default_backend default_web
 
backend php_server_host
	mode http
 	server web1 192.168.7.103 check inter 2000 fall 3 rise 5
 
backend image_server_host
	mode http
	server web1 192.168.7.104 check inter 2000 fall 3 rise 5
 
backend default_web
	mode http
	server web1 192.168.7.102:80 check inter 2000 fall 3 rise 5

6.8 ACL示例—匹配访问路径实现动静分离

listen web_host
	bind 192.168.7.101:80
	mode http
	balance roundrobin
	log global
	option httplog
	acl static_path path_beg -i /static /images /javascript
	use_backend static_path_host if static_path
	default_backend default_web

backend static_path_host
	mode http
	server web1 192.168.7.104 check inter 2000 fall 3 rise 5
 
backend default_web
	mode http
	server web1 192.168.7.102:80 check inter 2000 fall 3 rise 5

七、自定义HAProxy错误界面

如果后端服务器全挂了,会默认显示503状态码

7.1 指定转到错误页面

defaults
errorfile 500 /usr/local/haproxy/html/500.html
errorfile 502 /usr/local/haproxy/html/502.html
errorfile 503 /usr/local/haproxy/html/503.html
#2.1版本之后要按照以下格式,2.1版本之前可以随意写
cat /usr/1ocal/haproxy/html/503.html
HTTP/1.1 503 Service Unavailable
Content-Type: text/html;charset=utf-8

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>error</title>
</head>
<body>
<h1>页面维护中</h1>
<h2>503请联系总部: 18600000000</h2>
</body>
</html>

7.2 http重定向到指定页面

defaults
errorloc 503 http://192.168.7.102/error_page/503.html

八、HAProxy四层负载

应⽤场景:Memcache、Redis、MySQL、RabbitMQ

8.1实现四层负载

listen redis-port
	bind 192.168.7.102:6379
	mode tcp
	balance leastconn
	server server1 192.168.7.104:6379 check
	server server1 192.168.7.103:6379 check backup

8.2实现四层访问控制

listen web_host
	bind 192.168.7.101:80
	mode http
	balance roundrobin
	log global
	option httplog
 
	acl invalid_src src 192.168.1.0/24 192.168.7.102
	tcp-request connection reject if invalid_src
 
	default_backend default_web

backend php_server_host
	mode http
 	server web1 192.168.7.103 check inter 2000 fall 3 rise 5
 
backend static_path_host
	mode http
 	server web1 192.168.7.104 check inter 2000 fall 3 rise 5
 
backend default_web
	mode http
	server web1 192.168.7.102:80 check inter 2000 fall 3 rise 5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值