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.

upstream_probe#

New in version 1.4.0.

Syntax:

upstream_probe name [port=number] [interval=time] [test=condition] [essential] [fails=number] [passes=number] [max_response=size] [mode=always|idle|onfail] [udp] [send=string];

Default:

Context:

server

Defines an active health probe for peers within the upstream groups that are specified for proxy_pass in the same location context with the upstream_probe directive. Subsequently, Angie PRO regularly probes each peer of the upstream group according to the parameters configured here.

A peer’s probe is passed if the request to the peer succeeds, considering all parameter settings of the upstream_probe directive and the settings that control how upstreams are used by the directive’s location context, including the proxy_next_upstream directive.

To make use of the probes, the upstream must have a shared memory zone (zone). One upstream may be configured with several probes.

The following parameters are accepted:

name

Mandatory name of the probe.

port

Alternative port number for the probe request.

interval

Interval between probes.
By default — 5s.

test

The condition for the probe, defined as a string of variables. If the variables’ substitution yields "" or "0", the probe is not passed.

essential

If set, the initial state of the peer is being checked, so the peer doesn’t receive client requests until the probe is passed.

fails

Number of subsequent failed probes that renders the peer unhealthy.
By default — 1.

passes

Number of subsequent passed probes that renders the peer unhealthy.
By default — 1.

max_response

Maximum memory size for the response. If a zero value is specified, response waiting is disabled.
By default — 256k.

mode

Probe mode, depending on the peers’ health:

  • always — peers are probed regardless of their state;

  • idle — probes affect unhealthy peers and peers where interval has elapsed since the last client request.

  • onfail — only unhealthy peers are probed.

By default — always.

udp

If specified, the UDP protocol is used for probing. By default, TCP is used for probing.

send

Data sent for the check; this can be a string with the prefix data: or a file name with data (specified absolutely or relative to the /usr/local/angie/ directory).

Example:

upstream backend {
    zone backend 1m;

    server a.example.com;
    server b.example.com;
}

map $upstream_probe_response $good {
    ~200    "1";
    default  "";
}

server {
    listen ...;

    # ...
    proxy_pass backend;
    upstream_probe_timeout 1s;

    upstream_probe backend_probe
        port=12345
        interval=5s
        test=$good
        essential
        fails=3
        passes=3
        max_response=512k
        mode=onfail
        "send=data:GET / HTTP/1.0\n\n";
}

Details of probe operation:

  • Initially, the peer won’t receive client requests until it passes all essential probes configured for it. If there are no such probes, the peer is considered healthy.

  • The peer is considered unhealthy and won’t receive client requests, if any of the probes configured for it hits fails or the peer reaches max_fails.

  • For an unhealthy peer to be considered healthy again, all probes configured for it must reach their respective passes; after that, max_fails is also considered.

upstream_probe_timeout#

New in version 1.4.0.

Syntax:

upstream_probe_timeout time;

Default:

upstream_probe_timeout 50s;

Context:

server

Sets the maximum inactivity time of an established server connection for probes configured using the upstream_probe directive; if this limit is exceeded, the connection will be closed.

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 the fail_timeout parameter to consider the server unavailable for a duration also set by the fail_timeout parameter.
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 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.

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.

New 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. For this parameter to work, the resolver and resolver_timeout directives should be specified in the upstream block or inherited from the stream.

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.

New 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.

New in version 1.4.0.

state#

Syntax:

state file;

Default:

Context:

upstream

Specifies the file where the upstream’s server list is persisted. When installing from our packages, a designated /var/lib/angie/state/ (/var/db/angie/state/ on FreeBSD) directory with appropriate permissions is created to store these files, so you will only need to add the file’s basename in the configuration:

upstream backend {

    zone backend 1m;
    state /var/lib/angie/state/<FILE NAME>;
}

The format of this server list is similar to server. The contents of the file change whenever there is any modification to servers in the /config/stream/upstreams/ section via the configuration API. The file is read at Angie PRO start or configuration reload.

Caution

For the state directive to be used in an upstream block, the block should have no server directives; instead, it must have a shared memory zone (zone).

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.

least_time#

Syntax:

least_time connect | first_byte | last_byte;

Default:

Context:

upstream

Sets the load balancing method for a group where the probability of sending a request to an active server is inversely proportional to the average time it takes to respond; the smaller the response time, the more requests the server will receive.

In the connect mode, the average time to establish the connection is considered; in the first_byte mode, the average time to receive the first byte of the response is considered; in the last_byte mode, the average time to receive the complete response is considered.

The calculation of averages is controlled by the response_time_factor directive. Current values are presented as connect_time, first_byte_time, and last_byte_time in the health object of the server among the stream upstream metrics in the API.

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 PRO 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.

response_time_factor#

Syntax:

response_time_factor number;

Default:

response_time_factor 90;

Context:

upstream

Sets the smoothing factor for the least_time load balancing method, using the previous value when calculating the average response time according to the formula of the exponential weighted moving average.

The larger the specified number, the less new values influence the average; if 90 is specified, 90% of the previous value will be taken, and only 10% of the new value. Acceptable values range from 0 to 99 inclusive.

Current calculations are presented as connect_time (time to establish the connection), first_byte_time (time to receive the first byte of the response), and last_byte_time (time to receive the complete response) in the health object of the server among the stream upstream metrics in the API.

Note

Only successful responses are considered in the calculation; what constitutes an unsuccessful response is determined by the proxy_next_upstream directives.

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.