Nginx负载均衡

Nginx负载均衡

  • [1. upstream 配置](#1. upstream 配置)
  • [2. 负载均衡调度算法](#2. 负载均衡调度算法)
  • [3. 负载均衡类型讲解](#3. 负载均衡类型讲解)
    • [3.1. backup热备](#3.1. backup热备)
    • [3.2. RR轮询](#3.2. RR轮询)
    • [3.3. WRR加权轮询](#3.3. WRR加权轮询)
    • [3.4. IP_Hash算法](#3.4. IP_Hash算法)
    • [3.5. URL_Hash算法](#3.5. URL_Hash算法)
    • [3.6. 负载均衡的其他配置](#3.6. 负载均衡的其他配置)
  • [4. Nginx 负载均衡7层负载配置实战](#4. Nginx 负载均衡7层负载配置实战)
    • [4.1. OSI七层回顾](#4.1. OSI七层回顾)
    • [4.2. 配置实战](#4.2. 配置实战)
  • [5. Nginx 负载均衡4层配置方法(扩展)](#5. Nginx 负载均衡4层配置方法(扩展))

1. upstream 配置

Nginx 的upstream模块允许我们定义一组后端服务器,并根据一定的策略转发客户端请求到这些后端服务器上。通过upstream配置,可以实现负载均衡和故障转移等功能。

Nginx 根据配置的策略,自动选择后端服务器,并将客户端请求转发到所选的服务器上。在后端服务器故障时,Nginx 支持自动剔除故障服务器,并将请求重新分配到其他正常的服务器上。

bash 复制代码
#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服务器是不会工作的,就是用来当备胎的

bash 复制代码
upstream myweb { 
      server 172.17.14.2:8080; 
      server 172.17.14.3:8080 backup;  #热备     
    }

3.2. RR轮询

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

bash 复制代码
upstream myweb { 
      server 172.17.14.2:8080; 
      server 172.17.14.3:8080;      
    }

3.3. WRR加权轮询

加权轮询(weight round robin):跟据配置的权重的大小,分发给不同服务器不同数量的请求。

如果不设置,则默认为1。下面服务器的请求顺序为:ABBABBABBABBABB...

bash 复制代码
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请求相同的服务器。

bash 复制代码
upstream myweb { 
      server 172.17.14.2:8080; 
      server 172.17.14.3:8080;
      ip_hash;
    }

3.5. URL_Hash算法

bash 复制代码
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一起使用。相当于一个冷却服务的时间

bash 复制代码
 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)是一个开放性的通行系统互连参考模型,他是一个定义的非常好的协议规范,共包含七层协议。

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反向代理服务器
c 复制代码
//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工具访问

c 复制代码
//在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访问,观察情况

c 复制代码
[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由四个层次组成:网络接口层、网络层、传输层、应用层。

nginx在1.9.0的时候,增加了一个 stream 模块,用来实现四层协议(网络层和传输层)的转发、代理、负载均衡等。

stream 模块的用法跟 http 的用法类似,允许我们配置一组TCP或者UDP等协议的监听

配置案例:

bash 复制代码
#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;
        }
}
相关推荐
绵绵细雨中的乡音2 小时前
网络基础知识
linux·网络
Peter·Pan爱编程2 小时前
Docker在Linux中安装与使用教程
linux·docker·eureka
kunge20133 小时前
Ubuntu22.04 安装virtualbox7.1
linux·virtualbox
清溪5493 小时前
DVWA中级
linux
MUY09903 小时前
应用控制技术、内容审计技术、AAA服务器技术
运维·服务器
楠奕3 小时前
elasticsearch8.12.0安装分词
运维·jenkins
Sadsvit4 小时前
源码编译安装LAMP架构并部署WordPress(CentOS 7)
linux·运维·服务器·架构·centos
xiaok4 小时前
为什么 lsof 显示多个 nginx 都在 “使用 443”?
linux
java资料站4 小时前
Jenkins
运维·jenkins
苦学编程的谢4 小时前
Linux
linux·运维·服务器