十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

nginx的upstream目前支持5种方式的分配

上一篇 / 下一篇  2011-06-05 23:28:12 / 个人分类:nginx

1、轮询(默认)51Testing软件测试网"B?JB(K

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。51Testing软件测试网c8wPp?LL

weight指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。  51Testing软件测试网:~Rh;r$?V
例如:
@L#zA\d t%K0    upstream bakend {
VAC,U_IkXP-BG0         server 192.168.0.14 weight=10;
+V sp~*t0l%Z0         server 192.168.0.15 weight=10;51Testing软件测试网\,P1BoMI g/D
    }

YuS U)B8t/LCF4S0

2、ip_hash 
.Y$r7l5Pn4bg0每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
%@+t;v p.K,| Z0K0例如:51Testing软件测试网6J b{2~\;F
    upstream bakend {51Testing软件测试网U"P:A!W;cI0mO
         ip_hash;
BK nf E/adMp&x0         server 192.168.0.14:88;
"go^$Y-d&s.@0         server 192.168.0.15:80;
Zm FQF0    }51Testing软件测试网tSX/t"w|(q]u

3、fair(第三方)51Testing软件测试网8D#r$}b0K(\
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
S})aY&K4j0upstream backend {
V(PNCIp S g_/B9L0    server server1;
kw8u uR#MTP0    server server2;
2\ K i^ V(~)B0    fair;51Testing软件测试网P\z0P2akcv
}

$v8P ud9U le$~0

4、url_hash(第三方)51Testing软件测试网B].M.z/D*I)^c(Fh.\

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

.W,Y,uvviz%u0

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法51Testing软件测试网L"lC'o T bk

upstream backend {51Testing软件测试网!Jd#{ LD#@7?2Uo
    server squid1:3128;51Testing软件测试网&{7m^Mf? eV
    server squid2:3128;
+sdk m-jZu#@0    hash   $request_uri;51Testing软件测试网F7V/l5v&?f-i f
    hash_method crc32;
&R%LJzYx0^0}51Testing软件测试网eET-`:tv

 

^{c6I5P0

5、tips:

r3i h-N.swi0

upstream bakend{#定义负载均衡设备的Ip及设备状态
Z.r%Qm(J\He0ip_hash;51Testing软件测试网([5Tt ~;?V5EtF
    server 127.0.0.1:9090 down;51Testing软件测试网6s?)B/Ko/[7n
    server 127.0.0.1:8080 weight=2;51Testing软件测试网+H6J7X4?8y
    server 127.0.0.1:6060;
hN$XK4~H_`Q0    server 127.0.0.1:7070 backup;
m3_Fz2k_?\ Rg5Z0}
u2d.m!|mJ0在需要使用负载均衡的server中增加51Testing软件测试网I4qZ_$pAA%|S1^
proxy_pass http://bakend/;

%U(P\-D7R"GB)ro4M0

每个设备的状态设置为:51Testing软件测试网 y2D:i,`x P1^%N(y#@
1.down 表示单前的server暂时不参与负载
,b7^b;G,AY6TX1W y02.weight 默认为1.weight越大,负载的权重就越大。51Testing软件测试网d7[:m o5Y(a
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误51Testing软件测试网)q.jGg+oI.m}
4.fail_timeout:max_fails次失败后,暂停的时间。51Testing软件测试网|+z%l5}8N5bl8f
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

%?;~Cd5KA6j8Z0

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

d gS5I$k}f] {0

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug51Testing软件测试网Q{1u V\V)D*M%B
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录51Testing软件测试网r6kI&N^ Au:Q8E

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

Ikw,q Qy(J9OYge0

TAG: nginx Nginx

 

评分:0

我来说两句

Open Toolbar