=encoding utf-8

=head1 NAME

ngx_stream_upstream_module - Module ngx_stream_upstream_module




=head1



The C<ngx_stream_upstream_module> module (1.9.0)
is used to define groups of servers that can be referenced
by the L<ngx_stream_proxy_module>
directive.




=head1 Example Configuration




    
    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>;
    }







Dynamically configurable group,
available as part of our
commercial subscription:

    
    resolver 10.0.0.1;
    
    upstream <emphasis>dynamic</emphasis> {
        zone upstream_dynamic 64k;
    
        server backend1.example.com:12345 weight=5;
        server backend2.example.com:12345 fail_timeout=5s slow_start=30s;
        server 192.0.2.1:12345            max_fails=3;
        server backend3.example.com:12345 resolve;
        server backend4.example.com       service=http resolve;
    
        server backup1.example.com:12345  backup;
        server backup2.example.com:12345  backup;
    }
    
    server {
        listen 12346;
        proxy_pass <emphasis>dynamic</emphasis>;
        health_check;
    }






=head1 Directives

=head2 upstream


B<syntax:> upstream I<I<C<name>> { B<...> } >



B<context:> I<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 C<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.







=head2 server


B<syntax:> server I<I<C<address>> [I<C<parameters>>]>



B<context:> I<upstream>





Defines the I<C<address>> and other I<C<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 “C<unix:>” prefix.
A domain name that resolves to several IP addresses defines
multiple servers at once.





The following parameters can be defined:

=over



=item 
C<weight>=I<C<number>>





sets the weight of the server, by default, 1.



=item 
C<max_fails>=I<C<number>>





sets the number of unsuccessful attempts to communicate with the server
that should happen in the duration set by the C<fail_timeout>
parameter to consider the server unavailable for a duration also set by the
C<fail_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.



=item 
C<fail_timeout>=I<C<time>>





sets

=over




=item *

the time during which the specified number of unsuccessful attempts to
communicate with the server should happen to consider the server unavailable;



=item *

and the period of time the server will be considered unavailable.



=back


By default, the parameter is set to 10 seconds.



=item 
C<backup>





marks the server as a backup server.
Connections to the backup server will be passed
when the primary servers are unavailable.



=item 
C<down>





marks the server as permanently unavailable.




=back







Additionally,
the following parameters are available as part of our
commercial subscription:

=over



=item 
C<max_conns>=I<C<number>>





limits the maximum I<C<number>> of simultaneous connections to the
proxied server.
Default value is zero, meaning there is no limit.



=item 
C<resolve>





monitors changes of the IP addresses
that correspond to a domain name of the server,
and automatically modifies the upstream configuration
without the need of restarting nginx.
The server group must reside in the shared memory.


In order for this parameter to work,
the L<ngx_stream_core_module> directive
must be specified in the
L<ngx_stream_core_module> block.
Example:

    
    stream {
        resolver 10.0.0.1;
    
        upstream u {
            zone ...;
            ...
            server example.com:12345 resolve;
        }
    }







=item 
C<service>=I<C<name>>





enables resolving of DNS
L<SRV|https://tools.ietf.org/html/rfc2782>
records and sets the service I<C<name>> (1.9.13).
In order for this parameter to work, it is necessary to specify
the L</resolve> parameter for the server
and specify a hostname without a port number.


If the service name does not contain a dot (“C<.>”), then
the L<RFC|https://tools.ietf.org/html/rfc2782>-compliant name
is constructed
and the TCP protocol is added to the service prefix.
For example, to look up the
C<_http._tcp.backend.example.com> SRV record,
it is necessary to specify the directive:

    
    server backend.example.com service=http resolve;


If the service name contains one or more dots, then the name is constructed
by joining the service prefix and the server name.
For example, to look up the C<_http._tcp.backend.example.com>
and C<server1.backend.example.com> SRV records,
it is necessary to specify the directives:

    
    server backend.example.com service=_http._tcp resolve;
    server example.com service=server1.backend resolve;







Highest-priority SRV records
(records with the same lowest-number priority value)
are resolved as primary servers,
the rest of SRV records are resolved as backup servers.
If the L</backup> parameter is specified for the server,
high-priority SRV records are resolved as backup servers,
the rest of SRV records are ignored.





=item 
C<slow_start>=I<C<time>>





sets the I<C<time>> during which the server will recover its weight
from zero to a nominal value,
or when the server becomes available after a period of time
it was considered unavailable.
Default value is zero, i.e. slow start is disabled.




=back








B<NOTE>

If there is only a single server in a group, C<max_fails>,
C<fail_timeout> and C<slow_start> parameters
are ignored, and such a server will never be considered unavailable.








=head2 zone


B<syntax:> zone I<I<C<name>> [I<C<size>>]>



B<context:> I<upstream>





Defines the I<C<name>> and I<C<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 zone size only once.





Additionally,
as part of our commercial subscription,
such groups allow changing the group membership
or modifying the settings of a particular server
without the need of restarting nginx.
The configuration is accessible via a special location
handled by
L<ngx_http_upstream_conf_module>.







=head2 state


B<syntax:> state I<I<C<file>>>



B<context:> I<upstream>



This directive appeared in version 1.9.7.





Specifies a I<C<file>> that keeps the state
of the dynamically configurable group.
The state is currently limited to the list of servers with their parameters.
The file is read when parsing the configuration and is updated each time
the upstream configuration is
L<changed|ngx_http_upstream_conf_module>.
Changing the file content directly should be avoided.
The directive cannot be used
along with the L</server> directive.






B<NOTE>

Changes made during
L<configuration reload|control>
or L<binary upgrade|control>
can be lost.







B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 hash


B<syntax:> hash I<I<C<key>> [C<consistent>]>



B<context:> I<upstream>





Specifies a load balancing method for a server group
where client-server mapping is based on the hashed I<C<key>> value.
Currently, the only supported value for the C<key>
is the client remote address specified as C<$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
L<Cache::Memcached|http://search.cpan.org/perldoc?Cache%3A%3AMemcached>
Perl library.





If the C<consistent> parameter is specified,
the L<ketama|http://www.last.fm/user/RJ/journal/2007/04/10/392555/>
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
L<Cache::Memcached::Fast|http://search.cpan.org/perldoc?Cache%3A%3AMemcached%3A%3AFast>
Perl library with the I<C<ketama_points>> parameter set to 160.







=head2 least_conn




B<context:> I<upstream>





Specifies that a server 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.







=head2 least_time


B<syntax:> least_time I<C<connect> E<verbar>
     C<first_byte> E<verbar>
     C<last_byte>>



B<context:> I<upstream>





Specifies that a group should use a load balancing method where a connection
is passed to the server with the least average time and
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.





If the C<connect> parameter is specified,
time to connect to the upstream server is used.
If the C<first_byte> parameter is specified,
time to receive the first byte of data is used.
If the C<last_byte> is specified,
time to receive the last byte of data is used.






B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 health_check


B<syntax:> health_check I<[I<C<parameters>>]>



B<context:> I<server>





Enables periodic health checks of the servers in a
group.





The following optional parameters are supported:

=over



=item 
C<interval>=I<C<time>>





sets the interval between two consecutive health checks,
by default, 5 seconds;



=item 
C<fails>=I<C<number>>





sets the number of consecutive failed health checks of a particular server
after which this server will be considered unhealthy,
by default, 1;



=item 
C<passes>=I<C<number>>





sets the number of consecutive passed health checks of a particular server
after which the server will be considered healthy,
by default, 1;



=item 
C<match>=I<C<name>>





specifies the C<match> block configuring the tests that a
successful connection should pass in order for a health check to pass.
By default,
only the ability to establish a TCP connection with the server is checked;



=item 
C<port>=I<C<number>>





defines the port used when connecting to a server
to perform a health check (1.9.7);
by default, equals the L</server> port;



=item 
C<udp>





specifies that the C<UDP> protocol should be used for
health checks instead of the default C<TCP> protocol (1.9.13);
requires a match block with the
send and expect
parameters.




=back







For example,

    
    server {
        proxy_pass backend;
        health_check;
    }


will check the ability to establish a TCP connection to each server
in the C<backend> group every five seconds.
When a connection to the server cannot be established,
the health check will fail, and the server will
be considered unhealthy.
Client connections are not passed to unhealthy servers.





Health checks can also be configured to test data obtained from the server.
Tests are configured separately using the L</match> directive
and referenced in the C<match> parameter.





The server group must reside in the shared memory.





If several health checks are defined for the same group of servers,
a single failure of any check will make the corresponding server be
considered unhealthy.






B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 health_check_timeout


B<syntax:> health_check_timeout I<I<C<timeout>>>


B<default:> I<5s>


B<context:> I<stream>


B<context:> I<server>





Overrides the
L<ngx_stream_proxy_module>
value for health checks.






B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 match


B<syntax:> match I<I<C<name>>  { B<...> } >



B<context:> I<stream>





Defines the named test set used to verify server responses to health checks.





The following parameters can be configured:

=over



=item 
C<send> I<C<string>>;





sends a I<C<string>> to the server;



=item 
C<expect> I<C<string>> E<verbar>
C<~> I<C<regex>>;





a literal string (1.9.12) or a regular expression
that the data obtained from the server should match.
The regular expression is specified with the preceding
“C<~*>” modifier (for case-insensitive matching), or the
“C<~>” modifier (for case-sensitive matching).




=back


Both C<send> and C<expect> parameters
can contain hexadecimal literals with the prefix “C<\x>”
followed by two hex digits, for example, “C<\x80>” (1.9.12).





Health check is passed if:

=over



=item *

the TCP connection was successfully established;



=item *

the I<C<string>> from the C<send> parameter,
if specified, was sent;



=item *

the data obtained from the server matched the string or regular expression
from the C<expect> parameter, if specified;



=item *

the time elapsed does not exceed the value specified
in the L</health_check_timeout> directive.



=back







Example:

    
    upstream backend {
        zone     upstream_backend 10m;
        server   127.0.0.1:12345;
    }
    
    match http {
        send     "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n";
        expect ~ "200 OK";
    }
    
    server {
        listen       12346;
        proxy_pass   backend;
        health_check match=http;
    }








B<NOTE>

Only the first
L<ngx_stream_proxy_module>
bytes of data obtained from the server are examined.







B<NOTE>

This directive is available as part of our
commercial subscription.








