欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Nginx多种负载均衡策略搭建

程序员文章站 2022-05-15 13:35:02
背景介绍 上篇介绍了利用Nginx反向代理实现负载均衡,本文详细讲述Nginx下的几种负载均衡策略。 轮询 轮询,顾名思义,就是轮流请求,基于上篇文章的介绍,我们将负载均衡策略聚焦于 文件的 。 在浏览器中对 连续发出请求,根据nginx请求日志可以看出web02与web03访问的次数是相同的。 加 ......

背景介绍

上篇介绍了利用nginx反向代理实现负载均衡,本文详细讲述nginx下的几种负载均衡策略。

轮询

轮询,顾名思义,就是轮流请求,基于上篇文章的介绍,我们将负载均衡策略聚焦于default.conf文件的upstream

upstream backend {
    server web02:80;
    server web03:80;
}

在浏览器中对localhost:8080连续发出请求,根据nginx请求日志可以看出web02与web03访问的次数是相同的。

web01    | 172.24.0.1 - - [26/jun/2019:10:03:06 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:10:03:06 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:10:03:07 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.3 - - [26/jun/2019:10:03:07 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:10:05:25 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:10:05:25 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.3 - - [26/jun/2019:10:05:26 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:10:05:26 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:10:05:27 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:10:05:27 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:10:05:27 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.3 - - [26/jun/2019:10:05:27 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"

加权轮询

加权轮询是指基于上文中的轮询,加上一定的权重,使得某个服务器被转发的次数更多一些,加权通过在ip后方拼接weight实现。

upstream backend {
    server web02:80 weight=5;
    server web03:80 weight=1;
}

weight值越大,所分配的请求就越多,根据nginx的请求日志可以看出web02被访问了8次,web03被访问了两次。

web01    | 172.24.0.1 - - [26/jun/2019:14:48:13 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:14:48:13 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:14:48:16 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:16 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:16 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:14:48:16 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:17 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.3 - - [26/jun/2019:14:48:17 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:17 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:14:48:17 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:14:48:18 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:18 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:19 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:14:48:19 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:19 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:14:48:19 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:20 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.3 - - [26/jun/2019:14:48:20 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.3 - - [26/jun/2019:14:48:20 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [26/jun/2019:14:48:20 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"

ip_hash

如果一个客户端的请求可能被转发到任何一台服务器,可能会导致每台服务器都缓存了该客户端的信息,比较浪费资源,ip_hash作用就是通过客户端ip的前三个部分计算出key,以便将同一个客户端的请求转发到指定的服务器中,配置的方法比较简单,只需要在upstream上方加一行代码即可。

upstream backend {
    ip_hash;
    server web02:80;
    server web03:80;
}

重复请求了5次,通过日志看出请求一直被锁定在了web03上。

web01    | 172.24.0.1 - - [27/jun/2019:11:48:15 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.2 - - [27/jun/2019:11:48:15 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "172.24.0.1"
web01    | 172.24.0.1 - - [27/jun/2019:11:48:22 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.2 - - [27/jun/2019:11:48:22 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "172.24.0.1"
web03    | 172.24.0.2 - - [27/jun/2019:11:48:23 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "172.24.0.1"
web01    | 172.24.0.1 - - [27/jun/2019:11:48:23 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.2 - - [27/jun/2019:11:48:24 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "172.24.0.1"
web01    | 172.24.0.1 - - [27/jun/2019:11:48:24 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [27/jun/2019:11:48:24 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.2 - - [27/jun/2019:11:48:24 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "172.24.0.1"

url_hash

这种模式是将url进行哈希,然后定位到具体的某一台服务器,相同的url永远会被转发到同一台服务器上。

upstream backend {
    hash $request_uri;
    server web02:80;
    server web03:80;
}

通过浏览器访问两次http://localhost:8080,通过日志可以看到转发被固定在了web03。

web01    | 172.24.0.1 - - [27/jun/2019:11:58:31 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.4 - - [27/jun/2019:11:58:31 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web03    | 172.24.0.4 - - [27/jun/2019:11:58:38 +0000] "get / http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [27/jun/2019:11:58:38 +0000] "get / http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"

在链接后面拼接一些参数,就可以看到转发发生了变化,固定在了web02,随着参数的变化,请求也会采用轮询的方式转发给服务器,但使用过的url再访问就不会再变化了。

web02    | 172.24.0.4 - - [27/jun/2019:11:59:54 +0000] "get /?a=1 http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [27/jun/2019:11:59:54 +0000] "get /?a=1 http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web02    | 172.24.0.4 - - [27/jun/2019:11:59:55 +0000] "get /?a=1 http/1.0" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"
web01    | 172.24.0.1 - - [27/jun/2019:11:59:55 +0000] "get /?a=1 http/1.1" 304 0 "-" "mozilla/5.0 (macintosh; intel mac os x 10_14_2) applewebkit/537.36 (khtml, like gecko) chrome/74.0.3729.169 safari/537.36" "-"