Module ngx_http_upstream_module
The ngx_http_upstream_module
module allows to define groups of servers that can be referenced from the proxy_pass, fastcgi_pass, and memcached_pass directives.
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
syntax: | upstream |
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.
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; }
By default, requests are distributed between servers using a weighted round-robin balancing method. In the above example, each 7 requests will be distributed as follows: 5 requests to backend1.example.com
and one request to each of second and third servers. If an error occurs when communicating with the server, a request will be passed to the next server, and so on until all of the functioning servers will be tried. If a successful response could not be obtained from any of the servers, the client will be returned the result of contacting the last server.
syntax: | server |
default: | — |
context: | upstream |
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.
max_fails
=number
- 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. fail_timeout
=time
- 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.
backup
- marks the server as a backup server. It will be passed requests when the primary servers are down.
down
- marks the server as permanently down; used along with the directive.
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.
The connections
parameter sets the maximum number of idle keepalive connections to upstream servers that are retained in the cache per one worker process. When this number is exceeded, the least recently used connections are closed.
It should be particularly noted thatkeepalive
directive does not limit the total number of connections that nginx worker process can open to upstream servers. Theconnections
parameter should be set low enough to allow upstream servers to process additional new incoming connections as well.
Example configuration of memcached upstream with keepalive connections:
upstream memcached_backend { server 127.0.0.1:11211; server 10.0.0.2:11211; keepalive 32; } server { ... location /memcached/ { set $memcached_key $uri; memcached_pass memcached_backend; } }
For HTTP, the proxy_http_version directive should be set to “1.1
” and the “Connection” header field should be cleared:
upstream http_backend { server 127.0.0.1:8080; keepalive 16; } server { ... location /http/ { proxy_pass http://http_backend; proxy_http_version 1.1; proxy_set_header Connection ""; ... } }
Alternatively, HTTP/1.0 persistent connections can be used by passing the “Connection: Keep-Alive” header field to an upstream server, though this is not recommended.
For FastCGI servers, it is required to set fastcgi_keep_conn for keepalive connections to work:
upstream fastcgi_backend { server 127.0.0.1:9000; keepalive 8; } server { ... location /fastcgi/ { fastcgi_pass fastcgi_backend; fastcgi_keep_conn on; ... } }
When using load balancer methods other than the default round-robin, it is necessary to activate them before the keepalive
directive.
SCGI and uwsgi protocols do not have a notion of keepalive connections.
syntax: | least_conn; |
default: | — |
context: | upstream |
This directive appeared in versions 1.3.1 and 1.2.2.
Specifies that a group should use a load balancing method where a request is passed to the server with the least number of active connections, taking into account weights of servers. If there are several such servers, they are tried using a weighted round-robin balancing method.
Embedded Variables
The ngx_http_upstream_module
module supports the following embedded variables:
$upstream_addr
- keeps an IP address and port of the server, or a path to the UNIX-domain socket. If several servers were contacted during request processing, their addresses are separated by commas, e.g. “
192.168.1.1:80, 192.168.1.2:80, unix:/tmp/sock
”. If an internal redirect from one server group to another happened using “X-Accel-Redirect” or error_page then these server groups are separated by colons, e.g. “192.168.1.1:80, 192.168.1.2:80, unix:/tmp/sock : 192.168.10.1:80, 192.168.10.2:80
”. $upstream_cache_status
- keeps status of accessing a response cache (0.8.3). The status can be one of “
MISS
”, “BYPASS
”, “EXPIRED
”, “STALE
”, “UPDATING
” or “HIT
”. $upstream_response_length
- keeps lengths of responses obtained from upstream servers (0.7.27); lengths are kept in bytes. Several responses are separated by commas and colons like in the
$upstream_addr
variable. $upstream_response_time
- keeps times of responses obtained from upstream servers; times are kept in seconds with a milliseconds resolution. Several responses are separated by commas and colons like in the
$upstream_addr
variable. $upstream_status
- keeps codes of responses obtained from upstream servers. Several responses are separated by commas and colons like in the
$upstream_addr
variable. $upstream_http_...
- keep server response header fields. For example, the “Server” response header field is made available through the
$upstream_http_server
variable. The rules of converting header field names to variable names are the same as for variables starting with the “$http_” prefix. Only the last server’s response header fields are saved.