=encoding utf-8 =head1 NAME ngx_http_upstream_module - Module ngx_http_upstream_module =head1 The C module is used to define groups of servers that can be referenced by the L, L, L, L, L, and L directives. =head1 Example Configuration upstream backend { 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://backend; } } Dynamically configurable group with periodic L is available as part of our commercial subscription: resolver 10.0.0.1; upstream dynamic { zone upstream_dynamic 64k; server backend1.example.com weight=5; server backend2.example.com:8080 fail_timeout=5s slow_start=30s; server 192.0.2.1 max_fails=3; server backend3.example.com resolve; server backend4.example.com service=http resolve; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; } server { location / { proxy_pass http://dynamic; health_check; } } =head1 Directives =head2 upstream B upstream I> { B<...> } > B I 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 weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; server backup1.example.com backup; } By default, requests are distributed between the servers using a weighted round-robin balancing method. In the above example, each 7 requests will be distributed as follows: 5 requests go to C and one request to each of the second and third servers. If an error occurs during communication with a server, the 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 receive the result of the communication with the last server. =head2 server B server I> [I>]> B I Defines the I> and other I> of a server. The address can be specified as a domain name or IP address, with an optional port, or as a UNIX-domain socket path specified after the “C” prefix. If a port is not specified, the port 80 is used. A domain name that resolves to several IP addresses defines multiple servers at once. The following parameters can be defined: =over =item C=I> sets the weight of the server, by default, 1. =item C=I> limits the maximum I> of simultaneous active 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. B If idle keepalive connections, multiple L, and the shared memory are enabled, the total number of active and idle connections to the proxied server may exceed the C value. B Since version 1.5.9 and prior to version 1.11.5, this parameter was available as part of our commercial subscription. =item C=I> sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the C parameter to consider the server unavailable for a duration also set by the C parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts. What is considered an unsuccessful attempt is defined by the L, L, L, L, L, and L directives. =item C=I> 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 marks the server as a backup server. It will be passed requests when the primary servers are unavailable. B The parameter cannot be used along with the L, L, and L load balancing methods. =item C marks the server as permanently unavailable. =item C 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 (1.5.12). The server group must reside in the shared memory. In order for this parameter to work, the C directive must be specified in the L block or in the corresponding upstream block. B Prior to version 1.27.3, this parameter was available only as part of our commercial subscription. =item C=I> enables resolving of DNS L records and sets the service I> (1.9.13). In order for this parameter to work, it is necessary to specify the L 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-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 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 parameter is specified for the server, high-priority SRV records are resolved as backup servers, the rest of SRV records are ignored. B Prior to version 1.27.3, this parameter was available only as part of our commercial subscription. =back Additionally, the following parameters are available as part of our commercial subscription: =over =item C=I> sets the server route name. =item C=I> sets the I> during which the server will recover its weight from zero to a nominal value, when unhealthy server becomes L, 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. B The parameter cannot be used along with the L, L, and L load balancing methods. =item C puts the server into the “draining” mode (1.13.6). In this mode, only requests bound to the server will be proxied to it. B Prior to version 1.13.6, the parameter could be changed only with the L module. =back B If there is only a single server in a group, C, C and C parameters are ignored, and such a server will never be considered unavailable. =head2 zone B zone I> [I>]> B I This directive appeared in version 1.9.0. Defines the I> and I> 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 I> 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 the L module (1.13.3). B Prior to version 1.13.3, the configuration was accessible only via a special location handled by L. =head2 state B state I>> B I This directive appeared in version 1.9.7. Specifies a I> that keeps the state of the dynamically configurable group. Examples: state /var/lib/nginx/state/servers.conf; # path for Linux state /var/db/nginx/state/servers.conf; # path for FreeBSD 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. Changing the file content directly should be avoided. The directive cannot be used along with the L directive. B Changes made during L or L can be lost. B This directive is available as part of our commercial subscription. =head2 hash B hash I> [C]> B I This directive appeared in version 1.7.2. Specifies a load balancing method for a server group where the client-server mapping is based on the hashed I> value. The I> can contain text, variables, and their combinations. 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 Perl library. If the C parameter is specified, the L 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 Perl library with the I> parameter set to 160. =head2 ip_hash B I 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 from the same client will always be passed to the same server except when this server is unavailable. In the latter case client requests will be passed to another server. Most probably, it will always be the same server as well. B IPv6 addresses are supported starting from versions 1.3.2 and 1.2.2. If one of the servers needs to be temporarily removed, it should be marked with the C parameter in order to preserve the current hashing of client IP addresses. Example: upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com down; server backend4.example.com; } B Until versions 1.3.1 and 1.2.2, it was not possible to specify a weight for servers using the C load balancing method. =head2 keepalive B keepalive I>> B I This directive appeared in version 1.1.4. Activates the cache for connections to upstream servers. The I> parameter sets the maximum number of idle keepalive connections to upstream servers that are preserved in the cache of each worker process. When this number is exceeded, the least recently used connections are closed. B It should be particularly noted that the C directive does not limit the total number of connections to upstream servers that an nginx worker process can open. The I> parameter should be set to a number small enough to let upstream servers process new incoming connections as well. B When using load balancing methods other than the default round-robin method, it is necessary to activate them before the C directive. Example configuration of memcached upstream with keepalive connections: 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; } } For HTTP, the L directive should be set to “C<1.1>” and the C header field should be cleared: 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 ""; ... } } B Alternatively, HTTPE1.0 persistent connections can be used by passing the C header field to an upstream server, though this method is not recommended. For FastCGI servers, it is required to set L for keepalive connections to work: upstream fastcgi_backend { server 127.0.0.1:9000; keepalive 8; } server { ... location /fastcgi/ { fastcgi_pass fastcgi_backend; fastcgi_keep_conn on; ... } } B SCGI and uwsgi protocols do not have a notion of keepalive connections. =head2 keepalive_requests B keepalive_requests I>> B I<1000> B I This directive appeared in version 1.15.3. Sets the maximum number of requests that can be served through one keepalive connection. After the maximum number of requests is made, the connection is closed. Closing connections periodically is necessary to free per-connection memory allocations. Therefore, using too high maximum number of requests could result in excessive memory usage and not recommended. B Prior to version 1.19.10, the default value was 100. =head2 keepalive_time B keepalive_time I>> B I<1h> B I This directive appeared in version 1.19.10. Limits the maximum time during which requests can be processed through one keepalive connection. After this time is reached, the connection is closed following the subsequent request processing. =head2 keepalive_timeout B keepalive_timeout I>> B I<60s> B I This directive appeared in version 1.15.3. Sets a timeout during which an idle keepalive connection to an upstream server will stay open. =head2 ntlm B I This directive appeared in version 1.9.2. Allows proxying requests with L. The upstream connection is bound to the client connection once the client sends a request with the C header field value starting with “C” or “C”. Further client requests will be proxied through the same upstream connection, keeping the authentication context. In order for NTLM authentication to work, it is necessary to enable keepalive connections to upstream servers. The L directive should be set to “C<1.1>” and the C header field should be cleared: upstream http_backend { server 127.0.0.1:8080; ntlm; } server { ... location /http/ { proxy_pass http://http_backend; proxy_http_version 1.1; proxy_set_header Connection ""; ... } } B When using load balancer methods other than the default round-robin method, it is necessary to activate them before the C directive. B This directive is available as part of our commercial subscription. =head2 least_conn B I This directive appeared in version 1.3.1. This directive appeared in version 1.2.2. 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 in turn using a weighted round-robin balancing method. =head2 least_time B least_time I< C
E C [C]> B I This directive appeared in version 1.7.10. Specifies that a group should use a load balancing method where a request is passed to the server with the least average response 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
parameter is specified, time to receive the response header is used. If the C parameter is specified, time to receive the full response is used. If the C parameter is specified (1.11.6), incomplete requests are also taken into account. B Prior to version 1.11.6, incomplete requests were taken into account by default. B This directive is available as part of our commercial subscription. =head2 queue B queue I< I> [C=I>]> B I This directive appeared in version 1.5.12. If an upstream server cannot be selected immediately while processing a request, the request will be placed into the queue. The directive specifies the maximum I> of requests that can be in the queue at the same time. If the queue is filled up, or the server to pass the request to cannot be selected within the time period specified in the C parameter, the C<502> (C) error will be returned to the client. The default value of the C parameter is 60 seconds. B When using load balancer methods other than the default round-robin method, it is necessary to activate them before the C directive. B This directive is available as part of our commercial subscription. =head2 random B random I<[C [I>]]> B I This directive appeared in version 1.15.1. Specifies that a group should use a load balancing method where a request is passed to a randomly selected server, taking into account weights of servers. The optional C parameter instructs nginx to randomly select L servers and then choose a server using the specified C. The default method is C which passes a request to a server with the least number of active connections. The C method passes a request to a server with the least average response time and least number of active connections. If C is specified, the time to receive the response header is used. If C is specified, the time to receive the full response is used. B The C method is available as a part of our commercial subscription. =head2 resolver B resolver I< I> ... [C=I>] [C=CEC] [C=CEC] [C=I>]> B I This directive appeared in version 1.27.3. Configures name servers used to resolve names of upstream servers into addresses, for example: resolver 127.0.0.1 [::1]:5353; The address can be specified as a domain name or IP address, with an optional port. If port is not specified, the port 53 is used. Name servers are queried in a round-robin fashion. By default, nginx will look up both IPv4 and IPv6 addresses while resolving. If looking up of IPv4 or IPv6 addresses is not desired, the C (1.23.1) or the C parameter can be specified. By default, nginx caches answers using the TTL value of a response. An optional C parameter allows overriding it: resolver 127.0.0.1 [::1]:5353 valid=30s; B To prevent DNS spoofing, it is recommended configuring DNS servers in a properly secured trusted local network. The optional C parameter (1.17.5) enables L of DNS server statistics of requests and responses in the specified I>. The parameter is available as part of our commercial subscription. B Since version 1.17.5 and prior to version 1.27.3, this directive was available only as part of our commercial subscription. =head2 resolver_timeout B resolver_timeout I>> B I<30s> B I This directive appeared in version 1.27.3. Sets a timeout for name resolution, for example: resolver_timeout 5s; B Since version 1.17.5 and prior to version 1.27.3, this directive was available only as part of our commercial subscription. =head2 sticky B sticky I< C I> [CI>] [CI>] [C] [CCECECEI>] [C] [CI>]> B sticky I< C I> ...> B sticky I< C CI> CI> CI>:I> [CI>] [C
] [C]> B I This directive appeared in version 1.5.7. Enables session affinity, which causes requests from the same client to be passed to the same server in a group of servers. Three methods are available: =over =item C When the C method is used, information about the designated server is passed in an HTTP cookie generated by nginx: upstream backend { server backend1.example.com; server backend2.example.com; sticky cookie srv_id expires=1h domain=.example.com path=/; } 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 with this cookie will be passed to the designated server. If the designated server cannot process a request, the new server is selected as if the client has not been bound yet. B As a load balancing method always tries to evenly distribute the load considering already bound requests, the server with a higher number of active bound requests has less possibility of getting new unbound requests. The first parameter sets the name of the cookie to be set or inspected. The cookie value is a hexadecimal representation of the MD5 hash of the IP address and port, or of the UNIX-domain socket path. However, if the “C” parameter of the L directive is specified, the cookie value will be the value of the “C” parameter: upstream backend { server backend1.example.com route=a; server backend2.example.com route=b; sticky cookie srv_id expires=1h domain=.example.com path=/; } In this case, the value of the “C” cookie will be either I> or I>. Additional parameters may be as follows: =over =item CI> Sets the I> for which a browser should keep the cookie. The special value C will cause the cookie to expire on “C<31 Dec 2037 23:55:55 GMT>”. If the parameter is not specified, it will cause the cookie to expire at the end of a browser session. =item CI> Defines the I> for which the cookie is set. Parameter value can contain variables (1.11.5). =item C Adds the C attribute to the cookie (1.7.11). =item CC E C E C E I> Adds the C (1.19.4) attribute to the cookie with one of the following values: C, C, C, or using variables (1.23.3). In the latter case, if the variable value is empty, the C attribute will not be added to the cookie, if the value is resolved to C, C, or C, the corresponding value will be assigned, otherwise the C value will be assigned. =item C Adds the C attribute to the cookie (1.7.11). =item CI> Defines the I> for which the cookie is set. =back If any parameters are omitted, the corresponding cookie fields are not set. =item C When the C method is used, proxied server assigns client a route on receipt of the first request. All subsequent requests from this client will carry routing information in a cookie or URI. This information is compared with the “C” parameter of the L directive to identify the server to which the request should be proxied. If the “C” parameter is not specified, the route name will be a hexadecimal representation of the MD5 hash of the IP address and port, or of the UNIX-domain socket path. If the designated server cannot process a request, the new server is selected by the configured balancing method as if there is no routing information in the request. The parameters of the C method specify variables that may contain routing information. The first non-empty variable is used to find the matching server. Example: map $cookie_jsessionid $route_cookie { ~.+\.(?P\w+)$ $route; } map $request_uri $route_uri { ~jsessionid=.+\.(?P\w+)$ $route; } upstream backend { server backend1.example.com route=a; server backend2.example.com route=b; sticky route $route_cookie $route_uri; } Here, the route is taken from the “C” cookie if present in a request. Otherwise, the route from the URI is used. =item C When the C method (1.7.1) is used, nginx analyzes upstream server responses and learns server-initiated sessions usually passed in an HTTP cookie. upstream backend { server backend1.example.com:8080; server backend2.example.com:8081; sticky learn create=$upstream_cookie_examplecookie lookup=$cookie_examplecookie zone=client_sessions:1m; } In the example, the upstream server creates a session by setting the cookie “C” in the response. Further requests with this cookie will be passed to the same server. If the server cannot process the request, the new server is selected as if the client has not been bound yet. The parameters C and C specify variables that indicate how new sessions are created and existing sessions are searched, respectively. Both parameters may be specified more than once, in which case the first non-empty variable is used. Sessions are stored in a shared memory zone, whose I> and I> are configured by the C parameter. One megabyte zone can store about 4000 sessions on the 64-bit platform. The sessions that are not accessed during the time specified by the C parameter get removed from the zone. By default, C is set to 10 minutes. The C
parameter (1.13.1) allows creating a session right after receiving response headers from the upstream server. The C parameter (1.13.8) enables L of the shared memory zone. =back B This directive is available as part of our commercial subscription. =head2 sticky_cookie_insert B sticky_cookie_insert I> [CI>] [CI>] [CI>]> B I This directive is obsolete since version 1.5.7. An equivalent L directive with a new syntax should be used instead: B C I> [CI>] [CI>] [CI>]; =head1 Embedded Variables The C module supports the following embedded variables: =over =item C<$upstream_addr> keeps the IP address and port, or the path to the UNIX-domain socket of the upstream server. If several servers were contacted during request processing, their addresses are separated by commas, e.g. “C<192.168.1.1:80, 192.168.1.2:80, unix:EtmpEsock>”. If an internal redirect from one server group to another happens, initiated by C or L, then the server addresses from different groups are separated by colons, e.g. “C<192.168.1.1:80, 192.168.1.2:80, unix:EtmpEsock : 192.168.10.1:80, 192.168.10.2:80>”. If a server cannot be selected, the variable keeps the name of the server group. =item C<$upstream_bytes_received> number of bytes received from an upstream server (1.11.4). Values from several connections are separated by commas and colons like addresses in the $upstream_addr variable. =item C<$upstream_bytes_sent> number of bytes sent to an upstream server (1.15.8). Values from several connections are separated by commas and colons like addresses in the $upstream_addr variable. =item C<$upstream_cache_status> keeps the status of accessing a response cache (0.8.3). The status can be either “C”, “C”, “C”, “C”, “C”, “C”, or “C”. =item C<$upstream_connect_time> keeps time spent on establishing a connection with the upstream server (1.9.1); the time is kept in seconds with millisecond resolution. In case of SSL, includes time spent on handshake. Times of several connections are separated by commas and colons like addresses in the $upstream_addr variable. =item C<$upstream_cookie_>I> cookie with the specified I> sent by the upstream server in the C response header field (1.7.1). Only the cookies from the response of the last server are saved. =item C<$upstream_header_time> keeps time spent on receiving the response header from the upstream server (1.7.10); the time is kept in seconds with millisecond resolution. Times of several responses are separated by commas and colons like addresses in the $upstream_addr variable. =item C<$upstream_http_>I> keep server response header fields. For example, the C response header field is available through the C<$upstream_http_server> variable. The rules of converting header field names to variable names are the same as for the variables that start with the “L<$http_|ngx_http_core_module>” prefix. Only the header fields from the response of the last server are saved. =item C<$upstream_last_server_name> keeps the name of last selected upstream server (1.25.3); allows passing it L: proxy_ssl_server_name on; proxy_ssl_name $upstream_last_server_name; B This variable is available as part of our commercial subscription. =item C<$upstream_queue_time> keeps time the request spent in the upstream queue (1.13.9); the time is kept in seconds with millisecond resolution. Times of several responses are separated by commas and colons like addresses in the $upstream_addr variable. =item C<$upstream_response_length> keeps the length of the response obtained from the upstream server (0.7.27); the length is kept in bytes. Lengths of several responses are separated by commas and colons like addresses in the $upstream_addr variable. =item C<$upstream_response_time> keeps time spent on receiving the response from the upstream server; the time is kept in seconds with millisecond resolution. Times of several responses are separated by commas and colons like addresses in the $upstream_addr variable. =item C<$upstream_status> keeps status code of the response obtained from the upstream server. Status codes of several responses are separated by commas and colons like addresses in the $upstream_addr variable. If a server cannot be selected, the variable keeps the C<502> (C) status code. =item C<$upstream_trailer_>I> keeps fields from the end of the response obtained from the upstream server (1.13.10). =back