haproxy高级功能及案例配置(cookie会话、IP透传、acl等)

一、基于cookie的会话保持

cookie value: 为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source 地址hash 调度算法对客户端的粒度更精准,但同时也加大了haproxy负载,目前此模式使用较少,已经被session共享服务器代替

  1. 配置选项:

    • name: #cookie的key名称,用于实现持久连接#插入新的cookie,默认不插入cookie

    • insert: #如果客户端已经有cookie,则不会再发送cookie信息

    • indirect: #当client和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie

    • nocache: #因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器

案例配置:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

    balance  roundrobin
    cookie WEBCOOKIE insert nocache indirect
    server  web3 172.25.254.10:80  cookie txy3  check inter 2 fall 3 rise 5 weight 1
    server  web4 172.25.254.20:80  cookie txy4   check inter 2 fall 3 rise 5 weight 1

# 验证:客户机上测试  网络上测试

#curl 访问指定cookoie
[root@client ~]# curl -b WEBCOOKIE=txy3 172.25.254.114
websever3 - 172.25.254.10
[root@client ~]# curl -b WEBCOOKIE=txy4 172.25.254.114
websever4 - 172.25.254.20
[root@client ~]#

 

 

二、状态页

1. 通过web界面,显示当前HAProxy的运行状态

stats enable   #基于默认的参数启用stats page

stats hide-version  #将状态页中haproxy版本隐藏
 
stats auth <user>:<passwd>  #认证时的账号和密码,可定义多个用户,每行指定一个用户#默认:no authentication
stats refresh <delay>    #设定自动刷新时间间隔,默认不自动刷新
stats uri <prefix>     #自定义stats page uri,默认值:/haproxy?stats
stats admin{if|unless }<cond> 
#启用stats page中的管理功能

 # 配置
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

 

三、IP透传

web服务器中需要记录客户端的真实IP地址,用于做访问统计、安全防护、行为分析、区域排行等场景。

  • 没做之前的环境:

 

  •  做之前的环境 开启forwardfor 

 

 注意apache没有

1. 四层透传(nginx)

1.1查看nginx配置

 1.2 相关配置

  • #查看访问后的haproxy的日志,没有传输过来的ip地址
    [root@websever4 ~]# tail -3 /var/log/nginx/access.log

  • #开启四层透传
    # 在访问日志中通过变量$proxy_protoco1_addr 记录透传过来的客户端IP
    
    [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
     改为 tcp

 

  • 此时看不到传输过来的ip地址,设定代理

  •  测试,此时是没有获取
  •  加参数
[root@websever3 ~]# vim /etc/nginx/nginx.conf
[root@websever3 ~]#  systemctl restart  nginx.service
[root@websever3 ~]# tail -3 /var/log/nginx/access.log

2.七层透传

1.1 基础配置检查还原  (nginx下)

注意 option forwardfor       except 127.0.0.0/8  的状态  不可以注释

 重启在client上测试

 看日志,得到ip

 1.2 apache
 相关配置:
#停止nginx服务
[root@websever3 ~]# systemctl disable nginx.service
Removed "/etc/systemd/system/multi-user.target.wants/nginx.service".
[root@websever3 ~]# systemctl stop nginx.service

#装httpd服务
[root@websever3 ~]# yum install httpd -y

[root@websever3 ~]# echo this is server1 - 172.25.254.10 > /var/www/html/index.html
[root@websever3 ~]# systemctl start httpd
[root@websever3 ~]#
[root@websever3 ~]#
客户端测试

 改配置文件

 测试:看客户端ip

四、ACL(访可控制外表)

1.1 定义:

        是一种基于包过滤的访问控制技术,它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配)即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,比如允许其通过或丢弃。

 1.2配置选项

#用ac1来定义或声明一个ac1
acl   <aclname>  <criterion>   [flags]    [operator]       [<value>]
  
acl     名称        匹配规范     匹配模式   具体操作符   
操作对象类型

 1.3 ACL-Name  名称

acl        image_service         hdr_dom(host)        -i        img.magedu.com

#ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大小写,比如:my_ac1和My_AC1就是两个完全不同的ac15.8.1.2 ACL-criterion

1.4 匹配规范

 定义ACL匹配规范,即:判断条件

 hdr string                提取在一个HTTP请求报文的首部
hdr([<name> [,<occ>]])    完全匹配字符串,header的指定信息,<occ>表示在多值中使用的值的出现次数
hdr_beg([<name> [,<occ>]]) 前缀匹配,header中指定匹配内容的begin
hdr_end[<name> [,<occ>]]) 后缀匹配,header中指定匹配内容end
hdr_dom([<name> [,<occ>]]) 域匹配,header中的domainname(host)
hdr_dir([<name> [,<0CC>]]) 路径匹配,header的uri路径
hdr_len([<name> [,<0CC>]]) 长度匹配,header的长度匹配
hdr_reg([<name> [,<occ>]]) 正则表达式匹配,自定义表达式(regex)模糊匹配
hdr_sub([<name> [,<occ>]]) 子串匹配,header中的uri模糊匹配 模糊匹配
 
#示例:
hdr(<string>)    用于测试请求头部首部指定内容
hdr_dom(host)    请求的host名称,如www.zx.org
hdr_beg(host)    请求的host开头,如www. img. video. download. ftp.
hdr_end(host)    请求的host结尾,如.com. net .cn

1.5 相关例子

配置acl

 此时访问不了,做域名解析

 然后在Windows中做本地解析,  C:\Windows\System32\drivers\etc

 

 

测试


[root@client ~]# curl www.txy.org
websever4 - 172.25.254.20

以为结尾   hdr_end

 

 

 自定义错误页面

 现在我们把websever3和websever4停用,然后客户主机去访问haproxy主机,就会出现503错误

 

 访问出问题

 改配置

[root@haproxy ~]#mkdir /etc/haproxy/errorpage -p
 
[root@haproxy ~]#vim /etc/haproxy/errorpage/503.http

#添加以下自定义内容
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html;charset=UTF-8
 
<html><body><h1>什么动物生气最安静</h1>
大猩猩!!
</body></html>

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# restart haproxy.service

 

 haproxy四层负载

针对除http以外的tcp协议应用服务访问的应用场景

例子
环境

haproxy主机、websrver3、websever4主机都安装mariadb数据库
 

[root@websever3 ~]# yum install mariadb-server -y
[root@websever4 ~]# yum install mariadb-server -y
[root@haproxy ~]# yum install mariadb-server -y

websrver3、websever4主机
#为了区分效果,把主机的server_id分别改为1和2
[root@websever3 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[root@websever3 ~]# systemctl start mariadb
 
[root@websever4 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[root@websever4 ~]# systemctl start mariadb

 

 

 

  •  

    然后为了让haproxy主机远程登录主机的数据库,我们需要再主机上做以下操作

    主机都要创建用户,都是一样的操作

 

远程访问

https加密访问

前提是需要打开web服务

haproxy可以实现https的证书安全,从用户到haproxy为https,从haproxy到后端服务器用http通信但基于性能考虑,生产中证书都是在后端服务器比如nginx上实现

#建立一个目录,用来放密钥
[root@haproxy ~]# mkdir -p /etc/haproxy/certs
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/txy.org.key -x509 -days 365 -out /etc/haproxy/certs/txy.org.crt

 [root@haproxy ~]# ls /etc/haproxy/certs/

 

  •  打开haproxy配置文件

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

 

 [root@haproxy ~]# netstat -antlupe | grep haproxy

 

就不用手动加密了,它直接跳转到https,输入172.25.250.114,自动加密为https://172.25.250.114

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值