haproxy+nginx网站架构,实现负载均衡实验笔记

前提准备:

  1. 两台nginx,一台haproxy
  2. nginx1:192.168.180.120
  3. nginx2:192.168.180.130,NFS
  4. haproxy:192.168.180.110

nginx(两台nginx的操作是一样的):

  1. 安装nginx
bash 复制代码
#先安装这个
yum install -y epel-release
yum install -y nginx

2.分别编写网页

bash 复制代码
echo "server1 192.168.180.120" >/usr/share/nginx/html/index.html
echo "server2 192.168.180.130" >/usr/share/nginx/html/index.html
  1. 开启nginx
bash 复制代码
systemctl start nginx
  1. 关闭防火墙
bash 复制代码
systemctl stop firewalld
setenforce 0

haproxy:

  1. 安装haproxy(通过源码包安装)
bash 复制代码
yum install -y gcc gcc-c++ make lrzsz
tar zxf haproxy-2.9.9.tar.gz
cd haproxy-2.9.9
make TARGET=linux-glibc && make install
  1. 移动主配置文件
bash 复制代码
mkdir /etc/haproxy
cp addons/ot/test/sa/haproxy.cfg /etc/haproxy/
  1. 修改主配置文件
bash 复制代码
vim /etc/haproxy/haproxy.cfg
#将端口改为8080

#注释

#添加内容
frontend http_front
        bind *:80
                 default_backend servers-backend

backend servers-backend
    mode http
    server inst1 192.168.180.120:80 check inter 80 fall 3
    server inst2 192.168.180.130:80 check inter 80 fall 3 backup
  1. 创建自启动脚本
bash 复制代码
cp ~/haproxy-2.9.9/examples/haproxy.init /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
chmod +x /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
/etc/init.d/haproxy start
  1. 关闭防火墙
bash 复制代码
systemctl stop firewalld
setenforce 0

NFS-192.168.180.130:

  1. 在nginx上均安装
bash 复制代码
yum install -y nfs-utils rpcbind
  1. 创建共享目录
bash 复制代码
mkdir -p /opt/wwwroot
vim /etc/exports
/opt/wwwroot    192.168.180.0/24(rw,sync,no_root_squash)
  1. 分别启动
bash 复制代码
systemctl start nfs
systemctl start rpcbind
  1. 查看NFS共享了什么目录
bash 复制代码
showmount -e 192.168.180.130
  1. nginx均挂载NFS共享目录
bash 复制代码
mount 192.168.180.130:/opt/wwwroot /usr/share/nginx/html/
  1. 创建测试页面
bash 复制代码
echo "nginx-NFS" > /usr/share/nginx/html/index.html
  1. 访问测试
相关推荐
小程故事多_803 分钟前
DeepSeek-V4技术报告全解读 从架构到Infra的全栈重构之路
人工智能·重构·架构·智能体
掘根13 分钟前
【微服务即时通讯】客户端数据中心
qt·微服务·架构
heimeiyingwang18 分钟前
【架构实战】观察者模式在分布式系统中的应用
观察者模式·架构·wpf
skilllite作者22 分钟前
从“记忆”到“项目 Wiki”:我在 SkillLite 里实现了一套 Markdown-only LLM Wiki 自动维护机制
开发语言·jvm·人工智能·后端·架构·rust
littleM41 分钟前
深度拆解 HermesAgent(六):研究功能与测试体系
开发语言·人工智能·python·架构·ai编程
车载诊断技术42 分钟前
在工作中如何保持奋斗的动力?
网络·架构·汽车·电子电气架构·ecu 诊断 diag
奇逍科技圈44 分钟前
开源架构 + BC 一体化:批发零售企业订货系统源码重构增长新路径
后端·架构·开源
littleM1 小时前
深度拆解 HermesAgent(七):CLI、安全与部署实践指南
人工智能·安全·架构
面汤放盐1 小时前
架构对比:单体架构与微服务架构
微服务·云原生·架构
iwS2o90XT1 小时前
微服务架构设计:Spring Cloud Gateway与Nacos集成
微服务·云原生·架构