Nginx负载均衡
一.前言
主要讲一下规划: (系统为Centos7)
1.nginx服务器:配置网页访问
2.辅助机器:配置相同网页或者不同网页进行访问
二. Linux虚拟机配置
Hostname | IP |
---|---|
nginx | 172.129.22.10 |
node1 | 172.129.22.20 |
node2 | 172.129.22.30 |
三. 基础环境
1.基础环境
# 防火墙,selinux等一些基础配置
# 三台节点配置
1.配置源
(我这里使用的是阿里云网络源)
mv /etc/yum.repos.d/* /media/ # 清理原本的源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release # nginx软件在epel源中
2.关闭防火墙和selinux
systemctl disable firewalld --now
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disbaled/g' /etc/selinux/config
3.配置无密钥连接
# nginx节点
echo -e "172.129.22.10 nginx\n172.129.22.20 node1\n172.129.22.30 node2" >> /etc/hosts
ssh-keygen (全部默认回车即可)
ssh-copy-id node1
ssh-copy-id node2
2.配置网页
# 配置静态网页
1.配置不同的静态网页
# node1节点
rm -rf /usr/share/nginx/html/* # 清理原本的网页
echo "I am node1." >> /usr/share/nginx/html/index.html
# node2节点
rm -rf /usr/share/nginx/html/* # 清理原本的网页
echo "I am node2." >> /usr/share/nginx/html/index.html
2.开启服务
systemctl enable nginx --now
3.配置权重
# 配置文件
# nginx节点
1.配置新文件
vi /etc/nginx/conf.d/test.conf # 这个文件名可自己取
'''
server { # 服务器配置
listen 80; # 监听端口
location / { # 配置根目录网页
proxy_pass http://abc; # 配置负载均衡(这里的abc和下面abc相对应,两者需相同)
}
}
upstream abc {
server node1:80 weight=1; # 配置轮询和权重,权重越大则接受访问量越多
server node2:80 weight=1;
}
:wq
'''
vi /etc/nginx/nginx.conf
'''
...
include /etc/nginx/conf.d/*.conf; # 将36行之后的配置删除
...
:wq
'''
2.开启服务并验证
systemctl enable nginx --now
# 验证
# nginx节点
curl 127.0.0.1 # 这里我们将会看到node1和node2的网页分别显示
四. Nginx负载均衡
作者的理解
当一个网站需要被大量访问时,如果只有一个nginx服务器,是不够支持大量访问的,所有需要有负载均衡,通过服务器将访问量进行分流,但是我们所看到的网页并不会由任何变化。
五. 总结
Nginx负载均衡还是比较重要的,对于企业来说,需要防止网站的流量过大,导致其服务器崩溃,从而损失严重。本人最近也在往云运维方向发展(可能写的不好,请见谅!!!)❤️❤️❤️❤️❤️