Nginx负载均衡

1. upstream 配置

Nginx 的upstream模块允许我们定义一组后端服务器,并根据一定的策略转发客户端请求到这些后端服务器上。通过upstream配置,可以实现负载均衡和故障转移等功能。
Nginx 根据配置的策略,自动选择后端服务器,并将客户端请求转发到所选的服务器上。在后端服务器故障时,Nginx 支持自动剔除故障服务器,并将请求重新分配到其他正常的服务器上。

#upstream语法
upstream testapp { 
      server 10.0.105.199:8081 backup;      #后端服务器的IP和端口,backup策略
      server 10.0.105.202:8081;     #后端服务器的IP和端口
    }
server {
        ....
        location / {         
           proxy_pass  http://testapp;  #请求转向 testapp 定义的服务器列表         
        } 
    }

2. 负载均衡调度算法

1.轮询(默认的负载均衡算法):每个请求按时间顺序逐一分配到不同的后端服务器。
2.ip_hash:每个请求按访问IP的hash结果分配,同一个IP客户端固定访问一个后端服务器。可以保证来自同一ip的请求被打到固定的机器上,可以解决session问题。
3.url_hash:按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器。
4.fair:这是比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持 fair的,如果需要使用这种调度算法,必须下载Nginx的 upstream_fair模块。

3. 负载均衡类型讲解

3.1. backup热备

热备:如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务,A服务器正常的情况,B服务器是不会工作的,就是用来当备胎的

upstream myweb { 
      server 172.17.14.2:8080; 
      server 172.17.14.3:8080 backup;  #热备     
    }

3.2. RR轮询

轮询(round robin):nginx默认就是轮询其权重都默认为1,服务器处理请求的顺序:ABABABABAB…

upstream myweb { 
      server 172.17.14.2:8080; 
      server 172.17.14.3:8080;      
    }

3.3. WRR加权轮询

加权轮询(weight round robin):跟据配置的权重的大小,分发给不同服务器不同数量的请求。
如果不设置,则默认为1。下面服务器的请求顺序为:ABBABBABBABBABB…

upstream myweb { 
      server 172.17.14.2:8080 weight=1;
      server 172.17.14.3:8080 weight=2;
}

3.4. IP_Hash算法

ip_hash:nginx会让相同的客户端ip请求相同的服务器。

upstream myweb { 
      server 172.17.14.2:8080; 
      server 172.17.14.3:8080;
      ip_hash;
    }

3.5. URL_Hash算法

upstream myweb { 
      server 172.17.14.2:8080; 
      server 172.17.14.3:8080;
      hash $request_uri;
      hash_method crc32;
    }
#hash_method是使用的hash算法

3.6. 负载均衡的其他配置

nginx负载均衡配置状态参数
1.down:表示当前的server暂时不参与负载均衡。
​2.backup:预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
​3.max_fails:允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
​4.fail_timeout:在经历了max_fails次失败后,暂停服务的时间,单位秒。
max_fails可以和fail_timeout一起使用。相当于一个冷却服务的时间

 upstream myweb { 
      server 172.17.14.2:8080 weight=2 max_fails=2 fail_timeout=2;
      server 172.17.14.3:8080 weight=1 max_fails=2 fail_timeout=1;    
    }

4. Nginx 负载均衡7层负载配置实战

4.1. OSI七层回顾

OSI(Open System Interconnection)是一个开放性的通行系统互连参考模型,他是一个定义的非常好的协议规范,共包含七层协议。
1

4.2. 配置实战

准备三台机器,再增加一台机器,作为nginx的后端服务器,安装方式建议跟前面2台保持一致

IP主要作用注意事项
192.168.221.130反向代理服务器(客户访问的最前端的服务器)在本地做好host文件解析,解析的IP是128
192.168.221.136后端真实服务器index.html文件要和138不一致
192.168.221.138后端真实服务器index.html文件要和136不一致
  1. 配置nginx反向代理服务器
//nginx配置文件
[root@localhost ~]# cd /etc/nginx/conf.d
[root@localhost conf.d]# cp proxy.conf upstream.conf
[root@localhost conf.d]# vim upstream.conf
upstream testweb {
       server 192.168.221.136:80 weight=1 max_fails=2 fail_timeout=2s;
       server 192.168.221.138:80 weight=3 max_fails=2 fail_timeout=2s;
        #ip_hash;   #ip_hash会话保持
     }
server {
    listen       80;
    server_name  www.test-upstream.com;
    access_log  /var/log/nginx/test-upstrem_access.log  main;
    location / { 
       proxy_pass http://testweb;
       proxy_redirect default;
       proxy_set_header Host $http_host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_connect_timeout 30; 
       proxy_send_timeout 60; 
       proxy_read_timeout 60;
       }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
  1. 配置nginx后端服务器
    192.168.221.136修改index.html文件(随意改动一点)
    192.168.221.138使用官方yum源安装后,只需要启动nginx即可,不需要修改index.html文件,也是保持默认即可
  2. 访问反向代理服务器,观察情况
    在hosts文件的最尾部添加一行
    192.168.221.130 www.test-upstream.com
    浏览器测试访问:http://www.test-upstream.com

声明:在浏览器中访问,ip_hash注释的情况下,权重的访问并不明显,这是因为浏览器中有缓存的原因;我们在服务器中使用elinks工具访问

//在136修改/etc/hosts文件
[root@localhost conf.d]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.17.128 www.test-upstream.com
​
//在136中使用elinks访问域名;第一次访问,跳到了129中
[root@localhost conf.d]# elinks  -dump www.test-upstream.com
                                欢迎来到我的网站
     * [1]主页
     * [2]关于
     * [3]联系方式
   在这里添加您的主要内容...
   版权所有 © 2023 My Website
References
   Visible links
   1. http://www.test-upstream.com/#
   2. http://www.test-upstream.com/#
   3. http://www.test-upstream.com/#
   
//第二次访问,跳到了138上面   
[root@localhost conf.d]# elinks  -dump www.test-upstream.com
                               Welcome to nginx!
   If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.
   For online documentation and support please refer to [1]nginx.org.
   Commercial support is available at [2]nginx.com.
   Thank you for using nginx.
References
   Visible links
   1. http://nginx.org/
   2. http://nginx.com/
//后面多访问几次,可以看到跳转页面根据权重来分配

现在将130中配置文件的ip_hash取消注释,也就是将ip_hash算法打开,再次使用elinks访问,观察情况

[root@localhost conf.d]# hostname -I
192.168.221.130
[root@localhost conf.d]# vim upstrem.conf 
upstream testweb {
    server 192.168.221.136:80 weight=1 max_fails=2 fail_timeout=2s;
    server 192.168.221.138:80 weight=3 max_fails=2 fail_timeout=2s;
    ip_hash;    #将这行的注释打开
     }
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl restart nginx   
​
//136使用elinks访问,观察情况
[root@localhost conf.d]# elinks  -dump www.test-upstream.com
                               Welcome to nginx!
​
   If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.
​
   For online documentation and support please refer to [1]nginx.org.
   Commercial support is available at [2]nginx.com.
​
   Thank you for using nginx.
​
References
​
   Visible links
   1. http://nginx.org/
   2. http://nginx.com/
http://nginx.com/
//多访问几次,可以看到都是访问到权重较大的服务器上了

总结:加了ip_hash算法的时候,同一个客户端的访问IP会被转发到同一个后端服务器中,没有加ip_hash的时候会按照upstream中的权重来访问

5. Nginx 负载均衡4层配置方法(扩展)

4层协议:TCP/IP协议
之所以说TCP/IP是一个协议族,是因为TCP/IP协议包括TCP、IP、UDP、ICMP、RIP、SMTP、ARP、TFTP等许多协议,这些协议一起称为TCP/IP协议。
从协议分层模型方面来讲,TCP/IP由四个层次组成:网络接口层、网络层、传输层、应用层。
2
nginx在1.9.0的时候,增加了一个 stream 模块,用来实现四层协议(网络层和传输层)的转发、代理、负载均衡等。
stream 模块的用法跟 http 的用法类似,允许我们配置一组TCP或者UDP等协议的监听
配置案例:

#4层tcp负载 
stream {
        upstream myweb {
                hash $remote_addr consistent;
                server 172.17.14.2:8080;
                server 172.17.14.3:8080;
        }
        server {
            listen 82;
            proxy_connect_timeout 10s;
            proxy_timeout 30s;
            proxy_pass myweb;
        }
}
  • 21
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TA548464

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值