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

验证

nginx负载均衡

nginx负载均衡

nginx负载均衡

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

验证

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

nginx负载均衡

nginx负载均衡

nginx负载均衡

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

验证

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

nginx负载均衡

加权轮询

修改配置

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

验证

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

nginx负载均衡

nginx负载均衡

nginx负载均衡

上一篇:使用vue-cli创建Vue项目


下一篇:我的数据库