HAproxy 高级用法

HAproxy 高级用法

基于cookie的会话保持

cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,但同时也加大了haproxy负载,目前此模式使用较少,已经被session共享服务器代替
注意:不支持mode tcp,使用mode http

cookie [option]
name:     #cookie的key名称,用于实现持久连接
insert:   #插入新的cookie,默认不插入cookie
indirect: #如果客户端已经有cookie,则不会再发送cookie信息
nocache:  #当client和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie

#配置
listen web_80
  bind 172.16.0.8:80
  bind 10.0.0.8:80
  cookie webserver insert nocache 
  log global
  server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
  server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

curl -i 172.16.0.8
  set-cookie: webserver=web1; path=/
curl -b webserver=web1 172.16.0.8
  10.0.0.7

HAproxy状态页

配置:
stats enable            #基于默认的参数启用stats page
stats hide-version      #将状态页中haproxy版本隐藏
stats refresh 30s       #设定自动刷新时间间隔,默认不自动刷新
stats uri /status       #自定义stats uri路径 
stats realm             #账户认证时的提示信息 
stats auth admin:123456 #认证时的账号和密码,可有多行用户
stats admin if TRUE     #启用stats page中的管理功能,不建议开启
#例:
 listen stats
    bind 10.0.0.8:9999
    stats enable
    log global
    stats uri  /haproxy_status
    stats auth  admin:123456
    stats admin if TRUE
    
#通过curl 命令对haproxy的状态页的访问实现健康检查 
curl -I http://admin:123456@10.0.0.8:9999/status
  HTTP/1.1 200 OK
  content-type: text/html

七层IP透传

#IP透传:
web服务器中需要记录客户端的真实IP地址,用于做访问统计、安全防护等场景

#Haproxy配置
defaults
 option forwardfor header x-client   #首部字段为  x-client:10.0.0.10
 mode http
 
#apache服务器配置 
LogFormat "%{X-client}i %a %l %u %t \"%r\" %>s %b \"%{Referer}i\"" combined
CustomLog "logs/access_log" combined
tail -f /var/log/httpd/access_log
172.16.0.6 10.0.0.8 - - [09/Apr/2020:23:29:20 +0800] "GET / HTTP/1.1" 200 9 "-"

#Nginx服务器配置
$proxy_add_x_forwarded_for:包括客户端IP和中间经过的所有代理的IP
$http_x_forwarded_For:只有客户端IP
log_format main  '"$proxy_add_x_forwarded_for"'
                 '$status $body_bytes_sent "$http_referer"'
                 '"$http_user_agent" $http_x_forwarded_For';
tail /var/log/nginx/access.log
"172.16.0.200, 10.0.0.100" 10.0.0.100 - - [09/Apr/2020:19:10:16 +0800] "GET / HTTP/1.1" 200 4057 "-" "curl/7.19.7 " "172.16.0.200"

报文修改

   在http模式下,基于实际需求修改客户端的请求报文与响应报文,通过http-request和http-response在请求报文添加或删除字段
示例:
http-request add-header Date %T   #请求报文添加date字段
http-request del-header Date      #请求报文删除date字段
http-response add-header Via haproxy  #响应报文添加Via字段
http-response del-header server       #响应报文删除server字段

listen web_80
 bind 172.16.0.8:80
 http-request add-header Date %T
 http-response del-header server

日志管理

log global      #开启日志功能,默认只会在记录下面格式的日志
option httplog  #开启记录httplog日志格式选项
capture request header Host len  256      #记录请求报文中host的256字节
capture request header X-clinet len 15    #记录请求报文中x-clinet的15字节

压缩功能

#对响应给客户端的报文进行压缩,以节省网络带宽
#注:会占用部分CPU性能,建议在后端服务器开启压缩功能,而非在HAProxy上开启压缩

compression algo gzip   #启用http协议中的压缩机制gzip
compression type <mime type>  #要压缩的文件类型
#示例:
listen web_http
 compression algo gzip
 compression type text/html text/csstext/plain

Web服务器状态监测

#web服务器状态七层检查会生成大量日志
#使用HEAD方法节省流量带宽
option httpchk HEAD /  #默认HTTP/1.0
option httpchk HEAD / HTTP/1.1\r\nHost:\ 10.0.0.8  #HTTP/1.1 必须带host首部  
#例:
 listen 
 option httpchk HEAD / HTTP/1.1\r\nHost:\ 10.0.0.8

 #后端服务器查看访问日志
 tail -f /var/log/httpd/access_log 
- 10.0.0.8 - - [10/Apr/2020:14:07:47 +0800] "HEAD / HTTP/1.1" 200 - "-"
- 10.0.0.8 - - [10/Apr/2020:14:07:50 +0800] "HEAD / HTTP/1.1" 200 - "-"
- 10.0.0.8 - - [10/Apr/2020:14:07:53 +0800] "HEAD / HTTP/1.1" 200 - "-" ...

ACL控制

访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定的条件对经过服务器传输的数据包进行过滤,并做出控制

#匹配规范
hdr_beg(host) www.    #前缀匹配,header中host字段以www.开头
hdr_end(host) .com    #后缀匹配,header中host字段以.com结尾
hdr_dom(host) www.ssy.org  #域匹配,header中host字段为www.ssy.org
hdr_dir(host)  #路径匹配,header的uri路径
hdr_sub(User-agent)   #子串匹配,包含,header中的模糊匹配

#提取请求的URL路径,该路径从第一个斜杠开始,并在问号之前结束(无主机部分)
<scheme>://<user>:<password>@<host>:<port>#/<path>;<params>#?<query>#<frag>
path_beg    #请求的URL开头,如/static、/images、/img、/css
path_end    #请求的URL中资源的结尾,如 .gif .png .css .js .jpg .jpeg

dst   #目标IP   
dst_port #目标PORT
src   #源IP
src_port #源PORT
 
#acl匹配模式
   -i 不区分大小写
   -n 不做DNS解析
   -u 禁止acl重名,否则多个同名ACL匹配或关系

#acl调用方式
  与:默认使用
  或:使用“or” 
  取反:使用 "!" 

ACL基于域名匹配

frontend web_80
 mode http
 bind 172.16.0.8:80
 log global
#acl
 acl pc.domain hdr_dom(host) www.ssy.org
 acl m.domain hdr_dom(host) m.ssy.org 
 use_backend pc_host if pc.domain
 use_backend m_host if m.domain
 default_backend pc_host   #默认调度

backend pc_host
  server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
backend m_host
  server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

cat /etc/hosts
172.16.0.8 m.ssy.org www.ssy.org
curl www.ssy.org
10.0.0.7 Apache
curl m.ssy.org
10.0.0.17 Nginx 

ACL基于源IP地址或子网匹配

frontend web_80
 mode http
 bind 172.16.0.8:80
 log global
 acl pc.domain hdr_dom(host) www.ssy.org
 acl m.domain hdr_dom(host) m.ssy.org 
 acl ip_host src 172.16.0.0/16 10.0.0.6  #匹配源IP来自172.16.0.0/24网段
 use_backend pc_host if ip_host  #调度到后端pc_host,此优先级比下面的高
 use_backend pc_host if pc.domain
 use_backend m_host if m.domain
 default_backend pc_host

backend pc_host
  server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
backend m_host
  server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

curl 172.16.0.8  #连续三次curl都是后端10.0.0.7
10.0.0.7 Apache

ACl基于浏览器类型匹配

frontend web_80
 mode http
 bind 172.16.0.8:80
 log global
 acl use_agent hdr_sub(User-agent) -i curl wget
 acl use_agent_ab hdr_sub(User-agent) -i Apachebench  
 http-request deny if use_agent_ab           #使用ab命令就拒绝 
 redirect prefix www.baidu.com if use_agent  #使用curl或wget就302重定向
 default_backend pc_host     

backend pc_host
  server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
backend m_host
  server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

curl  -I  172.16.0.8
   HTTP/1.1 302 Found
   content-length: 0
   location: www.baidu.com/ 
ab  -c1  -n1  http://172.16.0.8/ 
   Non-2xx responses:      1
curl -A apachebench 172.16.0.8
   <html><body><h1>403 Forbidden</h1>

ACL-基于后缀名实现动静分离

frontend web_80
 mode http
 bind 172.16.0.8:80
 log global
 acl static path_end -i .html .css .jpg .png
 acl php path_end -i .php
 use_backend pc_host if php   
 use_backend m_host if static
 default_backend pc_host
#也可以使用此方式
# acl static path_beg  -i /static /images /javascript
# use_backend m_host if static
# default_backend pc_host

backend pc_host
  server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
backend m_host
  server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

curl  172.16.0.8/yyy.html
   static
curl  172.16.0.8/yyy.php
   php

自定义HAproxy错误页面

#自定义错误页
支持状态码 400, 403, 405, 408, 425, 429, 500, 502,503,504
#示例:
 defaults
 errorfile 403 /etc/haproxy/errorfiles/403forbid.http
 errorfile 503 /etc/haproxy/errorfiles/503sorry.http
 
vim /apps/haproxy/html/503.http    #http后缀文件需要有报文头部信息
HTTP/1.1 503 Service Unavailable
Content-Type:text/html;charset=utf-8
.... #以下为错误页面代码
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值