欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 进阶!haproxy高级功能与配置

进阶!haproxy高级功能与配置

2025/2/23 0:33:39 来源:https://blog.csdn.net/qq3178535571/article/details/141114013  浏览:    关键词:进阶!haproxy高级功能与配置

文章目录

  • 前言
  • 基于cookie的会话保持
  • IP透传
    • 四层IP透传
      • 未开启状态
      • 开启透传状态
    • 七层IP透传
  • 自定义错误界面
  • 重定向
  • HAProxy 四层负载之数据库
  • HAProxy https

前言

本文主要介绍HAProxy高级配置及使用案例

文章相关连接如下:

  • 如果想深入了解haproxy算法的相关知识,请点击:超越自我——带你学haproxy算法一遍过!!!
    • 如果想更多了解haproxy的相关知识,请点击:一站式带你深入了解HAProxy

基于cookie的会话保持

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

不支持tcp mode ,使用http mode

语法格式
cookie name rewrite insert prefix ]indirect nocache ]postonly ]preserve ]httponly secure ]domain ]maxidle <idle>]maxlifename:		#cookie的key名称,用于实现持久连接
insert:		#插入新的cookie,默认不插入cookie
indirect:	#如果客户端已经有cookie,则不会再发送cookie信息
nocache:	#当c1ient和hapoxy.之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie,#因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器

配置:

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

在这里插入图片描述
验证:
通过命令行验证:
在这里插入图片描述
注意:需要做window的本地解析:路径C:\Windows\System32\drivers\etc\hosts
在这里插入图片描述

IP透传

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

四层:IP+PORT转发
七层:协议+内容交换

四层IP透传

未开启状态

未开启透传的四层代理时查看/etc/nginx/nginx.conf

[root@webserver1 ~]# vim /etc/nginx/nginx.conf

在这里插入图片描述
看不到访问服务器的源IP地址
100主机访问,在10日志查看

[root@haproxy ~]# curl 172.25.254.100

在这里插入图片描述

[root@webserver1 ~]# tail -n 3 /var/log/nginx/access.log

在这里插入图片描述

开启透传状态

nginx配置:在访问日志中通过变量$proxy._protocol._addr记录透传过来的客户端IP
lproxy._protoco1; #启用此项,将无法直接访问此网站,只能通过四层代理

[root@webserver1 ~]# vim /etc/nginx/nginx.conf
[root@webserver1 ~]# systemctl restart nginx.service # 修改haproxy
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service

在这里插入图片描述
在这里插入图片描述
验证:

在这里插入图片描述

七层IP透传

  • 当haproxy工作在七层的时候,也可以透传客户端真实IP至后端服务器
  • send-proxy 设置成代理
[root@webserver1 ~]# vim /etc/nginx/nginx.conf
[root@webserver1 ~]# systemctl restart nginx.service # 修改haproxy
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service

在这里插入图片描述
在这里插入图片描述

修改nginx
在这里插入图片描述
结果验证:
在这里插入图片描述

自定义错误界面

[root@haproxy ~]# mkdir /etc/haproxy/errorpage -p
[root@haproxy ~]# vim /etc/haproxy/errorpage/badpage.http
[root@haproxy ~]# systemctl restart httpd

修改自定义日志的内容

[root@haproxy ~]# vim /etc/haproxy/errorpage/badpage.http

在这里插入图片描述
修改haproxy自定日志路径
在这里插入图片描述
验证前需关闭两台服务器的服务

[root@webserver1 ~]# systemctl stop nginx.service
[root@webserver22 ~]# systemctl stop nginx.service

验证:
在这里插入图片描述

重定向

接着自定义日志的环境继续修改实验

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

在这里插入图片描述
验证:
在这里插入图片描述
在这里插入图片描述

HAProxy 四层负载之数据库

应用场景:针对除HTTP以外的TCP协议应用服务访问 1.MySQL 2.Redis 3.Memcache 4.RabbitMQ

重新开启服务

[root@webserver1 ~]# systemctl restart nginx.service
[root@webserver22 ~]# systemctl restart nginx.service

下载数据库客户端

[root@haproxy ~]# dnf install mariadb -y

服务器下载数据库服务端

[root@webserver1 ~]# dnf install mariadb-server -y
[root@webserver22 ~]# dnf install mariadb-server -y

修改haproxy

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

在这里插入图片描述

更改数据库id,方便实验结果显示
在这里插入图片描述
在这里插入图片描述

在10服务器上的数据库操作

查看id是否修改正确
在这里插入图片描述
添加用户lee,并赋予远程登陆权限,设置密码为lee
在这里插入图片描述
给用户lee赋予访问所以数据库和所以表的权限,
在这里插入图片描述

在20服务器上的数据库操作

查看id是否修改正确
在这里插入图片描述

添加用户lee,并赋予远程登陆权限,设置密码为lee
在这里插入图片描述
给用户lee赋予访问所以数据库和所以表的权限,
在这里插入图片描述

HAProxy https

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

#配置HAProxy支持https协议,支持ssl会话;
bind *:443 ssl crt /PATH/TO/SOME_PEM_FILE
#指令 crt 后证书文件为PEM格式,需要同时包含证书和所有私钥
cat demo.key demo.crt > demo.pem
#把80端口的请求重向定443
bind *:80
redirect scheme https if !{ ssl_fc }

制作证书:

[root@haproxy ~]# mkdir /etc/haproxy/certs/
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/luo.org.key -x509 -days 365 -out /etc/haproxy/certs/luo.org.crt
# 将证书和钥匙放入PEM格式的文件里
[root@haproxy certs]# cat luo.org.crt luo.org.key > wang.org.pem

全站加密:redirect scheme https if !{ ssl_fc }加上这条命令就会自动将80端口的请求重定向到443
在这里插入图片描述

文章相关连接如下:

  • 如果想深入了解haproxy算法的相关知识,请点击:超越自我——带你学haproxy算法一遍过!!!
    • 如果想更多了解haproxy的相关知识,请点击:一站式带你深入了解HAProxy

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com