haproxy自定义日志格式
配置选项
log global #开启记录日志,默认不开启
option httplog #开启记录httplog日志格式选项
capture coolie <name> len <length> #捕获请求和响应报文中的cookie及值的长度,将之记录到日志
capture request header <name> len <length> #捕获请求报文中指定的首部内容和长度并记录日志
capture response header <name> len <length> #捕获响应报文中指定的内容和长度首部并记录日志
示例:
log global
option httplog
capture request header Host len 256
capture request header User-Agent len 512
capture request header Referer len 15
capture request header X-Forwarded-For len 15
把日志放在haproxy服务器本机记录
主配置文件修改
[root@ubuntu2004 ~]#vim /etc/haproxy/haproxy.cfg
log 127.0.0.1 local3 info
开启514端口
[root@ubuntu2004 ~]#vim /etc/rsyslog.conf
module(load="imudp")
input(type="imudp" port="514")
定义日志存放位置
[root@ubuntu2004 ~]#vim /etc/rsyslog.d/haproxy.conf
local3.info /var/log/haproxy.log
配置server开启日志功能
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
log global
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1 check addr 10.0.0.101 port 80
server web2 10.0.0.102:80 weight 1 check addr 10.0.0.101 port 80
[root@ubuntu2004 ~]#systemctl restart haproxy.service
前端客户端访问
[root@ubuntu2004 ~]#curl www.meng.org
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#tail -f /var/log/haproxy.log
Oct 26 08:46:08 localhost haproxy[1974]: Connect from 192.168.10.123:35682 to 192.168.10.100:80 (WEB_PORT_80/HTTP)
定制日志格式
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
log global
option httplog
capture request header Host len 256
capture request header User-Agent len 512
capture request header Referer len 15
capture request header X-Forwarded-For len 15
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1 check addr 10.0.0.101 port 80
server web2 10.0.0.102:80 weight 1 check addr 10.0.0.102 port 80
[root@ubuntu2004 ~]#systemctl restart haproxy.service
查看访问日志
[root@ubuntu2004 ~]#tail -f /var/log/haproxy.log
Oct 26 08:55:30 localhost haproxy[2285]: 192.168.10.123:57650 [26/Oct/2022:08:55:30.090] WEB_PORT_80 WEB_PORT_80/web1 0/0/0/1/1 200 233 - - ---- 1/1/0/0/0 0/0 {www.wang.org|curl/7.68.0||} "GET / HTTP/1.1"
压缩功能
对响应给客户端的报文进行压缩,以节省网络带宽,但是会占用部分CPU性能
建议在后端服务器开启压缩功能,而非在HAProxy上开启压缩
compression algo <algorithm> ... #启用http协议中的压缩机制,常用算法有 gzip, deflate
#压缩算法<algorithm>支持下面类型:
identity #debug调试使用的压缩方式
gzip #常用的压缩方式,与各浏览器兼容较好
deflate #有些浏览器不支持
raw-deflate #新式的压缩方式
compression type <mime type> ... #要压缩的文件类型
示例:
compression algo gzip deflate
compression type text/html text/css text/plain
配置示例
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
listen WEB_PORT_80
log global
compression algo gzip deflate
compression type compression type text/plain text/html text/css text/xml text/javascript application/javascript
option httplog
capture request header Host len 256
capture request header User-Agent len 512
capture request header Referer len 15
capture request header X-Forwarded-For len 15
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1 check addr 10.0.0.101 port 80
server web2 10.0.0.102:80 weight 1 check addr 10.0.0.102 port 80
[root@ubuntu2004 ~]#systemctl restart haproxy.service
curl 命令不支持压缩,可去状态页F12查看
后端服务器健康性监测
三种状态监测方式
基于四层的传输端口做状态监测,此为默认方式
基于指定 URI 做状态监测,需要访问整个页面资源,占用更多带宽
基于指定 URI 的 request 请求头部内容做状态监测,占用较少带宽,建议使用此方式
基于应用层http协议进行健康性检测
基于应用层http协议,采有不同的监测方式,对后端real server进行状态监测
注意: 此方式会导致在后端服务器生成很多的HAProxy发起的访问日志
option httpchk
#启用七层健康性检测,对tcp 和 http 模式都支持,默认为:OPTIONS /HTTP/1.0 nginx默认不支持 apache支持
option httpchk GET /index.html
示例一:(nginx默认不支持 apache支持)
listen WEB_PORT_80
log global
option httpchk
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1 check addr 10.0.0.101 port 80
server web2 10.0.0.102:80 weight 1 check addr 10.0.0.102 port 80
查看状态页显示active or backup DOWN
示例二:
listen WEB_PORT_80
log global
#option httpchk
option httpchk GET /index.html
bind 192.168.10.100:80
server web1 10.0.0.101:80 weight 1 check addr 10.0.0.101 port 80
server web2 10.0.0.102:80 weight 1 check addr 10.0.0.102 port 80
即使用户不妨问,haproxy也在不断地访问后端服务器,造成资源浪费。
可改成稍微省资源的方法
option httpchk HEAD /index.html
测试:把页面挪走,状态页显示错误,挪过来就恢复正常
ACL(访问控制列表)
多个ACL的组合调用方式
与:隐式(默认)使用
或:使用“or" 或 “||"表示
否定:使用 "!" 表示
#示例:
if valid_src valid_port #与关系,ACL中A和B都要满足为true,默认为与
if invalid_src || valid_port #或,ACL中A或者B满足一个为true
if invalid_src # 非 , 取反 , 不满足ACL才为true
ACL示例:域名匹配
第一步:在客户端解析域名
[root@ubuntu2004 ~]#vim /etc/hosts
192.168.10.100 www.meng.org www.meng.com
第二步:更改haproxy的server配置文件
frontend www.meng.org
bind 192.168.10.100:80
acl meng.org_domain hdr_dom(host) -i www.meng.org
use_backend www.meng.org if meng.org_domain #当meng.org_domain和前面一样,访问www.meng.org
default_backend www.wang.com #默认访问www.meng.com
backend www.meng.org
server web1 10.0.0.101:80 weight 1 check
backend www.meng.com
server web2 10.0.0.102:80 weight 1 check
第三步:客户端访问
[root@ubuntu2004 ~]#curl www.meng.com
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#curl www.meng.org
web1.meng.org 10.0.0.101
ACL示例:基于源IP或子网调度访问
frontend www.meng.org
bind 192.168.10.100:80
acl wang.org_domain hdr_dom(host) -i www.meng.org
acl beijing_net src 192.168.10.123
use_backend www.meng.org if meng.org_domain || beijing_net
#如果wang.org_domain或beijing_net满足,就往www.wang.org上调度
default_backend www.meng.com #默认往www.meng.com调度
backend www.meng.org
server web1 10.0.0.101:80 weight 1 check
backend www.meng.com
server web2 10.0.0.102:80 weight 1 check
192.168.10.123客户端访问
[root@ubuntu2004 ~]#curl www.meng.org
web1.meng.org 10.0.0.101
[root@ubuntu2004 ~]#curl www.meng.com
web1.meng.org 10.0.0.101
访问www.meng.com也是往101上调度,因为是从192.168.10.123上进行访问的
ACL示例:基于源地址的访问控制
加上浏览器版本的判断
frontend www.meng.org
bind 192.168.10.100:80
acl acl_user_agent hdr_sub(User-Agent) -i curl wget
use_backend www.meng.org if acl_user_agent
default_backend www.meng.com
backend www.meng.org
server web1 10.0.0.101:80 weight 1 check
backend www.meng.com
server web2 10.0.0.102:80 weight 1 check
访问
[root@ubuntu2004 ~]#curl www.meng.com
web1.meng.org 10.0.0.101
[root@ubuntu2004 ~]#curl www.meng.org
web1.meng.org 10.0.0.101
用的curl浏览器 都往101上调度
换个浏览器,用IE,就往102上调度了
[root@ubuntu2004 ~]#curl -AIE www.meng.org
web2.meng.org 10.0.0.102
[root@ubuntu2004 ~]#curl -AIE www.meng.org
web2.meng.org 10.0.0.102
可以重定向,调度到百度上去
frontend www.meng.org
bind 192.168.10.100:80
acl acl_user_agent hdr_sub(User-Agent) -i curl wget
redirect prefix http://www.baidu.com if acl_user_agent
default_backend www.meng.com
backend www.meng.org
server web1 10.0.0.101:80 weight 1 check
backend www.meng.com
server web2 10.0.0.102:80 weight 1 check
访问 是curl浏览器就往百度上调度,不是curl浏览器就往102上调度
[root@ubuntu2004 ~]#curl www.meng.org -I
HTTP/1.1 302 Found
content-length: 0
location: http://www.baidu.com/
cache-control: no-cache
[root@ubuntu2004 ~]#curl -AIE www.meng.com -I
HTTP/1.1 200 OK
server: nginx/1.18.0 (Ubuntu)
date: Wed, 26 Oct 2022 14:17:51 GMT
content-type: text/html
content-length: 25
last-modified: Tue, 25 Oct 2022 02:01:43 GMT
etag: "63574387-19"
accept-ranges: bytes
拒绝curl浏览器和wget访问
acl acl_user_agent hdr_sub(User-Agent) -i curl wget
http-request deny if acl_user_agent
ACL示例:基于文件后缀名实现动静分离
frontend www.meng.org
bind 192.168.10.100:80
acl acl_static path_end -i .jpg .jpeg .png .gif .css .js .html
acl acl_php path_end -i .php
use_backend www.meng.org if acl_static
#如果是.jpg .jpeg .png .gif .css .js .html,就往101上调度
default_backend www.meng.com #除去没定义的,就往102上调度
backend www.meng.org
server web1 10.0.0.101:80 weight 1 check
backend www.meng.com
server web2 10.0.0.102:80 weight 1 check
在后端服务器102建立txt文件
[root@ubuntu2004 ~]#echo index.txt > /var/www/html/index.txt
在前端客户端进行访问
[root@ubuntu2004 ~]#curl www.wang.org/index.html
web1.meng.org 10.0.0.101
[root@ubuntu2004 ~]#curl www.wang.org/index.txt
index.txt
ACL示例:预定义ACL使用
官方帮助文档:http://cbonte.github.io/haproxy-dconv/2.1/configuration.html#7.4
拒绝HTTP_1.1 协议,默认就是,不用定义
http-request deny if HTTP_1.1
自定义HAProxy错误页面
基于自定义的错误页面文件
#自定义错误页
errorfile <code> <file>
<code> #HTTP状态代码。支持200、400、403、405、408、425、429、500、502、503、504
<file>#包含完整HTTP响应头的错误页文件的绝对路径。建议后缀为".http",以和一般的html文件相区分
范例:除了.jpg .jpeg .png .gif .css .js .html格式的都往www.meng.com转发。其他的都往com上转发
frontend www.meng.org
bind 192.168.10.100:80
acl acl_static path_end -i .jpg .jpeg .png .gif .css .js .html
use_backend www.meng.org if acl_static
default_backend www.meng.com
backend www.meng.org
server web1 10.0.0.101:80 weight 1 check
backend www.meng.com
server web2 10.0.0.102:80 weight 1 check
重启后前端服务器访问
[root@ubuntu2004 ~]#curl www.meng.org/index.txt
index.txt
如果机器挂了,再去访问,就会出现503错误,然后重定义错误页面
第一步:挂掉后端102机器nginx服务
[root@ubuntu2004 ~]#systemctl stop nginx.service
前端客户端访问生成响应报文
[root@ubuntu2004 ~]#curl www.meng.org/index.txt -I
HTTP/1.1 503 Service Unavailable
content-length: 107
cache-control: no-cache
content-type: text/html
第二步:把响应报文写入到/apps/haproxy/html/503.http,没有创建目录
[root@ubuntu2004 ~]#mkdir /apps/haproxy/html/ -p
[root@ubuntu2004 ~]#vim /apps/haproxy/html/503.http
HTTP/1.1 503 Service Unavailable
content-type: text/html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>报错页面</title>
</head>
<body>
<center><h1>网站维护中......请稍候再试</h1></center>
<center><h2>联系电话: 400-123-4567</h2></center>
<center><h3>503 Service Unavailable</h3></center>
</body>
第三步:把错误加到haproxy主配置文件defaults中,可以加任何错误代码
[root@ubuntu2004 ~]#vim /etc/haproxy/haproxy.cfg
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
errorfile 503 /apps/haproxy/html/503.http
[root@ubuntu2004 ~]#systemctl restart haproxy.service
去windows浏览器访问192.168.10.100可看到自定义错误页面
基于http重定向错误页面
#错误页面重定向 errorloc <code> <url>
#相当于errorloc302 <code> <url>,利用302重定向至指URL
范例
第一步
[root@ubuntu2004 ~]#vim /etc/haproxy/conf.d/test.cfg
frontend www.meng.com
bind 192.168.10.100:80
acl meng.org_domain hdr_dom(host) -i www.meng.org
use_backend www.meng.org if meng.org_domain
default_backend www.meng.com
backend www.meng.org
server web1 10.0.0.101:80 weight 1 check
backend www.meng.com
server web2 10.0.0.102:80 weight 1 check
[root@ubuntu2004 ~]#systemctl restart haproxy.service
第二步
把错误加到haproxy主配置文件defaults中,可以加任何错误代码
[root@ubuntu2004 ~]#vim /etc/haproxy/haproxy.cfg
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
#errorfile 503 /apps/haproxy/html/503.http
errorloc 503 http://www.meng.org/
[root@ubuntu2004 ~]#systemctl restart haproxy.service
前端访问,由于102机器的服务关闭了,访问com会调度重定向到org上
[root@ubuntu2004 ~]#curl www.wang.com -I
HTTP/1.1 302 Found
cache-control: no-cache
content-length: 0
location: http://www.wang.org/
[root@ubuntu2004 ~]#curl www.wang.com -L
web1.meng.org 10.0.0.101