stream_upstream module#

The module is used to define groups of servers that can be referenced by the proxy_pass directive.

Example Configuration#

upstream backend {
    hash $remote_addr consistent;
    zone backend 1m;

    server backend1.example.com:1935  weight=5;
    server unix:/tmp/backend3;
    server backend3.example.com       service=_example._tcp resolve;

    server backup1.example.com:1935   backup;
    server backup2.example.com:1935   backup;
}

resolver 127.0.0.53 status_zone=resolver;

server {
    listen 1936;
    proxy_pass backend;
}

Directives#

upstream#

Syntax:

upstream name { … }

Default:

Context:

stream

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:1935 weight=5;
    server 127.0.0.1:1935            max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend2;
    server backend3.example.com:1935 resolve;

    server backup1.example.com:1935  backup;
}

By default, requests are distributed between the servers using a weighted round-robin balancing method. In the above example, each 7 requests will be distributed as follows: 5 requests go to backend1.example.com and one request to each of the second and third servers.

If an error occurs during communication with a server, the 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 receive the result of the communication with the last server.

server#

Syntax:

server address [parameters];

Default:

Context:

upstream

Defines the address and other parameters of a server. The address can be specified as a domain name or IP address with an obligatory port, or as a UNIX domain socket path specified after the unix: prefix. A domain name that resolves to several IP addresses defines multiple servers at once.

The following parameters can be defined:

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.
Default value is 0, meaning there is no limit. If the server group does not reside in the shared memory, the limitation works per each worker process.

max_fails=number — sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by fail_timeout to consider the server unavailable; it is then retried after the same duration.

Here, an unsuccessful attempt is an error or timeout while establishing a connection with the server.

Note

If there’s only one server in an upstream, max_fails has no effect and will be ignored.

max_fails=1

the default number of unsuccessful attempts

max_fails=0

disables the accounting of attempts

fail_timeout=time — sets the period of time during which a number of unsuccessful attempts to communicate with the server (max_fails) should happen to consider the server unavailable. The server then becomes unavailable for the same amount of time before it is retried.

By default, this is set to 10 seconds.

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.

Caution

The backup parameter cannot be used along with the hash and random load balancing methods.

Added in version 1.3.0.

resolve

Enables monitoring changes to the list of IP addresses that corresponds to a domain name, updating it without a configuration reload. The group should be stored in a shared memory zone; also, you need to define a resolver.

service=name

Enables resolving DNS SRV records and sets the service name. For this parameter to work, specify the resolve server parameter, providing a hostname without a port number.

If there are no dots in the service name, the name is formed according to the RFC standard: the service name is prefixed with _, then _tcp is added after a dot. Thus, the service name http will result in _http._tcp.

Angie resolves the SRV records by combining the normalized service name and the hostname and obtaining the list of servers for the combination via DNS, along with their priorities and weights.

  • Top-priority SRV records (ones that share the minimum priority value) resolve into primary servers, and other records become backup servers. If backup is set with server, top-priority SRV records resolve into backup servers, and other records are ignored.

  • Weight influences the selection of servers by the assigned capacity: higher weights receive more requests. If set by both the server directive and the SRV record, the weight set by server is used.

This example will look up the _http._tcp.backend.example.com record:

server backend.example.com service=http resolve;

Added in version 1.4.0.

slow_start=time

sets the time to recover the weight for a server that goes back online, if load balancing uses the round-robin or least_conn method.

If the value is set and the server is again considered available and healthy as defined by max_fails and upstream_probe, the server will steadily recover its designated weight within the allocated timeframe.

If the value isn’t set, the server in a similar situation will recover its designated weight immediately.

Note

If there’s only one server in an upstream, slow_start has no effect and will be ignored.

zone#

Syntax:

zone name [size];

Default:

Context:

upstream

Defines the name and size of the shared memory zone that keeps the group’s configuration and run-time state that are shared between worker processes. Several groups may share the same zone. In this case, it is enough to specify the size only once.

hash#

Syntax:

hash key [consistent];

Default:

Context:

upstream

Specifies a load balancing method for a server group where the client-server mapping is based on the hashed key value. The key can contain text, variables, and their combinations (1.11.2). Usage example:

hash $remote_addr;

Note that adding or removing a server from the group may result in remapping most of the keys to different servers. The method is compatible with the Cache::Memcached Perl library.

If the consistent parameter is specified, the ketama consistent hashing method will be used instead. The method ensures that only a few keys will be remapped to different servers when a server is added to or removed from the group. This helps to achieve a higher cache hit ratio for caching servers. The method is compatible with the Cache::Memcached::Fast Perl library with the ketama_points parameter set to 160.

least_conn#

Syntax:

least_conn;

Default:

Context:

upstream

Specifies that a group should use a load balancing method where a connection 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 in turn using a weighted round-robin balancing method.

random#

Syntax:

random [two];

Default:

Context:

upstream

Specifies that a group should use a load balancing method where a request is passed to a randomly selected server, taking into account weights of servers.

The optional two parameter instructs Angie to randomly select two servers and then choose a server using the specified method. The default method is least_conn which passes a request to a server with the least number of active connections.

Built-in Variables#

The stream_upstream module supports the following Built-in variables:

$upstream_addr#

keeps the IP address and port, or the path to the UNIX domain socket of the upstream server. If several servers were contacted during request processing, their addresses are separated by commas, e.g. :

192.168.1.1:1935, 192.168.1.2:1935, unix:/tmp/sock

If a server cannot be selected, the variable keeps the name of the server group.

$upstream_bytes_received#

number of bytes received from an upstream server. Values from several connections are separated by commas like addresses in the $upstream_addr variable.

$upstream_bytes_sent#

number of bytes sent to an upstream server. Values from several connections are separated by commas like addresses in the $upstream_addr variable.

$upstream_connect_time#

time to connect to the upstream server; the time is kept in seconds with millisecond resolution. Times of several connections are separated by commas like addresses in the $upstream_addr variable.

$upstream_first_byte_time#

time to receive the first byte of data; the time is kept in seconds with millisecond resolution. Times of several connections are separated by commas like addresses in the $upstream_addr variable.

$upstream_probe_response#

Contents of the response received during an active probe configured by upstream_probe.

$upstream_session_time name#

session duration in seconds with millisecond resolution. Times of several connections are separated by commas like addresses in the $upstream_addr variable.