系统相关
首页 > 系统相关> > nginx负载均衡

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

标签:myweb,负载,192.168,server,nginx,html,均衡,localhost
来源: https://blog.csdn.net/weixin_54470937/article/details/117717591