nginx负载均衡

默认算法为:round-roubin

round-roubin

修改配置

load balance配置

#定义负载均衡器
    upstream myweb{
        server 192.168.186.140;
        server 192.168.186.141;
        server 192.168.186.142;
        }

    server {
        listen       80;
        server_name  localhost;

        location / {
                proxy_pass http://myweb;
        }

web服务器配置

web1

[root@web1 html]# vim /usr/local/nginx/html/index.html
hello! welcome to web1!

web2

[root@web2 html]# vim /usr/local/nginx/html/index.html
hello! welcome to web2!

web3

[root@web3 html]# vim /usr/local/nginx/html/index.html
hello! welcome to web3!

刷新服务

[root@localhost conf]# nginx -s reload

验证

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094611.png

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094657.png

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094725.png

least_conn

修改配置

load balance配置

upstream myweb{
        least_conn;
        server 192.168.186.140;
        server 192.168.186.141;
        server 192.168.186.142;
    }

    server {
        listen       80;
        server_name localhost;

        location / {
                proxy_pass http://myweb;
        }

刷新服务

[root@localhost conf]# nginx -s reload

验证

需要服务器有较多连接时才能查看明显效果

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094611.png

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094657.png

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094725.png

ip-hash

修改配置

load balance配置

upstream myweb{
        ip_hash;
        server 192.168.186.140;
        server 192.168.186.141;
        server 192.168.186.142;
    }

    server {
        listen       80;
        server_name localhost;

        location / {
                proxy_pass http://myweb;
        }

刷新服务

[root@localhost conf]# nginx -s reload

验证

多次刷新访问,访问的均为一个固定的服务器

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609100133.png

加权轮询

修改配置

laod balance配置

upstream myweb{
        server 192.168.186.140 weight=1;
        server 192.168.186.141 weight=10;
        server 192.168.186.142 weight=3;
    }

    server {
        listen       80;
        server_name localhost;

        location / {
                proxy_pass http://myweb;
        }

刷新服务

[root@localhost conf]# nginx -s reload

验证

权重值越大的,访问次数越多

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094611.png

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094657.png

https://gitee.com/wupei_w/photo/raw/master/wupei_w/photo/20210609094725.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值