欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > haproxy+nginx网站架构,实现负载均衡实验笔记

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

2025/4/25 23:05:42 来源:https://blog.csdn.net/m0_68472908/article/details/145182111  浏览:    关键词: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

#先安装这个
yum install -y epel-release
yum install -y nginx

2. 分别编写网页

echo "server1 192.168.180.120" >/usr/share/nginx/html/index.html
echo "server2 192.168.180.130" >/usr/share/nginx/html/index.html

3. 开启nginx

systemctl start nginx

4. 关闭防火墙

systemctl stop firewalld
setenforce 0

haproxy:

1. 安装haproxy(通过源码包安装)

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

2. 移动主配置文件

mkdir /etc/haproxy
cp addons/ot/test/sa/haproxy.cfg /etc/haproxy/

3. 修改主配置文件

vim /etc/haproxy/haproxy.cfg
#将端口改为8080#注释#添加内容
frontend http_frontbind *:80default_backend servers-backendbackend servers-backendmode httpserver inst1 192.168.180.120:80 check inter 80 fall 3server inst2 192.168.180.130:80 check inter 80 fall 3 backup

4. 创建自启动脚本

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

5. 关闭防火墙

systemctl stop firewalld
setenforce 0

NFS-192.168.180.130:

1. 在nginx上均安装

yum install -y nfs-utils rpcbind

2. 创建共享目录

mkdir -p /opt/wwwroot
vim /etc/exports
/opt/wwwroot    192.168.180.0/24(rw,sync,no_root_squash)

3. 分别启动

systemctl start nfs
systemctl start rpcbind

4. 查看NFS共享了什么目录

showmount -e 192.168.180.130

5. nginx均挂载NFS共享目录

mount 192.168.180.130:/opt/wwwroot /usr/share/nginx/html/

6. 创建测试页面

echo "nginx-NFS" > /usr/share/nginx/html/index.html

7. 访问测试

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词