Nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕机了才会换一个。
upstream tomcatserver {#定义负载均衡 设备的Ip及设备状态
ip_hash;
server :
8080
down;
server :
8888
weight=
2
;
server :
9999
;
server :
7777
backup;
}
在需要使用负载均衡的server中增加
server {
listen
80
;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http:
//tomcatserver;
}
# 转发jsp servlet 或springmvc请求
#
location ~ (\.jsp)|(\.
do
)|(\.action)$ {
proxy_pass http:
//tomcatserver;
}
}
每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1,weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。