Mercurial > hg > nginx-site
view xml/en/docs/stream/ngx_stream_upstream_module.xml @ 3099:9cfda14d0109 default tip
freenginx-1.27.4
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 03 Sep 2024 13:15:18 +0300 |
parents | 9eadb98ec770 |
children |
line wrap: on
line source
<?xml version="1.0"?> <!-- Copyright (C) Nginx, Inc. --> <!DOCTYPE module SYSTEM "../../../../dtd/module.dtd"> <module name="Module ngx_stream_upstream_module" link="/en/docs/stream/ngx_stream_upstream_module.html" lang="en" rev="40"> <section id="summary"> <para> The <literal>ngx_stream_upstream_module</literal> module (1.9.0) is used to define groups of servers that can be referenced by the <link doc="ngx_stream_proxy_module.xml" id="proxy_pass"/> directive. </para> </section> <section id="example" name="Example Configuration"> <para> <example> upstream <emphasis>backend</emphasis> { 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 <emphasis>backend</emphasis>; } </example> </para> </section> <section id="directives" name="Directives"> <directive name="upstream"> <syntax block="yes"><value>name</value></syntax> <default/> <context>stream</context> <para> Defines a group of servers. Servers can listen on different ports. In addition, servers listening on TCP and UNIX-domain sockets can be mixed. </para> <para> Example: <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; } </example> </para> <para> 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 <literal>backend1.example.com:12345</literal> 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. </para> </directive> <directive name="server"> <syntax><value>address</value> [<value>parameters</value>]</syntax> <default/> <context>upstream</context> <para> Defines the <value>address</value> and other <value>parameters</value> 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 “<literal>unix:</literal>” prefix. A domain name that resolves to several IP addresses defines multiple servers at once. </para> <para> The following parameters can be defined: <list type="tag"> <tag-name id="weight"> <literal>weight</literal>=<value>number</value> </tag-name> <tag-desc> sets the weight of the server, by default, 1. </tag-desc> <tag-name id="max_conns"> <literal>max_conns</literal>=<value>number</value> </tag-name> <tag-desc> limits the maximum <value>number</value> 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 <link id="zone">shared memory</link>, the limitation works per each worker process. </tag-desc> <tag-name id="max_fails"> <literal>max_fails</literal>=<value>number</value> </tag-name> <tag-desc> sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the <literal>fail_timeout</literal> parameter to consider the server unavailable for a duration also set by the <literal>fail_timeout</literal> 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. </tag-desc> <tag-name id="fail_timeout"> <literal>fail_timeout</literal>=<value>time</value> </tag-name> <tag-desc> sets <list type="bullet"> <listitem> the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable; </listitem> <listitem> and the period of time the server will be considered unavailable. </listitem> </list> By default, the parameter is set to 10 seconds. </tag-desc> <tag-name id="backup"> <literal>backup</literal> </tag-name> <tag-desc> marks the server as a backup server. Connections to the backup server will be passed when the primary servers are unavailable. <note> The parameter cannot be used along with the <link id="hash"/> and <link id="random"/> load balancing methods. </note> </tag-desc> <tag-name id="down"> <literal>down</literal> </tag-name> <tag-desc> marks the server as permanently unavailable. </tag-desc> </list> </para> <para> <note> If there is only a single server in a group, <literal>max_fails</literal> and <literal>fail_timeout</literal> parameters are ignored, and such a server will never be considered unavailable. </note> </para> </directive> <directive name="zone"> <syntax><value>name</value> [<value>size</value>]</syntax> <default/> <context>upstream</context> <para> Defines the <value>name</value> and <value>size</value> 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 <value>size</value> only once. </para> </directive> <directive name="hash"> <syntax><value>key</value> [<literal>consistent</literal>]</syntax> <default/> <context>upstream</context> <para> Specifies a load balancing method for a server group where the client-server mapping is based on the hashed <value>key</value> value. The <value>key</value> can contain text, variables, and their combinations (1.11.2). Usage example: <example> hash $remote_addr; </example> 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 <link url="https://metacpan.org/pod/Cache::Memcached">Cache::Memcached</link> Perl library. </para> <para> If the <literal>consistent</literal> parameter is specified, the <link url="https://www.metabrew.com/article/libketama-consistent-hashing-algo-memcached-clients">ketama</link> 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 <link url="https://metacpan.org/pod/Cache::Memcached::Fast">Cache::Memcached::Fast</link> Perl library with the <value>ketama_points</value> parameter set to 160. </para> </directive> <directive name="least_conn"> <syntax/> <default/> <context>upstream</context> <para> 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. </para> </directive> <directive name="random"> <syntax>[<literal>two</literal> [<value>method</value>]]</syntax> <default/> <context>upstream</context> <appeared-in>1.15.1</appeared-in> <para> 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. </para> <para> The optional <literal>two</literal> parameter instructs nginx to randomly select <link url="https://homes.cs.washington.edu/~karlin/papers/balls.pdf">two</link> servers and then choose a server using the specified <literal>method</literal>. The default method is <literal>least_conn</literal> which passes a connection to a server with the least number of active connections. </para> </directive> </section> <section id="variables" name="Embedded Variables"> <para> The <literal>ngx_stream_upstream_module</literal> module supports the following embedded variables: <list type="tag"> <tag-name id="var_upstream_addr"><var>$upstream_addr</var></tag-name> <tag-desc> 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. “<literal>192.168.1.1:12345, 192.168.1.2:12345, unix:/tmp/sock</literal>”. If a server cannot be selected, the variable keeps the name of the server group. </tag-desc> <tag-name id="var_upstream_bytes_received"><var>$upstream_bytes_received</var></tag-name> <tag-desc> number of bytes received from an upstream server (1.11.4). Values from several connections are separated by commas like addresses in the <link id="var_upstream_addr">$upstream_addr</link> variable. </tag-desc> <tag-name id="var_upstream_bytes_sent"><var>$upstream_bytes_sent</var></tag-name> <tag-desc> number of bytes sent to an upstream server (1.11.4). Values from several connections are separated by commas like addresses in the <link id="var_upstream_addr">$upstream_addr</link> variable. </tag-desc> <tag-name id="var_upstream_connect_time"><var>$upstream_connect_time</var></tag-name> <tag-desc> 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 <link id="var_upstream_addr">$upstream_addr</link> variable. </tag-desc> <tag-name id="var_upstream_first_byte_time"><var>$upstream_first_byte_time</var></tag-name> <tag-desc> 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 <link id="var_upstream_addr">$upstream_addr</link> variable. </tag-desc> <tag-name id="var_upstream_session_time"><var>$upstream_session_time</var></tag-name> <tag-desc> session duration in seconds with millisecond resolution (1.11.4). Times of several connections are separated by commas like addresses in the <link id="var_upstream_addr">$upstream_addr</link> variable. </tag-desc> </list> </para> </section> </module>