Upstream#
Provides context for describing groups of servers that can be used in the proxy_pass directive.
Session persistence and feedback-based load balancing (PRO)
are provided by the separate Upstream Sticky and
Upstream Feedback modules,
whose directives also go in the upstream block.
Related guides
Load Balancing: HTTP balancing without
ip_hash.Console Light Web Panel: shows only groups with a zone.
Configuration Example#
upstream backend {
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#
backup_switch (PRO)#
Added in version 1.10.0: PRO
The directive enables the ability to start server selection not from the primary group, but from the active group, i.e., the one where a server was successfully found previously. If a server cannot be found in the active group for the next request, and the search moves to the backup group, this backup group becomes active, and subsequent requests are first directed to servers in this group.
| If the |
Example:
upstream media_backend {
zone media_backend 1m;
server primary1.example.com:1935;
server primary2.example.com:1935;
server reserve1.example.com:1935 backup;
server reserve2.example.com:1935 backup;
backup_switch permanent=2m;
}
If the load balancer switches from primary servers to the backup group, all subsequent requests are handled by this backup group for 2 minutes. After 2 minutes expire, the load balancer re-checks the primary servers and makes them active again if they are working normally.
hash#
When building from the source code, support for this
directive can be disabled with the build option
--without-stream_upstream_hash_module.
Specifies a load balancing method for the group where client-server mapping is determined using a hashed key value. The key can contain variables. Note that any addition or removal of servers from the group may result in remapping of most keys to different servers. The method is compatible with the Perl Cache::Memcached library.
Usage example:
hash $remote_addr;
| If the |
A name in the server directive can resolve to several addresses,
either when the configuration is loaded or through the
resolve parameter.
With consistent, the method hashes the name, not its addresses (with
the service parameter, it hashes the target names of the SRV records):
a connection whose key maps to a name is passed to one of that name's addresses
by weighted round-robin balancing, so the key is not tied
to any single address.
Without consistent, each address counts as a separate server, but Angie
does not sort the addresses, so the address a key reaches can differ between
Angie instances.
To tie keys to individual addresses, see the
load-balancing guide.
least_conn#
When building from the source code, support for this
directive can be disabled with the build option
--without-stream_upstream_least_conn_module.
Specifies a load balancing method for the group where a connection is passed to the server with the least number of active connections, taking into account server weights. If several servers are suitable, they are selected cyclically (round-robin) with their weights taken into account.
least_time#
| |
Default | — |
upstream |
When building from the source code, support for this
directive can be disabled with the build option
--without-stream_upstream_least_time_module.
Specifies a load balancing method for the group where the probability of passing a connection to an active server is inversely proportional to its average response time; the lower it is, the more connections the server will receive.
| The directive takes into account the average time to establish a connection. |
| The directive uses the average time to receive the first byte of the response. |
| The directive uses the average time to receive the full response. |
| Performs the same function as response_time_factor, and overrides it if the parameter is specified. |
| Specifies a condition variable
that controls which connections are accounted for in the calculation.
The average value is updated
only if the connection's condition variable
is not equal to Note By default, probes (PRO)
are not included in the calculation;
combining the $upstream_probe variable
with |
Current values are presented as connect_time, first_byte_time,
and last_byte_time in the server's health object
among upstream metrics in the API.
no_port (PRO)#
Added in version 1.12.0: PRO
Explicitly marks the server group as portless: its server addresses are specified without a port, and the connect port is instead supplied by each proxy_pass directive that references the group.
The directive is intended for groups that list no servers statically and are instead populated dynamically through the configuration API. Such groups require a shared memory zone and by default are treated as requiring ports. A single portless group can then be reused for proxying to different ports without duplicating the server list:
upstream backend {
zone backend 1m;
no_port;
}
server {
listen 8443;
proxy_pass backend:8443;
}
server {
listen 8080;
proxy_pass backend:8080;
}
A group is also treated as portless without this directive
when its server addresses
are domain names or IP addresses without ports.
The no_port directive states this property explicitly
and rejects addresses that include a port.
A portless group accepts only domain names and IP addresses without ports;
UNIX domain socket addresses and servers that use the service=
parameter for DNS SRV resolution are not allowed.
Domain names and IP addresses in one group must either all include ports
or all omit them; mixed port usage is not allowed.
random#
When building from the source code, support for this
directive can be disabled with the build option
--without-stream_upstream_random_module.
Specifies a load balancing method for the group where a connection is passed to a randomly selected server, taking into account server weights.
| Angie randomly selects two servers and then passes the connection to whichever of them has the least number of active connections per unit of weight. |
| Names the method that chooses between the two servers selected with
|
response_time_factor#
Specifies for the least_time load balancing method the smoothing factor for the previous value when calculating the average response time using the exponentially weighted moving average formula.
The larger the specified number, the less new values affect the average; if
90 is specified, 90% of the previous value will be taken and only 10% of
the new value. Valid values are from 0 to 99 inclusive.
Current calculation results are presented as connect_time (time
to establish a connection), first_byte_time (time to receive the first
byte of the response), and last_byte_time (time to receive the full response) in
the server's health object among 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.
server#
Defines the address and other parameters of a server. The address can be
specified as a domain name or IP address, 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. For a domain name or IP address,
the port is required except in a portless group (PRO),
where it is omitted and supplied by the proxy_pass directive instead.
The following parameters can be defined:
| Sets the weight of the server; by default, 1. |
| Limits the maximum number of simultaneous active connections to the proxied server. Default value is |
| 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 a If an upstream contains only one server
after all its
|
| Sets the period of time during which a specified number of unsuccessful attempts to communicate with the server (max_fails) should happen to consider the server unavailable. The server then remains unavailable for the same amount of time before it is retried. By default, this is set to 10 seconds. Note If a If an upstream contains only one server
after all its |
| Marks the server as a backup server. It will be passed requests when the primary servers are unavailable. |
| Marks the server as permanently unavailable. |
| Marks the server as draining; this means
it receives only requests from the sessions
that were bound earlier with sticky.
Otherwise it behaves similarly to |
Warning
The backup parameter cannot be used along with the hash and random load balancing methods.
The down and drain parameters are mutually exclusive.
| Enables monitoring changes to the list of IP addresses that corresponds to a domain name, updating it without a configuration reload. The group must reside in a shared memory zone; also, a resolver must be defined. |
| Enables resolving DNS SRV records and sets the service name. For this parameter to work, the resolve parameter must also be specified, without specifying the server port in the hostname. If there are no dots in the service name,
the name is formed according to the RFC standard:
the service name is prefixed with 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.
|
This example will look up the _http._tcp.backend.example.com record:
server backend.example.com service=http resolve;
| Sets the server ID in the group. If the parameter is not specified, the ID is set as a hexadecimal MD5 hash of the IP address and port or UNIX domain socket path. |
| Same as |
| Sets the time for a server to recover its weight when returning to service with round-robin or least_conn load balancing methods. If the parameter is set and a server is again considered healthy after a failure according to max_fails and upstream_probe (PRO), the server gradually recovers its designated weight over the specified time period. If the parameter is not set, in a similar situation the server immediately starts working with its designated weight. |
Note
If only one server is specified in the upstream,
slow_start has no effect and will be ignored.
state (PRO)#
Specifies the file where the upstream server list is persistently stored.
When installing from
our packages,
a dedicated directory
/var/lib/angie/state/ (/var/db/angie/state/ on FreeBSD)
is created with appropriate permissions for storing such files,
so you only need to add the filename in the configuration:
upstream backend {
zone backend 1m;
state /var/lib/angie/state/<FILE_NAME>;
}
The server list format here is similar to server. The file contents change whenever servers are modified in the /config/stream/upstreams/ section via the configuration API. The file is read at Angie startup or configuration reload.
Warning
To use the state directive in an upstream block,
there should be no server directives in it,
but a shared memory zone (zone) is required.
upstream#
Describes 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 {
zone backend 1m;
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, connections are distributed between the servers using a weighted round-robin balancing method. In the above example, each 7 connections will be distributed as follows: 5 connections go to backend1.example.com:1935 and one connection to each of the second and third servers. The distribution is smooth: a higher-weight server's connections are spread across the rotation rather than sent in a single burst.
If an error occurs during communication with a server, the connection will be passed to the next server, and so on until all of the functioning servers will be tried. If communication with all servers fails, the connection will be closed.
Note
By default, a server that fails an occasional attempt without reaching max_fails temporarily receives a smaller share of connections, regaining its full share over the connections that follow. This differs from slow_start, which ramps a server back up only after it has been marked unavailable and has recovered.
zone#
When building from the source code, support for this
directive can be disabled with the build option
--without-stream_upstream_zone_module.
Defines the name and size of the shared memory zone that stores the group's configuration and runtime state, shared between worker processes. Multiple groups can use the same zone. In this case, it is sufficient to specify the size only once.
Note
A configuration reload creates the zone again whether or not size
changes: counters, passive-check state, and accumulated balancing history
start from zero. The addresses resolved for servers with resolve
carry over to the new zone, and in Angie PRO, so does the health flag
that active checks with the persistent parameter rely on.
Note
Upstream metrics are collected only when this zone is configured. Without it, the group does not appear in /status/stream/upstreams/<upstream>, the TCP/UDP Upstreams Widget, or Prometheus output, and no warning is logged.
Built-in Variables#
The stores the IP address and port, or the path to the UNIX domain socket of the upstream server. If several servers were contacted during proxying, 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. number of bytes received from an upstream server. Values from several connections are separated by commas and colons like addresses in the $upstream_addr variable. number of bytes sent to an upstream server. Values from several connections are separated by commas and colons like addresses in the $upstream_addr variable. time to connect to the upstream server; the time is kept in seconds with millisecond resolution. Times of several connections are separated by commas and colons like addresses in the $upstream_addr variable. address of the proxied server selected for the current connection attempt;
unlike $upstream_addr, which accumulates the addresses of every server
tried across retries, this variable always holds a single address and is
available immediately after the server is selected, including while
proxy_bind variables are evaluated. 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. session duration in seconds with millisecond resolution. Times of several connections are separated by commas like addresses in the $upstream_addr variable. Status of connections in groups that use sticky. Connection routed to a server group where sticky is not enabled. Connection does not contain sticky information. Connection with sticky information routed to the desired server. Connection with sticky information routed to a server
selected by the load balancing algorithm. Values from multiple connections are separated by commas and colons,
similar to addresses in the $upstream_addr variable.stream_upstream module supports the following built-in variables:$upstream_addr#$upstream_bytes_received#$upstream_bytes_sent#$upstream_connect_time#$upstream_current_addr (PRO)#$upstream_first_byte_time#$upstream_session_time#$upstream_sticky_status#""NEWHITMISS