Nginx Upstream 模块
官方文档: ngx_http_upstream_module
Example Configuration
Directives 指令
upstream
server
zone
state
hash
ip_hash
keepalive
keepalive_requests
keepalive_timeout
ntlm
least_conn
least_time
queue
random
sticky
sticky_cookie_insert
Embedded Variables
The ngx_http_upstream_module
module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, and grpc_pass directives.
upstream模块功能是定义服务器组,可以被 proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, and grpc_pass 指令引用.
Example Configuration 配置样例
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
Directives 指令
upstream
Syntax: | upstream name { ... } |
---|---|
Default: | — |
Context: | http |
Defines a group of servers. Servers can listen on different ports. In addition, servers listening on TCP and UNIX-domain sockets can be mixed.
定义的服务器组,服务器可以监听到不同端口,另外,可监听 tcp 和 Unix sockets.
Example:
upstream backend {
#默认使用轮询负载,加 weight是权重轮询,权重越大分配的请求越多
server backend1.example.com weight=5;
#后端tcp 监听 8080端口,30秒内最大失败次数3
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3; #sockets连接
#所有后端失败或者繁忙时,才考虑使用backup
server backup1.example.com backup;
}
server
Syntax: | server address [parameters]; |
---|---|
Default: | — |
Context: | upstream |
Directives | ||
---|---|---|
weight=number | sets the weight of the server, by default, 1. | 设置服务器权重,默认为一,越大则分配请求越多 |
max_conns=number | limits the maximum *number* of simultaneous active connections to the proxied server (1.11.5). Default value is zero, meaning there is no limit. If the server group does not reside in the shared memory, the limitation works per each worker process. |
限制与代理服务器同时活动的最大连接数,默认值为0,意味着没有限制. |
max_fails=number | sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the fail_timeout parameter to consider the server unavailable for a duration also set by the fail_timeout parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts. What is considered an unsuccessful attempt is defined by the proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream,scgi_next_upstream, memcached_next_upstream, and grpc_next_upstream directives. |
设置在fail_timeout参数设置的持续时间内应该发生的与服务器通信失败的尝试次数,以考虑在同样由fail_timeout参数设置的持续时间内服务器不可用。默认情况下,不成功尝试的次数设置为1。0值禁止对尝试进行计算。不成功的尝试由proxy_next_upstream、fastcgi_next_upstream、uwsgi_next_upstream、scgi_next_upstream、memcached_next_upstream和grpc_next_upstream指令定义。 |
fail_timeout =time |
the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable; and the period of time the server will be considered unavailable.By default, the parameter is set to 10 seconds. | 在fail_timeout时间内,与服务器通信失败 max_fails 次,认为服务器不可用.服务不可用时间为 fail_timeout.默认为10秒. |
backup | marks the server as a backup server. It will be passed requests when the primary servers are unavailable. | 标记此服务器为备份服务。在全部服务器不可用时才会将请求发送给它. |
down | marks the server as permanently unavailable | 标记此服务器长期不可用 |
ip_hash
Syntax: | ip_hash; |
---|---|
Default: | — |
Context: | upstream |
Example:
upstream backend {
ip_hash; #每个请求按照访问ip(即Nginx的前置服务器或者客户端IP)的hash结果分配,这样每个访客会固定访问一个后端服务器,可以解决session一致问题。
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
server backend4.example.com;
}