haproxy七层代理

haproxy基本部署

实验环境需要三台主机

haproxy:172.25.254.100

web1:172.25.254.10

web2:172.25.254.20

webserver

两台webserver进行以下配置:

dnf install nginx -y

echo webserver1 -172.25.254.10 > /usr/share/nginx/html/index.html

systemctl enable --now nginx

haproxy

下载haproxy包:dnf install haproxy -y

打开配置文件:vim /etc/haproxy/haproxy.cfg

启动haproxy服务:

systemctl enable haproxy   

systemctl start haproxy

访问

haproxy全局配置

global进程

设置global进程,将进程绑定cpu

打开配置文件 :vim /etc/haproxy/haproxy.cfg

启动haproxy服务:systemctl enable --now haproxy

查看进程:pstree -p | grep haproxy

定向haproxy日志

编辑文件 vim /etc/rsyslog.conf

haproxy代理参数 

haproxy主机下载httpd服务

编辑配置文件 vim /etc/httpd/conf/httpd.conf

作为backup --sorryserver的端口

echo sorry > /var/www/html/index.html

systemctl enable --now httpd

编辑配置文件 vim /etc/haproxy/haproxy.cfg

realserver

 重定向

 重启服务 systemctl restart haproxy

 haproxy的热处理

socat工具

dnf install socat -y

加权限

vim /etc/haproxy/haproxy.cfg

重启服务 systemctl restart haproxy

查看集群状态

echo "show servers state" | socat stdio /var/lib/haproxy/stats

查看/设置 权重

haproxy多进程热处理

查看  ll /var/lib/haproxy/

 haproxy算法

首先关闭多进程:vim /etc/haproxy/haproxy.cfg

静态

static-rr

first

 在多态主机中执行死循环测试效果

动态

roundrobin

leastconn

动静结合

source

uri

uri_param

hdr

高级功能及配置

启用状态页

浏览器访问:

基于cookie的会话保持

配置
 

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

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    cookie WEBCOOKIE insert nocache indirect
 server webserver1 172.25.254.10:80 cookie web1 weight 1 check inter 3s fall 3 rise 5  
 server webserver2 172.25.254.20:80 cookie web2 weight 1 check inter 3s fall 3 rise 5 

测试

IP透传

haproxy配置:

webserver1

webserver2

测试:

ACL

acl的常用参数

haproxy配置:

本地主机进行解析 C:\Windows\System32\drivers\etc 编辑hosts文件

测试:

以org为结尾

acl test hdr_end(host) -i .org

以bbs为开头

acl test hdr_beg(host) -i bbs

测试

acl扩展

frontend webcluster
     bind *:80
     mode http
     acl test hdr_dom(host)  -i  bbs
     #acl test base_sub -m sub wxq
     #acl test base_reg -i wxq/$
     acl test path_sub -m sub wxq
//前面是命令,后面是对acl调用
     #use_backend webcluster-host if test
     #use_backend webcluster-host if ! test
     #use_backend webcluster-host if test domain
     use_backend webcluster-host if test || domain
     default_backend default-host

backend webcluster-host
     mode http
     server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
     mode http
     server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

基于源ip访问

测试

基于文件后缀名实现动静分离

webserver:

yum install php

echo php-172.25.254.10 > /var/www/html/index.php

编辑haproxy

测试

匹配访问路径实现动静分离

 [root@webserver1 ~]# mkdir -p /var/www/html/php

[root@webserver1 ~]# mv /var/www/html/index.php

基于自定义的错误页面文件

编辑503.http文件

在haproxy主机下  vim /etc/haproxy/haproxy.cfg 

defaults下添加 errorfile 503 /etc/haproxy/errorpage/503.http

测试

 错误页面重定向

在defaults下添加  errorloc 503 百度一下,你就知道

浏览器访问172.25.254.100 就变成了访问百度

四层负载实例

以mysql为例

webserver下载:

dnf install mariadb-server -y

vim /etc/my.cnf.d/mariadb-server.cnf

web1:

web2:

启动mysql  systemctl start mariadb

授权

 

在haproxy主机编辑  vim /etc/haproxy/haproxy.cfg 

测试

haproxy的https

证书制作:

[root@haproxy ~]# mkdir -p  /etc/haproxy/certs
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/timingwxq.org.key -x509 -days 365 -out /etc/haproxy/certs/timingwxq.org.crt

[root@haproxy ~]# cat /etc/haproxy/certs/timingwxq.org.key /etc/haproxy/certs/timingwxq.org.crt > /etc/haproxy/certs/timingwxq.pem
[root@haproxy ~]# cat /etc/haproxy/certs/timingwxq.pem 

编辑文件 vim /etc/haproxy/haproxy.cfg 

进行全站加密

 测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值