view xml/en/docs/http/ngx_http_upstream_module.xml @ 942:ba3d6ade3513

Elaborate on upstream server failures. Documented that {proxy,fastcgi,memcached}_next_upstream directives define what is considered an unsuccessful attempt of communication with a server.
author Ruslan Ermilov <ru@nginx.com>
date Fri, 05 Jul 2013 10:40:38 +0400
parents e26a18eb5ccd
children aded7086e84f
line wrap: on
line source

<?xml version="1.0"?>

<!--
  Copyright (C) Igor Sysoev
  Copyright (C) Nginx, Inc.
  -->

<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">

<module name="Module ngx_http_upstream_module"
        link="/en/docs/http/ngx_http_upstream_module.html"
        lang="en"
        rev="4">

<section id="summary">

<para>
The <literal>ngx_http_upstream_module</literal> module
allows to define groups of servers that can be referenced
from the <link doc="ngx_http_proxy_module.xml" id="proxy_pass"/>,
<link doc="ngx_http_fastcgi_module.xml" id="fastcgi_pass"/>, and
<link doc="ngx_http_memcached_module.xml" id="memcached_pass"/> directives.
</para>

</section>


<section id="example" name="Example Configuration">

<para>
<example>
upstream <emphasis>backend</emphasis> {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://<emphasis>backend</emphasis>;
    }
}
</example>
</para>

</section>


<section id="directives" name="Directives">

<directive name="upstream">
<syntax block="yes"><value>name</value></syntax>
<default/>
<context>http</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 weight=5;
    server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3;
}
</example>
</para>

<para>
By default, requests are distributed between servers using a
weighted round-robin balancing method.
In the above example, each 7 requests will be distributed as follows:
5 requests to <literal>backend1.example.com</literal>
and one request to each of second and third servers.
If an error occurs when communicating with the server, a 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 be returned the result of contacting the last server.
</para>

</directive>


<directive name="server">
<syntax><value>address</value> [<value>parameters</value>]</syntax>
<default/>
<context>upstream</context>

<para>
Defines an <value>address</value> and other <value>parameters</value>
of the server.
An address can be specified as a domain name or IP address,
and an optional port, or as a UNIX-domain socket path
specified after the “<literal>unix:</literal>” prefix.
If port is not specified, the port 80 is used.
A domain name that resolves to several IP addresses essentially defines
multiple servers.
</para>

<para>
The following parameters can be defined:
<list type="tag">

<tag-name><literal>weight</literal>=<value>number</value></tag-name>
<tag-desc>
sets a weight of the server, by default 1.
</tag-desc>

<tag-name><literal>max_fails</literal>=<value>number</value></tag-name>
<tag-desc>
sets a number of unsuccessful attempts to communicate with the server
during a time set by the <literal>fail_timeout</literal> parameter
after which it will be considered down for a period of time also set
by the <literal>fail_timeout</literal> parameter.
By default, the number of unsuccessful attempts is set to 1.
A value of zero disables accounting of attempts.
What is considered an unsuccessful attempt is defined by the
<link doc="ngx_http_proxy_module.xml" id="proxy_next_upstream"/>,
<link doc="ngx_http_fastcgi_module.xml" id="fastcgi_next_upstream"/>, and
<link doc="ngx_http_memcached_module.xml" id="memcached_next_upstream"/>
directives.
</tag-desc>

<tag-name><literal>fail_timeout</literal>=<value>time</value></tag-name>
<tag-desc>
sets
<list type="bullet">

<listitem>
a time during which the specified number of unsuccessful attempts to
communicate with the server should happen for the server to be
considered down;
</listitem>

<listitem>
and a period of time the server will be considered down.
</listitem>

</list>
By default, timeout is set to 10 seconds.
</tag-desc>

<tag-name><literal>backup</literal></tag-name>
<tag-desc>
marks the server as a backup server.
It will be passed requests when the primary servers are down.
</tag-desc>

<tag-name><literal>down</literal></tag-name>
<tag-desc>
marks the server as permanently down; used along with
the <link id="ip_hash"/> directive.
</tag-desc>

</list>
</para>

<para>
Example:
<example>
upstream backend {
    server backend1.example.com     weight=5;
    server 127.0.0.1:8080           max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3;

    server backup1.example.com:8080 backup;
}
</example>
</para>

</directive>


<directive name="ip_hash">
<syntax/>
<default/>
<context>upstream</context>

<para>
Specifies that a group should use a load balancing method where requests
are distributed between servers based on client IP addresses.
The first three octets of the client IPv4 address, or the entire IPv6 address,
are used as a hashing key.
The method ensures that requests of the same client will always be
passed to the same server except when this server is considered down
in which case client requests will be passed to another server and
most probably it will also be the same server.
<note>
IPv6 addresses are supported starting from versions 1.3.2 and 1.2.2.
</note>
</para>

<para>
If one of the servers needs to be temporarily removed, it should
be marked with the <literal>down</literal> parameter in
order to preserve the current hashing of client IP addresses.
</para>

<para>
Example:
<example>
upstream backend {
    ip_hash;

    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com <emphasis>down</emphasis>;
    server backend4.example.com;
}
</example>
</para>

<para>
<note>
Until versions 1.3.1 and 1.2.2 it was not possible to specify a weight for
servers using the <literal>ip_hash</literal> load balancing method.
</note>
</para>

</directive>


<directive name="keepalive">
<syntax><value>connections</value></syntax>
<default/>
<context>upstream</context>
<appeared-in>1.1.4</appeared-in>

<para>
Activates cache of connections to upstream servers.
</para>

<para>
The <value>connections</value> parameter sets the maximum number of
idle keepalive connections to upstream servers that are retained in
the cache per one worker process.
When this number is exceeded, the least recently used connections
are closed.
<note>
It should be particularly noted that <literal>keepalive</literal> directive
does not limit the total number of connections that nginx worker process
can open to upstream servers.
The <value>connections</value> parameter should be set low enough
to allow upstream servers to process additional new incoming
connections as well.
</note>
</para>

<para>
Example configuration of memcached upstream with keepalive connections:
<example>
upstream memcached_backend {
    server 127.0.0.1:11211;
    server 10.0.0.2:11211;

    keepalive 32;
}

server {
    ...

    location /memcached/ {
        set $memcached_key $uri;
        memcached_pass memcached_backend;
    }

}
</example>
</para>

<para>
For HTTP, the <link doc="ngx_http_proxy_module.xml" id="proxy_http_version"/>
directive should be set to “<literal>1.1</literal>”
and the <header>Connection</header> header field should be cleared:
<example>
upstream http_backend {
    server 127.0.0.1:8080;

    keepalive 16;
}

server {
    ...

    location /http/ {
        proxy_pass http://http_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        ...
    }
}
</example>
</para>

<para>
<note>
Alternatively, HTTP/1.0 persistent connections can be used by passing the
<header>Connection: Keep-Alive</header> header field to an upstream server,
though this is not recommended.
</note>
</para>

<para>
For FastCGI servers, it is required to set
<link doc="ngx_http_fastcgi_module.xml" id="fastcgi_keep_conn"/>
for keepalive connections to work:
<example>
upstream fastcgi_backend {
    server 127.0.0.1:9000;

    keepalive 8;
}

server {
    ...

    location /fastcgi/ {
        fastcgi_pass fastcgi_backend;
        fastcgi_keep_conn on;
        ...
    }
}
</example>
</para>

<para>
<note>
When using load balancer methods other than the default
round-robin, it is necessary to activate them before
the <literal>keepalive</literal> directive.
</note>

<note>
SCGI and uwsgi protocols do not have a notion of keepalive connections.
</note>
</para>

</directive>


<directive name="least_conn">
<syntax/>
<default/>
<context>upstream</context>
<appeared-in>1.3.1</appeared-in>
<appeared-in>1.2.2</appeared-in>

<para>
Specifies that a group should use a load balancing method where a request
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 using a
weighted round-robin balancing method.
</para>

</directive>

</section>


<section id="variables" name="Embedded Variables">

<para>
The <literal>ngx_http_upstream_module</literal> module
supports the following embedded variables:
<list type="tag">

<tag-name><var>$upstream_addr</var></tag-name>
<tag-desc>
keeps an IP address and port of the server,
or a path to the UNIX-domain socket.
If several servers were contacted during request processing,
their addresses are separated by commas, e.g.
“<literal>192.168.1.1:80, 192.168.1.2:80, unix:/tmp/sock</literal>”.
If an internal redirect from one server group to another happened
using
<header>X-Accel-Redirect</header> or
<link doc="ngx_http_core_module.xml" id="error_page"/>
then these server groups are separated by colons, e.g.
“<literal>192.168.1.1:80, 192.168.1.2:80, unix:/tmp/sock : 192.168.10.1:80, 192.168.10.2:80</literal>”.
</tag-desc>

<tag-name><var>$upstream_cache_status</var></tag-name>
<tag-desc>
keeps status of accessing a response cache (0.8.3).
The status can be one of “<literal>MISS</literal>”, 
“<literal>BYPASS</literal>”, “<literal>EXPIRED</literal>”,
“<literal>STALE</literal>”, “<literal>UPDATING</literal>” or
“<literal>HIT</literal>”.
</tag-desc>

<tag-name><var>$upstream_response_length</var></tag-name>
<tag-desc>
keeps lengths of responses obtained from upstream servers (0.7.27);
lengths are kept in bytes.
Several responses are separated by commas and colons
like in the <var>$upstream_addr</var> variable.
</tag-desc>

<tag-name><var>$upstream_response_time</var></tag-name>
<tag-desc>
keeps times of responses obtained from upstream servers;
times are kept in seconds with a milliseconds resolution.
Several responses are separated by commas and colons
like in the <var>$upstream_addr</var> variable.
</tag-desc>

<tag-name><var>$upstream_status</var></tag-name>
<tag-desc>
keeps codes of responses obtained from upstream servers.
Several responses are separated by commas and colons
like in the <var>$upstream_addr</var> variable.
</tag-desc>

<tag-name><var>$upstream_http_...</var></tag-name>
<tag-desc>
keep server response header fields.
For example, the <header>Server</header> response header field
is made available through the <var>$upstream_http_server</var> variable.
The rules of converting header field names to variable names are the same
as for variables starting with the
“<link doc="ngx_http_core_module.xml" id="variables">$http_</link>” prefix.
Only the last server’s response header fields are saved.
</tag-desc>

</list>
</para>

</section>

</module>