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. 访问测试
相关推荐
艾醒(AiXing-w)1 小时前
大模型面试题剖析:深入解析 Transformer 与 MoE 架构
深度学习·架构·transformer
hzulwy1 小时前
微服务注册与监听
微服务·云原生·架构·go
爱喝白开水a15 小时前
2025时序数据库选型,从架构基因到AI赋能来解析
开发语言·数据库·人工智能·架构·langchain·transformer·时序数据库
-dcr15 小时前
22.Nginx 服务器 LNMP项目
运维·服务器·nginx·php·lnmp
東雪蓮☆15 小时前
Docker 搭建 Nginx 并启用 HTTPS 具体部署流程
运维·nginx·docker
zz-zjx17 小时前
Nginx 生产级知识架构树(按流量路径 + 运维维度组织)含生产常见错误
运维·nginx·架构
小任今晚几点睡19 小时前
LVS深度解析:从原理到实战的负载均衡完全指南
负载均衡·lvs
深蓝电商API19 小时前
Scrapy 框架深度解析:架构、组件与工作流程
scrapy·架构
58沈剑19 小时前
1W+属性,每秒10W+吞吐,100Y+数据,架构如何设计?(第101讲,万字收藏)
大数据·架构
Li zlun21 小时前
MySQL 管理与配置详解:从安装到架构解析
数据库·mysql·架构