使用HAProxy实现动静分离

解读:

  • 使用haproxy的ACL访问控制实现调度后端服务的动静分离
  • 每个RS上都部署相同业务实现负载均衡

实验环境

主机名 IP OS
haproxy eth0:172.25.254.100 Rocky Linux9.6
rs1 eth0:172.25.254.10 Rocky Linux9.6
rs2 eth0:172.25.254.20 Rocky Linux8.6

流程图

网络配置

网络配置使用自定义脚本来快速设置IP网关DNS主机名等

bash 复制代码
[root@haproxy ~]# vmset.sh eth0 172.25.254.100 haproxy
[root@rs1 ~]# vmset.sh eth0 172.25.254.10 rs1
[root@rs2 ~]# vmset.sh eth0 172.25.254.20 rs2

业务部署

RS部署web服务

bash 复制代码
[root@rs1 ~]# dnf install nginx -y
[root@rs2 ~]# dnf install nginx -y
[root@rs1 ~]# systemctl enable --now nginx
[root@rs2 ~]# systemctl enable --now nginx

[root@rs1 ~]# echo rs1 - webserver1 > /usr/share/nginx/html/index.html
[root@rs1 ~]# curl localhost
rs1 - webserver1

[root@rs2 ~]# echo rs2 - webserver2 > /usr/share/nginx/html/index.html
[root@rs2 ~]# curl localhost
rs2 - webserver2

RS2部署php

bash 复制代码
[root@rs2 ~]# dnf install php -y
[root@rs2 ~]# systemctl enable --now php-fpm

nginx添加php解析

bash 复制代码
[root@rs1+2 ~]# vim /etc/nginx/nginx.conf
server {
......
	location ~ \.php$ {
            root /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  				      
            $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
[root@rs1+2 ~]# systemctl restart nginx.service

测试是否能访问

haproxy部署

安装haproxy

bash 复制代码
[root@haproxy ~]# dnf install haproxy -y

编写haproxy规则

bash 复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
......
frontend webcluster
    bind    172.25.254.100:80

    acl url_static path_end -i .jpg .png .css .js .html
    acl url_php path_end -i .php

    use_backend webservera if url_static
    use_backend webserverb if url_php

backend webservera
    server web1 172.25.254.10:80 check
    server web2 172.25.254.20:80 check
backend webserverb
    server web1 172.25.254.10:80 check
    server web2 172.25.254.20:80 check

[root@haproxy ~]# systemctl enable --now haproxy.service

测试

k

server web2 172.25.254.20:80 check

root@haproxy \~# systemctl enable --now haproxy.service

复制代码
**测试**

![image-20260130183554123](https://i-blog.csdnimg.cn/img_convert/b9bbf674a271b53e45c548118a636bb0.png)

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/deea89651ade40f6b9dc83394f5c793c.png#pic_center)
相关推荐
长江&后浪8 分钟前
华为wlan实验-三层旁挂
运维·服务器·网络·华为
梓沂15 分钟前
CentOS 7.9 静默安装 Oracle 11gR2 企业版全记录
linux·oracle·centos
蜀道山老天师19 分钟前
Zabbix监控Docker容器应用实践完整指南
linux·运维·docker·容器·zabbix
hweiyu0019 分钟前
Linux命令:info
linux·运维
xiaoxiangsiyan23 分钟前
机房交换机交换技术与服务器链路聚合深度指南
运维·服务器·网络·学习·自动化·bond
万山寒1 小时前
python依赖包导出离线安装到没有外网的服务器
服务器·开发语言·python
wsad05321 小时前
CentOS Stream 10 Docker 容器无法访问外网:xt_addrtype 内核模块缺失解决方法
linux·网络·docker·云原生·eureka·centos
x17388062731 小时前
Linux
linux
加油码1 小时前
探秘 volatile关键字:编译器的过度优化
linux·bug·信号处理
dongfangkandian1 小时前
从浪涌失控到端口级调度:2026年AI服务器老化测试专用PDU的选型逻辑
服务器