1、基于cookie的会话保持
cookie value:为当前server指定cookie值,实现基于cookie的会话黏性
1.1、配置选项
cookie name [ rewrite | insert | prefix ][ indirect ] [
nocache ][ postonly ] [ preserve ][ httponly ] [ secure ][
domain ]* [ maxidle <idle> ][ maxlife ]
name:cookie 的key名称,⽤于实现持久连接
insert:#如果客⼾端请求报⽂没有cookie就插⼊新的cookie到响应报
⽂,如第⼀次访问HAProxy
indirect:#不会向客⼾端发送服务器已经处理过请求的cookie信息,
⽐如后端服务器宕机后
HAProxy将客⼾端请求强制转发⾄real server则会涉
及修改cookie,不建议配置
nocache:#当client和hapoxy之间有缓存时,haproxy不缓存客⼾端
cookie,因为客⼾端浏览器会
缓存cookie并携带cookie访问haproxy
1.2、配置⽰例
listen web_host
bind 192.168.1.61:80
mode http
log global
balance roundrobin
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.1.51:80 cookie web1 check inter
3000 fall 3 rise 5
server web2 192.168.1.52:80 cookie web2 check inter
3000 fall 3 rise 5
1.3、验证cookie信息
[root@server3 app]# curl --cookie "SERVER-COOKIE=web1"
http://192.168.1.61/app/index.html
app1 192.168.1.51
[root@server3 app]# curl --cookie "SERVER-COOKIE=web2"
http://192.168.1.61/app/index.html
app2 192.168.1.52
2、HAProxy状态⻚
通过web界⾯,显⽰当前HAProxy的运⾏状态。
2.1、状态⻚配置项
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、启⽤状态⻚
listen stats
bind :9009
stats enable
#stats hide-version
stats uri /haproxy-status
stats realm HAPorxy\ Stats\ Page
stats auth haadmin:123456
stats auth admin:123456
#stats refresh 30s
#stats admin if TRUE
2.3、登录状态⻚
pid = 3698 (process #2, nbproc = 2, nbthread = 1)
#pid为当
前pid号,process为当前进程号,nbproc和nbthread为⼀共多少进程和每
个进程多少个线程
uptime = 0d 0h00m08s
#启动了多⻓时间
system limits: memmax = unlimited; ulimit-n = 131124
#系统
资源限制:内存/最⼤打开⽂件数/
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:#在线服务器
backup UP:#标记为backup的服务器
active UP, going down:#监测未通过正在进⼊down过程
backup UP, going down:#备份服务器正在进⼊down过程
active DOWN, going up:#down的服务器正在进⼊up过程
backup DOWN, going up:#备份服务器正在进⼊up过程
active or backup DOWN:#在线的服务器或者是backup的服务器已经转换
成了down状态
not checked:#标记为不监测的服务器
active or backup DOWN for maintenance (MAINT)
#active或者
backup服务器⼈为下线的
active or backup SOFT STOPPED for maintenance
#active或者
backup被⼈为软下线(⼈为将weight改成0)
![图片[1]-HAProxy ⾼级功能及配置-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-16.png)
2.4、backend server信息
![图片[2]-HAProxy ⾼级功能及配置-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-17.png)
3、报⽂修改
在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
从响应报⽂中删除匹配正则表达式的⾸部
rspidel <search> [{if | unless} <cond>]
rspidel <search> [{if | unless} <cond>]
⽰例:
rspidel server.* #从响应报⽂删除server信息
rspidel X-Powered-By:.* #从响应报⽂删除X-Powered-By信息
4、HAProxy⽇志配置
配置HAProxy记录⽇志到指定⽇志⽂件中
4.1、HAProxy配置
在global配置项定义:
log 127.0.0.1 local{1-7} info
#基于syslog记录⽇志到指定设备,
级别有(err、warning、info、debug)
listen web_port
bind 127.0.0.1:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2
rise 5
# systemctl restart haproxy
4.2、Rsyslog配置
vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
# systemctl restart rsyslog
4.3、验证HAProxy⽇志
重启syslog服务并访问app⻚⾯,然后验证是否⽣成⽇志
# tail -f /var/log/haproxy.log
5、⾃定义⽇志格式
将特定信息记录在⽇志中
5.1、配置选项
capture cookie <name> len <length> #捕获请求和响应报⽂中的
cookie并记录⽇志
capture request header <name> len <length> #捕获请求报⽂中指
定的⾸部内容和⻓度并记录⽇志
capture response header <name> len <length> #捕获响应报⽂中指
定的内容和⻓度⾸部并记录⽇志
⽰例:
capture request header Host len 256
capture request header User-Agent len 512
capture request header Referer len 15
5.2、配置⽰例
listen web_host
bind 192.168.6.61:80
mode http
balance roundrobin
log global
option httplog #⽇志格式选项
capture request header X-Forwarded-For len 15
capture request header User-Agent len 512
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.5.51:80 cookie web1 check inter
3000 fall 3 rise 5
server web2 192.168.5.52:80 cookie web2 check inter
3000 fall 3 rise
6、压缩功能
对响应给客⼾端的报⽂进⾏压缩,以节省⽹络带宽,但是会占⽤部分CPU性能。
6.1、配置选项
compression algo # 启⽤http协议中的压缩机制,常⽤算法有gzip
deflate
identity # 调试使⽤的压缩⽅式
gzip # 常⽤的压缩⽅式,与各浏览器兼容较好
deflate # 有些浏览器不⽀持
raw-deflate # 新出的压缩⽅式
compression type # 要压缩的⽂件类型
6.2、配置⽰例
listen web_host
bind 192.168.6.61:80
mode http
balance roundrobin
log global
option httplog
#capture request header X-Forwarded-For len 15
#capture request header User-Agent len 512
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.5.51:80 cookie web1 check inter
3000 fall 3 rise 5
server web2 192.168.5.52:80 cookie web2 check inter
3000 fall 3 rise 5
7、web服务器状态监测
基于不同的监测⽅式,对后端real server进⾏状态监测
7.1、三种状态监测⽅式
基于四层的传输端⼝做状态监测
基于指定URI 做状态监测
基于指定URI的request请求头部内容做状态监测
7.2、配置⽰例
listen web_host
bind 192.168.6.61:80
mode http
balance roundrobin
log global
option httplog
#option httpchk GET /app/monitor/check.html HTTP/1.0
option httpchk HEAD /app/monitor/check.html
HTTP/1.0\r\nHost:\ 192.168.7.102
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.5.51:80 cookie web1 check inter
3000 fall 3 rise 5
server web2 192.168.5.52:80 cookie web2 check inter
3000 fall 3 rise 5
![图片[3]-HAProxy ⾼级功能及配置-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-18.png)
8、⾃定义HAProxy错误界⾯
对指定的报错进⾏重定向,进⾏优雅的显⽰错误⻚⾯
8.1、基于错误⻚⾯⽂件
defaults
#option forwardfor
#no option http-use-htx
#...... #以下三⾏
errorfile 500 /usr/local/haproxy/html/500.html
errorfile 502 /usr/local/haproxy/html/502.html
errorfile 503 /usr/local/haproxy/html/503.html
# cat /usr/local/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>
8.2、基于http重定向
defaults
#option http-keep-alive
#option forwardfor
#no option http-use-htx
#...... 以下⼀⾏
errorloc 503 http://192.168.7.102/error_page/503.html
9、HAProxy四层负载
针对有特殊访问写完的应⽤场景
- Memcache
- Redis
- MySQL
- RabbitMQ
listen redis-port
bind 192.168.6.61:6379
mode tcp
balance leastconn
server server1 192.168.5.51:6379 check
server server1 192.168.5.52:6379 check backup
10、HAProxy https实现
配置HAProxy⽀持https协议:
⽀持ssl会话;
bind *:443 ssl crt /PATH/TO/SOME_PEM_FILE
crt 后证书⽂件为PEM格式,且同时包含证书和所有私钥
cat demo.crt demo.key > demo.pem
把80端⼝的请求重向定443
bind *:80
redirect scheme https if !{ ssl_fc }
向后端传递⽤⼾请求的协议和端⼝(frontend或backend)
http_request set-header X-Forwarded-Port %[dst_port]
http_request add-header X-Forwared-Proto https if {
ssl_fc }
# 证书制作
# mkdir /usr/local/haproxy/certs
# cd /usr/local/haproxy/certs
# openssl genrsa -out haproxy.key 2048
# openssl req -new -x509 -key haproxy.key -out
haproxy.crt -subj "/CN=www.test.net"
# cat haproxy.key haproxy.crt > haproxy.pem
# openssl x509 -in haproxy.pem -noout -text #查看证书
# https配置⽰例
#web server http
frontend web_server-http
bind 192.168.6.61:80
redirect scheme https if !{ ssl_fc }
mode http
use_backend web_host
#web server https
frontend web_server-https
bind 192.168.5.51:443 ssl crt
/usr/local/haproxy/certs/haproxy.pem
mode http
use_backend web_host
backend default_host
mode http
server web1 192.168.5.52:80 check inter 2000 fall 3
rise 5
backend web_host
mode http
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if {
ssl_fc }
server web1 192.168.5.51:80 check inter 2000 fall 3
rise 5
server web2 192.168.5.52:80 check inter 2000 fall 3
rise 5
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END