Path: blob/main/xml/en/docs/stream/ngx_stream_upstream_module.xml
1 views
<?xml version="1.0"?>12<!--3Copyright (C) Nginx, Inc.4-->56<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">78<module name="Module ngx_stream_upstream_module"9link="/en/docs/stream/ngx_stream_upstream_module.html"10lang="en"11rev="42">1213<section id="summary">1415<para>16The <literal>ngx_stream_upstream_module</literal> module (1.9.0)17is used to define groups of servers that can be referenced18by the <link doc="ngx_stream_proxy_module.xml" id="proxy_pass"/>19directive.20</para>2122</section>232425<section id="example" name="Example Configuration">2627<para>28<example>29upstream <emphasis>backend</emphasis> {30hash $remote_addr consistent;3132server backend1.example.com:12345 weight=5;33server backend2.example.com:12345;34server unix:/tmp/backend3;3536server backup1.example.com:12345 backup;37server backup2.example.com:12345 backup;38}3940server {41listen 12346;42proxy_pass <emphasis>backend</emphasis>;43}44</example>45</para>4647<para>48Dynamically configurable group with49periodic <link doc="ngx_stream_upstream_hc_module.xml">health checks</link> is50available as part of our51<commercial_version>commercial subscription</commercial_version>:52<example>53resolver 10.0.0.1;5455upstream <emphasis>dynamic</emphasis> {56zone upstream_dynamic 64k;5758server backend1.example.com:12345 weight=5;59server backend2.example.com:12345 fail_timeout=5s slow_start=30s;60server 192.0.2.1:12345 max_fails=3;61server backend3.example.com:12345 resolve;62server backend4.example.com service=http resolve;6364server backup1.example.com:12345 backup;65server backup2.example.com:12345 backup;66}6768server {69listen 12346;70proxy_pass <emphasis>dynamic</emphasis>;71health_check;72}73</example>74</para>7576</section>777879<section id="directives" name="Directives">8081<directive name="upstream">82<syntax block="yes"><value>name</value></syntax>83<default/>84<context>stream</context>8586<para>87Defines a group of servers.88Servers can listen on different ports.89In addition, servers listening on TCP and UNIX-domain sockets90can be mixed.91</para>9293<para>94Example:95<example>96upstream backend {97server backend1.example.com:12345 weight=5;98server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;99server unix:/tmp/backend2;100server backend3.example.com:12345 resolve;101102server backup1.example.com:12345 backup;103}104</example>105</para>106107<para>108By default, connections are distributed between the servers using a109weighted round-robin balancing method.110In the above example, each 7 connections will be distributed as follows:1115 connections go to <literal>backend1.example.com:12345</literal>112and one connection to each of the second and third servers.113If an error occurs during communication with a server, the connection will114be passed to the next server, and so on until all of the functioning115servers will be tried.116If communication with all servers fails, the connection will be closed.117</para>118119</directive>120121122<directive name="server">123<syntax><value>address</value> [<value>parameters</value>]</syntax>124<default/>125<context>upstream</context>126127<para>128Defines the <value>address</value> and other <value>parameters</value>129of a server.130The address can be specified as a domain name or IP address131with an obligatory port, or as a UNIX-domain socket path132specified after the “<literal>unix:</literal>” prefix.133A domain name that resolves to several IP addresses defines134multiple servers at once.135</para>136137<para>138The following parameters can be defined:139<list type="tag">140141<tag-name id="weight">142<literal>weight</literal>=<value>number</value>143</tag-name>144<tag-desc>145sets the weight of the server, by default, 1.146</tag-desc>147148<tag-name id="max_conns">149<literal>max_conns</literal>=<value>number</value>150</tag-name>151<tag-desc>152limits the maximum <value>number</value> of simultaneous153connections to the proxied server (1.11.5).154Default value is zero, meaning there is no limit.155If the server group does not reside in the <link id="zone">shared memory</link>,156the limitation works per each worker process.157<note>158Prior to version 1.11.5, this parameter was available as part of our159<commercial_version>commercial subscription</commercial_version>.160</note>161</tag-desc>162163<tag-name id="max_fails">164<literal>max_fails</literal>=<value>number</value>165</tag-name>166<tag-desc>167sets the number of unsuccessful attempts to communicate with the server168that should happen in the duration set by the <literal>fail_timeout</literal>169parameter to consider the server unavailable for a duration also set by the170<literal>fail_timeout</literal> parameter.171By default, the number of unsuccessful attempts is set to 1.172The zero value disables the accounting of attempts.173Here, an unsuccessful attempt is an error or timeout174while establishing a connection with the server.175</tag-desc>176177<tag-name id="fail_timeout">178<literal>fail_timeout</literal>=<value>time</value>179</tag-name>180<tag-desc>181sets182<list type="bullet">183184<listitem>185the time during which the specified number of unsuccessful attempts to186communicate with the server should happen to consider the server unavailable;187</listitem>188189<listitem>190and the period of time the server will be considered unavailable.191</listitem>192193</list>194By default, the parameter is set to 10 seconds.195</tag-desc>196197<tag-name id="backup">198<literal>backup</literal>199</tag-name>200<tag-desc>201marks the server as a backup server.202Connections to the backup server will be passed203when the primary servers are unavailable.204<note>205The parameter cannot be used along with the206<link id="hash"/> and <link id="random"/> load balancing methods.207</note>208</tag-desc>209210<tag-name id="down">211<literal>down</literal>212</tag-name>213<tag-desc>214marks the server as permanently unavailable.215</tag-desc>216217<tag-name id="resolve">218<literal>resolve</literal>219</tag-name>220<tag-desc>221monitors changes of the IP addresses222that correspond to a domain name of the server,223and automatically modifies the upstream configuration224without the need of restarting nginx.225The server group must reside in the <link id="zone">shared memory</link>.226<para>227In order for this parameter to work,228the <literal>resolver</literal> directive229must be specified in the230<link doc="ngx_stream_core_module.xml" id="resolver">stream</link> block231or in the corresponding <link id="resolver">upstream</link> block.232</para>233234<para>235<note>236Prior to version 1.27.3, this parameter was available only as part of our237<commercial_version>commercial subscription</commercial_version>.238</note>239</para>240</tag-desc>241242<tag-name id="service">243<literal>service</literal>=<value>name</value>244</tag-name>245<tag-desc>246enables resolving of DNS247<link url="https://datatracker.ietf.org/doc/html/rfc2782">SRV</link>248records and sets the service <value>name</value> (1.9.13).249In order for this parameter to work, it is necessary to specify250the <link id="resolve"/> parameter for the server251and specify a hostname without a port number.252<para>253If the service name does not contain a dot (“<literal>.</literal>”), then254the <link url="https://datatracker.ietf.org/doc/html/rfc2782">RFC</link>-compliant name255is constructed256and the TCP protocol is added to the service prefix.257For example, to look up the258<literal>_http._tcp.backend.example.com</literal> SRV record,259it is necessary to specify the directive:260<example>261server backend.example.com service=http resolve;262</example>263If the service name contains one or more dots, then the name is constructed264by joining the service prefix and the server name.265For example, to look up the <literal>_http._tcp.backend.example.com</literal>266and <literal>server1.backend.example.com</literal> SRV records,267it is necessary to specify the directives:268<example>269server backend.example.com service=_http._tcp resolve;270server example.com service=server1.backend resolve;271</example>272</para>273274<para>275Highest-priority SRV records276(records with the same lowest-number priority value)277are resolved as primary servers,278the rest of SRV records are resolved as backup servers.279If the <link id="backup"/> parameter is specified for the server,280high-priority SRV records are resolved as backup servers,281the rest of SRV records are ignored.282</para>283284<para>285<note>286Prior to version 1.27.3, this parameter was available only as part of our287<commercial_version>commercial subscription</commercial_version>.288</note>289</para>290</tag-desc>291292</list>293</para>294295<para>296Additionally,297the following parameters are available as part of our298<commercial_version>commercial subscription</commercial_version>:299<list type="tag">300301<tag-name id="slow_start">302<literal>slow_start</literal>=<value>time</value>303</tag-name>304<tag-desc>305sets the <value>time</value> during which the server will recover its weight306from zero to a nominal value, when unhealthy server becomes307<link doc="ngx_stream_upstream_hc_module.xml" id="health_check">healthy</link>,308or when the server becomes available after a period of time309it was considered <link id="fail_timeout">unavailable</link>.310Default value is zero, i.e. slow start is disabled.311<note>312The parameter cannot be used along with the313<link id="hash"/> and <link id="random"/> load balancing methods.314</note>315</tag-desc>316317</list>318</para>319320<para>321<note>322If there is only a single server in a group, <literal>max_fails</literal>,323<literal>fail_timeout</literal> and <literal>slow_start</literal> parameters324are ignored, and such a server will never be considered unavailable.325</note>326</para>327328</directive>329330331<directive name="zone">332<syntax><value>name</value> [<value>size</value>]</syntax>333<default/>334<context>upstream</context>335336<para>337Defines the <value>name</value> and <value>size</value> of the shared338memory zone that keeps the group’s configuration and run-time state that are339shared between worker processes.340Several groups may share the same zone.341In this case, it is enough to specify the <value>size</value> only once.342</para>343344<para>345Additionally,346as part of our <commercial_version>commercial subscription</commercial_version>,347such groups allow changing the group membership348or modifying the settings of a particular server349without the need of restarting nginx.350The configuration is accessible via the351<link doc="../http/ngx_http_api_module.xml">API</link> module (1.13.3).352<note>353Prior to version 1.13.3,354the configuration was accessible only via a special location355handled by356<link doc="../http/ngx_http_upstream_conf_module.xml" id="upstream_conf"/>.357</note>358</para>359360</directive>361362363<directive name="state">364<syntax><value>file</value></syntax>365<default/>366<context>upstream</context>367<appeared-in>1.9.7</appeared-in>368369<para>370Specifies a <value>file</value> that keeps the state371of the dynamically configurable group.372</para>373374<para>375Examples:376<example>377state /var/lib/nginx/state/servers.conf; # path for Linux378state /var/db/nginx/state/servers.conf; # path for FreeBSD379</example>380</para>381382<para>383The state is currently limited to the list of servers with their parameters.384The file is read when parsing the configuration and is updated each time385the upstream configuration is386<link doc="../http/ngx_http_api_module.xml" id="stream_upstreams_stream_upstream_name_servers_">changed</link>.387Changing the file content directly should be avoided.388The directive cannot be used389along with the <link id="server"/> directive.390</para>391392<para>393<note>394Changes made during395<link doc="../control.xml" id="reconfiguration">configuration reload</link>396or <link doc="../control.xml" id="upgrade">binary upgrade</link>397can be lost.398</note>399</para>400401<para>402<note>403This directive is available as part of our404<commercial_version>commercial subscription</commercial_version>.405</note>406</para>407408</directive>409410411<directive name="hash">412<syntax><value>key</value> [<literal>consistent</literal>]</syntax>413<default/>414<context>upstream</context>415416<para>417Specifies a load balancing method for a server group418where the client-server mapping is based on the hashed <value>key</value> value.419The <value>key</value> can contain text, variables,420and their combinations (1.11.2).421Usage example:422<example>423hash $remote_addr;424</example>425Note that adding or removing a server from the group426may result in remapping most of the keys to different servers.427The method is compatible with the428<link url="https://metacpan.org/pod/Cache::Memcached">Cache::Memcached</link>429Perl library.430</para>431432<para>433If the <literal>consistent</literal> parameter is specified,434the <link url="https://www.metabrew.com/article/libketama-consistent-hashing-algo-memcached-clients">ketama</link>435consistent hashing method will be used instead.436The method ensures that only a few keys437will be remapped to different servers438when a server is added to or removed from the group.439This helps to achieve a higher cache hit ratio for caching servers.440The method is compatible with the441<link url="https://metacpan.org/pod/Cache::Memcached::Fast">Cache::Memcached::Fast</link>442Perl library with the <value>ketama_points</value> parameter set to 160.443</para>444445</directive>446447448<directive name="least_conn">449<syntax/>450<default/>451<context>upstream</context>452453<para>454Specifies that a group should use a load balancing method where a connection455is passed to the server with the least number of active connections,456taking into account weights of servers.457If there are several such servers, they are tried in turn using a458weighted round-robin balancing method.459</para>460461</directive>462463464<directive name="least_time">465<syntax>466<literal>connect</literal> |467<literal>first_byte</literal> |468<literal>last_byte</literal>469[<literal>inflight</literal>]</syntax>470<default/>471<context>upstream</context>472<appeared-in>1.7.11</appeared-in>473474<para>475Specifies that a group should use a load balancing method where a connection476is passed to the server with the least average time and477least number of active connections, taking into account weights of servers.478If there are several such servers, they are tried in turn using a479weighted round-robin balancing method.480</para>481482<para>483If the <literal>connect</literal> parameter is specified,484time to485<link id="var_upstream_connect_time">connect</link>486to the upstream server is used.487If the <literal>first_byte</literal> parameter is specified,488time to receive the489<link id="var_upstream_first_byte_time">first byte</link> of data is used.490If the <literal>last_byte</literal> is specified,491time to receive the492<link id="var_upstream_session_time">last byte</link> of data is used.493If the <literal>inflight</literal> parameter is specified (1.11.6),494incomplete connections are also taken into account.495<note>496Prior to version 1.11.6,497incomplete connections were taken into account by default.498</note>499</para>500501<para>502<note>503Prior to version 1.31.0,504this directive was available only as part of our505<commercial_version>commercial subscription</commercial_version>.506</note>507</para>508509</directive>510511512<directive name="random">513<syntax>[<literal>two</literal> [<value>method</value>]]</syntax>514<default/>515<context>upstream</context>516<appeared-in>1.15.1</appeared-in>517518<para>519Specifies that a group should use a load balancing method where a connection520is passed to a randomly selected server, taking into account weights521of servers.522</para>523524<para>525The optional <literal>two</literal> parameter526instructs nginx to randomly select527<link url="https://homes.cs.washington.edu/~karlin/papers/balls.pdf">two</link>528servers and then choose a server529using the specified <literal>method</literal>.530The default method is <literal>least_conn</literal>531which passes a connection to a server532with the least number of active connections.533</para>534535<para id="random_least_time">536The <literal>least_time</literal> method passes a connection to a server537with the least average time and least number of active connections.538If <literal>least_time=connect</literal> parameter is specified,539time to540<link id="var_upstream_connect_time">connect</link>541to the upstream server is used.542If <literal>least_time=first_byte</literal> parameter is specified,543time to receive the544<link id="var_upstream_first_byte_time">first byte</link> of data is used.545If <literal>least_time=last_byte</literal> is specified,546time to receive the547<link id="var_upstream_session_time">last byte</link> of data is used.548<note>549The <literal>least_time</literal> method is available as a part of our550<commercial_version>commercial subscription</commercial_version>.551</note>552</para>553554</directive>555556557<directive name="resolver">558<syntax>559<value>address</value> ...560[<literal>valid</literal>=<value>time</value>]561[<literal>ipv4</literal>=<literal>on</literal>|<literal>off</literal>]562[<literal>ipv6</literal>=<literal>on</literal>|<literal>off</literal>]563[<literal>status_zone</literal>=<value>zone</value>]</syntax>564<default/>565<context>upstream</context>566<appeared-in>1.27.3</appeared-in>567568<para>569Configures name servers used to resolve names of upstream servers570into addresses, for example:571<example>572resolver 127.0.0.1 [::1]:5353;573</example>574The address can be specified as a domain name or IP address,575with an optional port.576If port is not specified, the port 53 is used.577Name servers are queried in a round-robin fashion.578</para>579580<para id="resolver_ipv6">581By default, nginx will look up both IPv4 and IPv6 addresses while resolving.582If looking up of IPv4 or IPv6 addresses is not desired,583the <literal>ipv4=off</literal> (1.23.1) or584the <literal>ipv6=off</literal> parameter can be specified.585</para>586587<para id="resolver_valid">588By default, nginx caches answers using the TTL value of a response.589The optional <literal>valid</literal> parameter allows overriding it:590<example>591resolver 127.0.0.1 [::1]:5353 valid=30s;592</example>593<note>594To prevent DNS spoofing, it is recommended595configuring DNS servers in a properly secured trusted local network.596</note>597</para>598599<para id="resolver_status_zone">600The optional <literal>status_zone</literal> parameter (1.17.5)601enables602<link doc="../http/ngx_http_api_module.xml" id="resolvers_">collection</link>603of DNS server statistics of requests and responses604in the specified <value>zone</value>.605The parameter is available as part of our606<commercial_version>commercial subscription</commercial_version>.607</para>608609<para>610<note>611Since version 1.17.5 and prior to version 1.27.3,612this directive was available only as part of our613<commercial_version>commercial subscription</commercial_version>.614</note>615</para>616617</directive>618619620<directive name="resolver_timeout">621<syntax><value>time</value></syntax>622<default>30s</default>623<context>upstream</context>624<appeared-in>1.27.3</appeared-in>625626<para>627Sets a timeout for name resolution, for example:628<example>629resolver_timeout 5s;630</example>631</para>632633<para>634<note>635Since version 1.17.5 and prior to version 1.27.3,636this directive was available only as part of our637<commercial_version>commercial subscription</commercial_version>.638</note>639</para>640641</directive>642643</section>644645646<section id="variables" name="Embedded Variables">647648<para>649The <literal>ngx_stream_upstream_module</literal> module650supports the following embedded variables:651<list type="tag">652653<tag-name id="var_upstream_addr"><var>$upstream_addr</var></tag-name>654<tag-desc>655keeps the IP address and port,656or the path to the UNIX-domain socket of the upstream server (1.11.4).657If several servers were contacted during proxying,658their addresses are separated by commas, e.g.659“<literal>192.168.1.1:12345, 192.168.1.2:12345, unix:/tmp/sock</literal>”.660If a server cannot be selected,661the variable keeps the name of the server group.662</tag-desc>663664<tag-name id="var_upstream_bytes_received"><var>$upstream_bytes_received</var></tag-name>665<tag-desc>666number of bytes received from an upstream server (1.11.4).667Values from several connections668are separated by commas like addresses in the669<link id="var_upstream_addr">$upstream_addr</link> variable.670</tag-desc>671672<tag-name id="var_upstream_bytes_sent"><var>$upstream_bytes_sent</var></tag-name>673<tag-desc>674number of bytes sent to an upstream server (1.11.4).675Values from several connections676are separated by commas like addresses in the677<link id="var_upstream_addr">$upstream_addr</link> variable.678</tag-desc>679680<tag-name id="var_upstream_connect_time"><var>$upstream_connect_time</var></tag-name>681<tag-desc>682time to connect to the upstream server (1.11.4);683the time is kept in seconds with millisecond resolution.684Times of several connections685are separated by commas like addresses in the686<link id="var_upstream_addr">$upstream_addr</link> variable.687</tag-desc>688689<tag-name id="var_upstream_first_byte_time"><var>$upstream_first_byte_time</var></tag-name>690<tag-desc>691time to receive the first byte of data (1.11.4);692the time is kept in seconds with millisecond resolution.693Times of several connections694are separated by commas like addresses in the695<link id="var_upstream_addr">$upstream_addr</link> variable.696</tag-desc>697698<tag-name id="var_upstream_last_addr"><var>$upstream_last_addr</var></tag-name>699<tag-desc>700keeps the IP address or the path to the UNIX-domain socket701of the last selected upstream server (1.29.3).702703<para>704<note>705This variable is available as part of our706<commercial_version>commercial subscription</commercial_version>.707</note>708</para>709710</tag-desc>711712<tag-name id="var_upstream_session_time"><var>$upstream_session_time</var></tag-name>713<tag-desc>714session duration in seconds with millisecond resolution (1.11.4).715Times of several connections716are separated by commas like addresses in the717<link id="var_upstream_addr">$upstream_addr</link> variable.718</tag-desc>719720</list>721</para>722723</section>724725</module>726727728