Nginx: upstream parameters 参数, 配置负载均衡时如下例:
upstream backend {
server backend1.example.com weight=5;
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
}
其中可选参数为: weight=number,
max_fails
=number, fail_timeout=
time, backup,
down;
官网文档原文:
Defines an address
and other parameters
of the server. An address can be specified as a domain name or IP address, and an optional port, or as a UNIX-domain socket path specified after the “unix:
” prefix. If port is not specified, the port 80 is used. A domain name that resolves to several IP addresses essentially defines multiple servers.
The following parameters can be defined:
weight
=number
- sets a weight of the server, by default 1.
-
sets a number of unsuccessful attempts to communicate with the server during a time set by the
fail_timeout
parameter after which it will be considered down for a period of time also set by thefail_timeout
parameter. By default, the number of unsuccessful attempts is set to 1. A value of zero disables accounting of attempts. What is considered to be an unsuccessful attempt is configured by the proxy_next_upstream, fastcgi_next_upstream, and memcached_next_upstream directives. Thehttp_404
state is not considered an unsuccessful attempt. -
sets
- a time during which the specified number of unsuccessful attempts to communicate with the server should happen for the server to be considered down;
- and a period of time the server will be considered down.
- marks the server as a backup server. It will be passed requests when the primary servers are down.
- marks the server as permanently down; used along with the ip_hash directive.
max_fails
=
number
fail_timeout
=
time
backup
down
Example:
upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; server backup1.example.com:8080 backup; }
syntax: | ip_hash; |
default: | — |
context: | upstream |
Specifies that a group should use a load balancing method where requests are distributed between servers based on client IP addresses. The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key. The method ensures that requests of the same client will always be passed to the same server except when this server is considered down in which case client requests will be passed to another server and most probably it will also be the same server.
IPv6 addresses are supported starting from versions 1.3.2 and 1.2.2.
If one of the servers needs to be temporarily removed, it should be marked with the down
parameter in order to preserve the current hashing of client IP addresses.
Example:
upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com down; server backend4.example.com; }
Until versions 1.3.1 and 1.2.2 it was not possible to specify a weight for servers using the ip_hash
load balancing method.
syntax: | keepalive |
default: | — |
context: | upstream |
This directive appeared in version 1.1.4.
Activates cache of connections to upstream servers.
个人总结上面几点:
1. IPV6 从Nginx1.2.2(稳定版本)支持;
2. ip_hash 可以支持 weight参数,从Nginx1.2.2(稳定版本)支持;
3. down 只在配置ip_hash中使用;
4. max_fails和fail_timeout参数要结合使用;
5. 当一个做balance的servers配置max_fails和fail_timeout时:
当在fail_timeout的时间内,某个server连接失败了max_fails次,则nginx会认为该server不工作了.
同时,在接下来的 fail_timeout时间内,nginx不再将请求分发给失效的server.
如果超出fail_timeout后,nginx则继续把request发往该server上.
后面依次再做上面循环.