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. 访问测试
相关推荐
执卿11 分钟前
使用Hilt重构项目
架构
SimonKing13 分钟前
吊打面试官系列:Spring为什么不推荐使用字段依赖注入?
java·后端·架构
season_zhu1 小时前
Swift:优雅又强大的语法糖——Then库
ios·架构·swift
hstar95272 小时前
二、即时通讯系统设计经验
java·架构
江梦寻3 小时前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
打码人的日常分享11 小时前
物联网智慧医院建设方案(PPT)
大数据·物联网·架构·流程图·智慧城市·制造
白水baishui11 小时前
搭建强化推荐的决策服务架构
架构·推荐系统·强化学习·决策服务·服务架构
何双新11 小时前
第23讲、Odoo18 邮件系统整体架构
ai·架构
雪碧聊技术11 小时前
将单体架构项目拆分成微服务时的两种工程结构
微服务·架构·module·project·工程结构
从零开始学习人工智能12 小时前
Doris 数据库深度解析:架构、原理与实战应用
数据库·架构