Nginx的upstream目前支持的分配算法:
一、round-robin 轮询1:1轮流处理请求(默认)
每一个请求按时间顺序逐一分配到不一样的应用服务器,若是应用服务器down掉,自动剔除,剩下的继续轮询。
二、weight 权重(加权轮询)
经过配置权重,指定轮询概率,权重和访问比率成正比,用于应用服务器性能不均的状况。
三、ip_hash 哈希算法
每一个请求按访问ip的hash结果分配,这样每一个访客固定访问一个应用服务器,能够解决session共享的问题。应用服务器若是故障须要手工down掉。
参数含义:
down:表示单前的server暂时不参与负载
weight:默认为1,weight越大,负载的权重就越大。
max_fails:容许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
fail_timeout:max_fails次失败后,暂停的时间。
backup:其它全部的非backup机器down或者忙的时候,请求backup机器。html
1、七层负载配置(默认支持)
示例以下:
upstream tomcats {
server 10.0.0.1:8080;
server 10.0.0.2:8080 weight=2;
}
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://tomcats;
index index.html index.htm;
}
}nginx
2、四层负载配置
nginx1.9.0以后引入模块ngx_stream_core_module支持TCP负载,默认没有编译,须要在编译时添加--with-stream配置参数
stream与http处在同一级别
示例以下:
stream{
upstream tomcats {
server 10.0.0.1:8080;
server 10.0.0.2:8080 weight=2;
}
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://tomcats;
index index.html index.htm;
}
}
}
算法