Mercurial > hg > nginx-site
view xml/en/docs/http/ngx_http_upstream_module.xml @ 960:48ab154edf10
Upstream: removed obsoleted directives in the "sticky" module.
The "sticky" and "sticky_log_requests" directives were removed. The sticky
feature description has been revised.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Thu, 08 Aug 2013 14:51:40 +0400 |
parents | 488a3f738db0 |
children | f3754c623e6b |
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="7"> <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> <para> Dynamically configurable group, available as part of our <commercial_version/> only: <example> upstream <emphasis>appservers</emphasis> { zone appservers 64k; server appserv1.example.com weight=5; server appserv2.example.com:8080 fail_timeout=5s slow_start=30s; server 192.0.2.1 max_fails=3; server reserve1.example.com:8080 backup; server reserve2.example.com:8080 backup; } server { location / { proxy_pass http://<emphasis>appservers</emphasis>; health_check; } location /upstream_conf { upstream_conf; allow 127.0.0.1; deny all; } } </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>slow_start</literal>=<value>time</value></tag-name> <tag-desc> sets the <value>time</value> during which the server will recover its weight from zero to a nominal value, when unhealthy server becomes <link id="health_check">healthy</link>, or when the server becomes available after a period of time it was considered down. Default value is zero, i.e. slow start is disabled. <note> This functionality is available as part of our <commercial_version/> only. </note> </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="zone"> <syntax><value>name</value> <value>size</value></syntax> <default/> <context>upstream</context> <para> Makes the group dynamically configurable. Defines the <value>name</value> and <value>size</value> of a shared memory zone that keeps group’s configuration and run-time state that are shared between worker processes. Such groups allow to add, remove, and modify servers at run time. The configuration is accessible via a special location handled by <link id="upstream_conf"/>. </para> <para> <note> This directive is available as part of our <commercial_version/> only. </note> </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> <directive name="health_check"> <syntax> [<literal>interval=</literal><value>time</value>] <!-- [<literal>jitter=</literal><value>time</value>] --> [<literal>fails=</literal><value>number</value>] [<literal>passes=</literal><value>number</value>] [<literal>uri=</literal><value>uri</value>] [<literal>match=</literal><value>name</value>]</syntax> <default/> <context>location</context> <para> Enables periodic health checks of the servers in a <link id="upstream">group</link> referenced in the surrounding location. </para> <para> The following optional parameters are supported: <list type="bullet"> <listitem> <literal>interval</literal> sets the interval between two consecutive health checks, by default, 5 seconds; </listitem> <listitem> <literal>fails</literal> sets the number of consecutive failed health checks of a particular server after which this server will be considered unhealthy, by default, 1; </listitem> <listitem> <literal>passes</literal> sets the number of consecutive passed health checks of a particular server after which the server will be considered healthy, by default, 1; </listitem> <listitem> <literal>uri</literal> defines the URI used in health check requests, by default, “<literal>/</literal>”; </listitem> <listitem> <literal>match</literal> specifies the <literal>match</literal> block configuring the tests that a response should pass in order for a health check to pass; by default, the response should have status code 2xx or 3xx. </listitem> </list> </para> <para> For example, <example> location / { proxy_pass http://backend; health_check; } </example> will send “<literal>/</literal>” requests to each server in the <literal>backend</literal> group every five seconds. If any communication error or timeout occurs, or a proxied server responds with the status code other than 2xx or 3xx, the health check will fail, and the server will be considered unhealthy. Client requests are not passed to unhealthy servers. </para> <para> Health checks can be configured to test the status code of a response, presence of certain header fields and their values, and the body contents. Tests are configured separately using the <link id="match"/> directive and referenced in the <literal>match</literal> parameter. For example: <example> http { server { ... location / { proxy_pass http://backend; health_check match=welcome; } } match welcome { status 200; header Content-Type = text/html; body ~ "Welcome to nginx!"; } } </example> This configuration tells that for a health check to pass, the response to a health check request should succeed, have status 200, content type “<literal>text/html</literal>”, and contain “<literal>Welcome to nginx!</literal>” in the body. </para> <para> The server group must reside in the <link id="zone">shared memory</link>. </para> <para> 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. </para> <para> <note> This directive is available as part of our <commercial_version/> only. </note> </para> </directive> <directive name="match"> <syntax block="yes"><value>name</value></syntax> <default/> <context>http</context> <para> Defines the named test set used to verify responses to health check requests. </para> <para> The following items can be tested in a response: <list type="tag"> <tag-name><literal>status 200;</literal></tag-name> <tag-desc>status is 200</tag-desc> <tag-name><literal>status ! 500;</literal></tag-name> <tag-desc>status is not 500</tag-desc> <tag-name><literal>status 200 204;</literal></tag-name> <tag-desc>status is 200 or 204</tag-desc> <tag-name><literal>status ! 301 302;</literal></tag-name> <tag-desc>status is neither 301 nor 302</tag-desc> <tag-name><literal>status 200-399;</literal></tag-name> <tag-desc>status is in the 200..399 range</tag-desc> <tag-name><literal>status ! 400-599;</literal></tag-name> <tag-desc>status is not in the 400..599 range</tag-desc> <tag-name><literal>status 301-303 307;</literal></tag-name> <tag-desc>status is either 301, 302, 303, or 307</tag-desc> </list> <list type="tag"> <tag-name><literal>header Content-Type = text/html;</literal></tag-name> <tag-desc> header contains <header>Content-Type</header> with value <literal>text/html</literal> </tag-desc> <tag-name><literal>header Content-Type != text/html;</literal></tag-name> <tag-desc> header contains <header>Content-Type</header> with value other than <literal>text/html</literal> </tag-desc> <tag-name><literal>header Connection ~ close;</literal></tag-name> <tag-desc> header contains <header>Connection</header> with value matching regular expression <literal>close</literal> </tag-desc> <tag-name><literal>header Connection !~ close;</literal></tag-name> <tag-desc> header contains <header>Connection</header> with value not matching regular expression <literal>close</literal> </tag-desc> <tag-name><literal>header Host;</literal></tag-name> <tag-desc>header contains <header>Host</header></tag-desc> <tag-name><literal>header ! X-Accel-Redirect;</literal></tag-name> <tag-desc>header lacks <header>X-Accel-Redirect</header></tag-desc> </list> <list type="tag"> <tag-name><literal>body ~ "Welcome to nginx!";</literal></tag-name> <tag-desc> body matches regular expression “<literal>Welcome to nginx!</literal>” </tag-desc> <tag-name><literal>body !~ "Welcome to nginx!";</literal></tag-name> <tag-desc> body does not match regular expression “<literal>Welcome to nginx!</literal>” </tag-desc> </list> </para> <para> If several tests are specified, the response matches only if it matches all tests. <note> Only the first 256k of the response body are examined. </note> </para> <para> Examples: <example> # status is 200, content type is "text/html", # and body contains "Welcome to nginx!" match welcome { status 200; header Content-Type = text/html; body ~ "Welcome to nginx!"; } </example> <example> # status is not one of 301, 302, 303, or 307, and header does not have "Refresh:" match not_redirect { status ! 301-303 307; header ! Refresh; } </example> <example> # status ok and not in maintenance mode match server_ok { status 200-399; body !~ "maintenance mode"; } </example> </para> <para> <note> This directive is available as part of our <commercial_version/> only. </note> </para> </directive> <directive name="sticky_cookie_insert"> <syntax><value>name</value> [<literal>expires=</literal><value>time</value>] [<literal>domain=</literal><value>host</value>] [<literal>path=</literal><value>path</value>]</syntax> <default/> <context>upstream</context> <para> Enables session affinity that causes requests from the same client to be passed to the same server in a group of servers. Example: <example> upstream backend { server backend1.example.com; server backend2.example.com; sticky_cookie_insert srv_id expires=1h domain=example.com path=/; } </example> </para> <para> A request that comes from a client not yet bound to a particular server is passed to the server selected by the configured balancing method. Further requests from the same client are passed to the same server. If a request cannot be processed by the bound server, the new server is selected as if the client was not yet bound. </para> <para> Information about the bound server is kept in HTTP cookie. The first parameter sets the name of the cookie to be inserted or inspected. Additional parameters may be as follows: <list type="tag"> <tag-name><literal>expires</literal></tag-name> <tag-desc> Sets the time for which a browser should keep the cookie. The parameter <literal>max</literal> will cause the cookie to expire on “<literal>31 Dec 2037 23:55:55 GMT</literal>”. This is the maximum time understood by old browsers. If the parameter is not specified, it will cause the cookie to expire at the end of a browser session. </tag-desc> <tag-name><literal>domain</literal></tag-name> <tag-desc> Defines the domain for which the cookie is set. </tag-desc> <tag-name><literal>path</literal></tag-name> <tag-desc> Defines the path for which the cookie is set. </tag-desc> </list> If some parameter is omitted, the corresponding cookie field is not set. </para> <para> <note> This directive is available as part of our <commercial_version/> only. </note> </para> </directive> <directive name="upstream_conf"> <syntax/> <default/> <context>location</context> <para> Turns on the HTTP interface of upstream configuration in the surrounding location. Access to this location should be <link doc="ngx_http_core_module.xml" id="satisfy">limited</link>. </para> <para> Configuration commands can be used to: <list type="bullet"> <listitem>view all primary or backup servers in a group;</listitem> <listitem>view an individual server;</listitem> <listitem>modify an individual server;</listitem> <listitem>add a new server (see the note below);</listitem> <listitem>remove an individual server.</listitem> </list> <note> As noted in the <link id="server"/> directive, specifying a server as a domain name may result in several servers being added to the group. Since addresses in a group are not required to be unique, individual servers in a group can be uniquely referenced to only by their ID. IDs are assigned automatically and shown on viewing of the group configuration. </note> </para> <para> A configuration command consists of parameters passed as request arguments, for example: <example> http://127.0.0.1/upstream_conf?upstream=appservers </example> </para> <para> The following parameters are supported: <list type="tag" compact="no"> <tag-name> <literal>upstream=</literal><value>name</value></tag-name> <tag-desc> Selects a group. This parameter is mandatory. </tag-desc> <tag-name> <literal>backup=</literal> </tag-name> <tag-desc> If not set, selects primary servers in the group. If set, selects backup servers in the group. </tag-desc> <tag-name> <literal>id=</literal><value>number</value></tag-name> <tag-desc> Selects an individual primary or backup server in the group. </tag-desc> <tag-name> <literal>remove=</literal></tag-name> <tag-desc> Removes an individual primary or backup server from the group. </tag-desc> <tag-name> <literal>add=</literal></tag-name> <tag-desc> Adds a new primary or backup server to the group. </tag-desc> <tag-name> <literal>server=</literal><value>address</value></tag-name> <tag-desc> Same as the “<literal>address</literal>” parameter of the <link id="server"/> directive. </tag-desc> <tag-name> <literal>weight=</literal><value>number</value></tag-name> <tag-desc> Same as the “<literal>weight</literal>” parameter of the <link id="server"/> directive. </tag-desc> <tag-name> <literal>max_fails=</literal><value>number</value></tag-name> <tag-desc> Same as the “<literal>max_fails</literal>” parameter of the <link id="server"/> directive. </tag-desc> <tag-name> <literal>fail_timeout=</literal><value>time</value></tag-name> <tag-desc> Same as the “<literal>fail_timeout</literal>” parameter of the <link id="server"/> directive. </tag-desc> <tag-name> <literal>slow_start=</literal><value>time</value></tag-name> <tag-desc> Same as the “<literal>slow_start</literal>” parameter of the <link id="server"/> directive. </tag-desc> <tag-name> <literal>down=</literal></tag-name> <tag-desc> Same as the “<literal>down</literal>” parameter of the <link id="server"/> directive. </tag-desc> <tag-name> <literal>up=</literal></tag-name> <tag-desc> The opposite of the “<literal>down</literal>” parameter of the <link id="server"/> directive. </tag-desc> </list> The first three parameters select a target the command applies to. Without other parameters, the command shows configuration of the selected target. </para> <para> For example, to view the primary servers in the group, send: <example> http://127.0.0.1/upstream_conf?upstream=appservers </example> To view the backup servers in the group, send: <example> http://127.0.0.1/upstream_conf?upstream=appservers&backup= </example> To view an individual primary server in the group, send: <example> http://127.0.0.1/upstream_conf?upstream=appservers&id=42 </example> To view an individual backup server in the group, send: <example> http://127.0.0.1/upstream_conf?upstream=appservers&backup=&id=42 </example> </para> <para> To add a new primary or backup server to the group, specify its address in the “<literal>server=</literal>” parameter. Without other parameters specified, a server will be added with other parameters set to their default values (see the <link id="server"/> directive). </para> <para> For example, to add a new primary server to the group, send: <example> http://127.0.0.1/upstream_conf?add=&upstream=appservers&server=127.0.0.1:8080 </example> To add a new backup server to the group, send: <example> http://127.0.0.1/upstream_conf?add=&upstream=appservers&backup=&server=127.0.0.1:8080 </example> To add a new primary server to the group, set its parameters to non-default values and mark it as “<literal>down</literal>” by sending: <example> http://127.0.0.1/upstream_conf?add=&upstream=appservers&server=127.0.0.1:8080&weight=2&max_fails=3&fail_timeout=3s&down= </example> </para> <para> To remove an individual primary or backup server from the group, select it with the <literal>id=</literal> parameter. </para> <para> For example, to remove an individual primary server from the group, send: <example> http://127.0.0.1/upstream_conf?remove=&upstream=appservers&id=42 </example> To remove an individual backup server from the group, send: <example> http://127.0.0.1/upstream_conf?remove=&upstream=appservers&backup=&id=42 </example> </para> <para> To modify an individual primary or backup server in the group, select it with the <literal>id=</literal> parameter. </para> <para> For example, to modify an individual primary server in the group by marking it as “<literal>down</literal>”, send: <example> http://127.0.0.1/upstream_conf?upstream=appservers&id=42&down= </example> To modify the address of an individual backup server in the group, send: <example> http://127.0.0.1/upstream_conf?upstream=appservers&backup=&id=42&server=192.0.2.3:8123 </example> To modify other parameters of an individual primary server in the group, send: <example> http://127.0.0.1/upstream_conf?upstream=appservers&id=42&max_fails=3&weight=4 </example> </para> <para> <note> This directive is available as part of our <commercial_version/> only. </note> </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>