超详细Linux -- nginx负载均衡配置

Nginx 负载均衡应用配置

Nginx实现负载均衡的方式
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,后端服务器宕机时,能被自动删除,且请求不会受影响。
2、weight权重
指定轮询概率,weight和访问比率成正比,用于后端服务器性能不均的情况。权重越高,被访问的概率就越大。


1.环境配置

机器名服务器IP用途
nginx (主)192.168.233.70负载均衡服务器
服务器A192.168.233.80后端服务器
服务器B192.168.233.90后端服务器

需要三台虚拟机,同时都需要配置好nginx


 

 2.设置防火墙  三台虚拟机都要设置

firewall-cmd --zone=public --add-port=80/tcp --permanent
systemctl restart firewalld.service
 
关闭selinux: /etc/selinux/config
修改配置文件:将selinux=enforcing改为disabled
弄好后重启虚拟机,查看后出现Disabled
getenforce #查看selinux状态 
或者,
临时关闭(不用重启机器):setenforce 0

轮询模式负载均衡 

打开nginx(负责负载均衡)主虚拟机

编辑配置文件:

cd /usr/local/nginx/conf

cp nginx.conf nginx.conf.bak //养成良好习惯,备份一个配置文件

vim nginx.conf

在http{}模块里添加以下内容

upstream webServer {
  server 192.168.233.80:80; //服务器Aip
  server 192.168.233.90:80; //服务器Bip
}
server{
listen 80;
server_name 192.168.233.70;
location / {
  index  index.html index.htm;
  proxy_pass http://webServer; //【webServer】和upstream 【webServer】名字一致
}
}

检查语法并重启

/usr/local/nginx/sbin/nginx -t //检查语法
/usr/local/nginx/sbin/nginx -s reload //重启

配置服务器A

cd /usr/local/nginx/html/

cp index.html  index.html.bak

vim index.html //清空里面的所有配置

添加下面的语句:

<h>Welcome to serverA<h>

保存退出

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

 在浏览器测试:http://192.168.233.80


 配置服务器B

cd /usr/local/nginx/html/

cp index.html  index.html.bak

vim index.html //清空里面的所有配置

添加下面的语句:

<h>Welcome to serverB<h>

保存退出

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

在浏览器测试:http://192.168.233.90


测试负载均衡 

命令如下

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverA <h>
[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>
[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverA <h>
[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>

浏览器访问结果 :

 刷新之后,结果如图:


 权重模式负载均衡 

打开nginx(负责负载均衡)主虚拟机

编辑配置文件:

在http{}模块里添加以下内容

upstream webServer {
  server 192.168.233.80:80 weight=3; //在原来的基础上添加weight=自定义权重值
  server 192.168.233.90:80 weight=7;
}
server{
listen 80;
server_name 192.168.233.70;
location / {
  index  index.html index.htm;
  proxy_pass http://webServer;
}
}

保存退出

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

测试负载均衡 

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverA <h>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverA <h>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverA <h>

[root@localhost conf]# curl 192.168.233.70
<h>Welcome to serverB<h/>

从上面结果可以看出,一共测试访问10次用户请求,分流至serverB服务器上的有7次,分流至serverA服务器上的有3次,表明权重配置生效

  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值