Nginx实现负载均衡

1. upstream模块

2. 负载均衡算法

2.1 轮询算法

2.2 最少连接算法

2.3 权重算法

2.4 IP哈希算法

2.5 Nginx其他配置

3.Nginx负载均衡配置

3.1 实例环境

3.2 Nginx调度器配置

3.3 配置真实服务器节点(两台提供服务的服务器)

3.4 客户端配置

3.5 测试


1. upstream模块

upstream模块是配置Nginx与后端服务器负载均衡非常重要的一个模块,并且它还能对后端服务器的健康状态进行检查。若后端

服务器中的一台发生故障,则前端的请求不会转发到故障的机器。

在nginx.conf配置文件中,用upstream指令定义一组负载均衡后端服务器池。

2. 负载均衡算法

2.1 轮询算法

每个请求轮流分配到不同的后端服务器,为默认算法。如果后端服务器宕机,将自动剔除。

[root@CentOS7 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    upstream www.test.com {
        server 192.168.146.111;
        server 192.168.146.112;
    }

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://www.test.com;
        }
    }
}

2.2 最少连接算法

下一个请求被分配给活动连接数最少的服务器。

[root@CentOS7 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    upstream www.test.com {
        least_conn;
        server 192.168.146.111;
        server 192.168.146.112;
    }


    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://www.test.com;
        }
    }
}

2.3 权重算法

通过将不同的后端服务器设置不同的权重以便实现请求的按比例分配,当后端服务器故障时可以自动剔除该服务器。用于后端服务器性能不均的情况。

[root@CentOS7 ~]# vim /usr/local/nginx/conf/nginx.conf

http {

    upstream www.test.com {
        server 192.168.146.111 weight=2;
        server 192.168.146.112 weight=1;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://www.test.com;
        }

    }
}

2.4 IP哈希算法

此算法根据用户的客户端IP将请求分配给后端的服务器,由于源IP相同的客户端经过IP哈希算法后的值相同,因此同一客户端的请求可以分配到后端的同一台服务器上。IP哈希负载均衡主要通过指令ip_hash指定。

[root@CentOS7 ~]# vim /usr/local/nginx/conf/nginx.conf
http {

    upstream www.test.com {

        ip_hash;
        server 192.168.146.111;
        server 192.168.146.112;


    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://www.test.com;
        }
    }
}

2.5 Nginx其他配置

在Nginx upstream模块中,可以设定每台后端服务器在负载均衡调度中的状态,常用的状态有以下几种。

状态说明
down表示当前服务器暂时不参与负载均衡
backup预留的备份服务器。当其他所有的非backup服务器出现故障或繁忙的时候,才会请求到backup服务器,因此这台服务器的访问压力最低。可以用于产品的分批次上线
weight权重值,默认为1。weight越大,负载的权重就越大。
max_fails允许请求失败的次数,默认为1。当超过最大次数后,返回proxy_next_upstream模块定义的错误。 设置为0,禁用此服务器的运行状况检查
fail_timeout如果某台服务器在fail_timeout时间内出现了max_fails次连接失败,那么Nginx就会认为该服务器已经故障,从而剔除该服务器。

 

 

 

 

 

 

 

 

 

 

3.Nginx负载均衡配置

3.1 实例环境

参数说明
Nginx调度器192.168.146.101
server1192.168.146.111
server2192.168.146.112
client192.168.146.112
测试域名www.test.com
Linux版本CentOS

 

 

 

 

 

 

 

 

3.2 Nginx调度器配置

[root@CentOS7 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    #用upstream指令定义一组负载均衡后端服务器池,需要下面proxy_pass里的一样
    upstream www.test.com {
        server 192.168.146.111 weight=2;
        server 192.168.146.112 weight=1;
    }
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://www.test.com;
        }
    }
}

重启调度器Nginx服务

[root@CentOS7 ~]# /usr/local/nginx/sbin/nginx -s reload

3.3 配置真实服务器节点(两台提供服务的服务器)

[root@server1 ~]# vim auto.sh 
#!/bin/bash
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
#安装依赖包
#判断当前主机是否安装Nginx
if [ -z "$ISINSTALL" ]
    then
#       wget http://nginx.org/download/nginx-1.16.1.tar.gz
        tar -zxvf nginx-1.16.1.tar.gz
        cd nginx-1.16.1
        ./configure --prefix=/usr/local/nginx
        make -j 4 && make install
fi
#获取当前主机IP
CURRENT_IP=`/sbin/ifconfig ens33 | grep -v "inet6" | grep "inet" | awk '{print $2}'`
echo "$CURRENT_IP" > /usr/local/nginx/html/index.html
#测试配置文件是否正确
#启动Nginx
/usr/local/nginx/sbin/nginx

在server1和server2上执行如下操作

[root@server1 ~]# chmod +x auto.sh 
[root@server1 ~]# ./auto.sh

[root@server2 ~]# chmod +x auto.sh 
[root@server2 ~]# ./auto.sh

3.4 客户端配置

在客户端/etc/hosts添加主机名到域名的映射

[root@client ~]# echo "192.168.146.101 www.test.com" >> /etc/hosts

3.5 测试

负载均衡测试。本实验基于轮询的策略。

当把server2服务器(192.168.146.112)停掉,发现只访问server1。然后查看nginx调度服务器的错误日志。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值