Module ngx_stream_upstream_module
Example Configuration Directives upstream server zone hash least_conn random Embedded Variables |
The ngx_stream_upstream_module
module (1.9.0)
is used to define groups of servers that can be referenced
by the proxy_pass
directive.
Example Configuration
upstream backend { hash $remote_addr consistent; server backend1.example.com:12345 weight=5; server backend2.example.com:12345; server unix:/tmp/backend3; server backup1.example.com:12345 backup; server backup2.example.com:12345 backup; } server { listen 12346; proxy_pass backend; }
Directives
Syntax: |
upstream |
---|---|
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:12345 weight=5; server 127.0.0.1:12345 max_fails=3 fail_timeout=30s; server unix:/tmp/backend2; server backend3.example.com:12345 resolve; server backup1.example.com:12345 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:12345
and one connection to each of the second and third servers.
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.
Syntax: |
server |
---|---|
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 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. -
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 thefail_timeout
parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts. Here, an unsuccessful attempt is an error or timeout while establishing a connection with the server. -
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.
-
backup
-
marks the server as a backup server.
Connections to the backup server will be passed
when the primary servers are unavailable.
The parameter cannot be used along with the hash and random load balancing methods.
-
down
- marks the server as permanently unavailable.
If there is only a single server in a group,max_fails
andfail_timeout
parameters are ignored, and such a server will never be considered unavailable.
Syntax: |
zone |
---|---|
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.
Syntax: |
hash |
---|---|
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.
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.
Syntax: |
random [ |
---|---|
Default: | — |
Context: |
upstream |
This directive appeared in version 1.15.1.
Specifies that a group should use a load balancing method where a connection is passed to a randomly selected server, taking into account weights of servers.
The optional two
parameter
instructs nginx to randomly select
two
servers and then choose a server
using the specified method
.
The default method is least_conn
which passes a connection to a server
with the least number of active connections.
Embedded Variables
The ngx_stream_upstream_module
module
supports the following embedded variables:
$upstream_addr
-
keeps the IP address and port,
or the path to the UNIX-domain socket of the upstream server (1.11.4).
If several servers were contacted during proxying,
their addresses are separated by commas, e.g.
“
192.168.1.1:12345, 192.168.1.2:12345, 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 (1.11.4). 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 (1.11.4). 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 (1.11.4); 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 (1.11.4); 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_session_time
- session duration in seconds with millisecond resolution (1.11.4). Times of several connections are separated by commas like addresses in the $upstream_addr variable.