Path: blob/main/xml/en/docs/stream/ngx_stream_proxy_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_proxy_module"9link="/en/docs/stream/ngx_stream_proxy_module.html"10lang="en"11rev="38">1213<section id="summary">1415<para>16The <literal>ngx_stream_proxy_module</literal> module (1.9.0) allows proxying17data streams over TCP, UDP (1.9.13), and UNIX-domain sockets.18</para>1920</section>212223<section id="example" name="Example Configuration">2425<para>26<example>27server {28listen 127.0.0.1:12345;29proxy_pass 127.0.0.1:8080;30}3132server {33listen 12345;34proxy_connect_timeout 1s;35proxy_timeout 1m;36proxy_pass example.com:12345;37}3839server {40listen 53 udp reuseport;41proxy_timeout 20s;42proxy_pass dns.example.com:53;43}4445server {46listen [::1]:12345;47proxy_pass unix:/tmp/stream.socket;48}49</example>50</para>5152</section>535455<section id="directives" name="Directives">5657<directive name="proxy_bind">58<syntax>59<value>address</value>60[<literal>transparent</literal>] |61<literal>off</literal></syntax>62<default/>63<context>stream</context>64<context>server</context>65<appeared-in>1.9.2</appeared-in>6667<para>68Makes outgoing connections to a proxied server originate69from the specified local IP <value>address</value>.70Parameter value can contain variables (1.11.2).71The special value <literal>off</literal> cancels the effect72of the <literal>proxy_bind</literal> directive73inherited from the previous configuration level, which allows the74system to auto-assign the local IP address.75</para>7677<para id="proxy_bind_transparent">78The <literal>transparent</literal> parameter (1.11.0) allows79outgoing connections to a proxied server originate80from a non-local IP address,81for example, from a real IP address of a client:82<example>83proxy_bind $remote_addr transparent;84</example>85In order for this parameter to work,86it is usually necessary to run nginx worker processes with the87<link doc="../ngx_core_module.xml" id="user">superuser</link> privileges.88On Linux it is not required (1.13.8) as if89the <literal>transparent</literal> parameter is specified, worker processes90inherit the <literal>CAP_NET_RAW</literal> capability from the master process.91It is also necessary to configure kernel routing table92to intercept network traffic from the proxied server.93</para>9495</directive>969798<directive name="proxy_bind_dynamic">99<syntax><literal>on</literal> | <literal>off</literal></syntax>100<default>off</default>101<context>stream</context>102<context>server</context>103<appeared-in>1.29.3</appeared-in>104105<para>106When enabled, makes the <link id="proxy_bind">bind</link> operation107at each connection attempt.108</para>109110<para>111<note>112This directive is available as part of our113<commercial_version>commercial subscription</commercial_version>.114</note>115</para>116117</directive>118119120<directive name="proxy_buffer_size">121<syntax><value>size</value></syntax>122<default>16k</default>123<context>stream</context>124<context>server</context>125<appeared-in>1.9.4</appeared-in>126127<para>128Sets the <value>size</value> of the buffer used for reading data129from the proxied server.130Also sets the <value>size</value> of the buffer used for reading data131from the client.132</para>133134</directive>135136137<directive name="proxy_connect_timeout">138<syntax><value>time</value></syntax>139<default>60s</default>140<context>stream</context>141<context>server</context>142143<para>144Defines a timeout for establishing a connection with a proxied server.145</para>146147</directive>148149150<directive name="proxy_download_rate">151<syntax><value>rate</value></syntax>152<default>0</default>153<context>stream</context>154<context>server</context>155<appeared-in>1.9.3</appeared-in>156157<para>158Limits the speed of reading the data from the proxied server.159The <value>rate</value> is specified in bytes per second.160The zero value disables rate limiting.161The limit is set per a connection, so if nginx simultaneously opens162two connections to the proxied server,163the overall rate will be twice as much as the specified limit.164</para>165166<para>167Parameter value can contain variables (1.17.0).168It may be useful in cases where rate should be limited169depending on a certain condition:170<example>171map $slow $rate {1721 4k;1732 8k;174}175176proxy_download_rate $rate;177</example>178</para>179180</directive>181182183<directive name="proxy_half_close">184<syntax><literal>on</literal> | <literal>off</literal></syntax>185<default>off</default>186<context>stream</context>187<context>server</context>188<appeared-in>1.21.4</appeared-in>189190<para>191Enables or disables closing192each direction of a TCP connection independently (“TCP half-close”).193If enabled, proxying over TCP will be kept194until both sides close the connection.195</para>196197</directive>198199200<directive name="proxy_next_upstream">201<syntax><literal>on</literal> | <literal>off</literal></syntax>202<default>on</default>203<context>stream</context>204<context>server</context>205206<para>207When a connection to the proxied server cannot be established, determines208whether a client connection will be passed to the next server.209</para>210211<para>212Passing a connection to the next server can be limited by213<link id="proxy_next_upstream_tries">the number of tries</link>214and by <link id="proxy_next_upstream_timeout">time</link>.215</para>216217</directive>218219220<directive name="proxy_next_upstream_timeout">221<syntax><value>time</value></syntax>222<default>0</default>223<context>stream</context>224<context>server</context>225226<para>227Limits the time allowed to pass a connection to the228<link id="proxy_next_upstream">next server</link>.229The <literal>0</literal> value turns off this limitation.230</para>231232</directive>233234235<directive name="proxy_next_upstream_tries">236<syntax><value>number</value></syntax>237<default>0</default>238<context>stream</context>239<context>server</context>240241<para>242Limits the number of possible tries for passing a connection to the243<link id="proxy_next_upstream">next server</link>.244The <literal>0</literal> value turns off this limitation.245</para>246247</directive>248249250<directive name="proxy_pass">251<syntax><value>address</value></syntax>252<default/>253<context>server</context>254255<para>256Sets the address of a proxied server.257The address can be specified as a domain name or IP address,258and a port:259<example>260proxy_pass localhost:12345;261</example>262or as a UNIX-domain socket path:263<example>264proxy_pass unix:/tmp/stream.socket;265</example>266</para>267268<para>269If a domain name resolves to several addresses, all of them will be270used in a round-robin fashion.271In addition, an address can be specified as a272<link doc="ngx_stream_upstream_module.xml">server group</link>.273</para>274275<para>276The address can also be specified using variables (1.11.3):277<example>278proxy_pass $upstream;279</example>280In this case, the server name is searched among the described281<link doc="ngx_stream_upstream_module.xml">server groups</link>,282and, if not found, is determined using a283<link doc="ngx_stream_core_module.xml" id="resolver"/>.284</para>285286</directive>287288289<directive name="proxy_protocol">290<syntax><literal>on</literal> | <literal>off</literal></syntax>291<default>off</default>292<context>stream</context>293<context>server</context>294<appeared-in>1.9.2</appeared-in>295296<para>297Enables the298<link url="http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt">PROXY299protocol</link> for connections to a proxied server.300</para>301302</directive>303304305<directive name="proxy_requests">306<syntax><value>number</value></syntax>307<default>0</default>308<context>stream</context>309<context>server</context>310<appeared-in>1.15.7</appeared-in>311312<para>313Sets the number of client datagrams at which314binding between a client and existing UDP stream session is dropped.315After receiving the specified number of datagrams, next datagram316from the same client starts a new session.317The session terminates when all client datagrams are transmitted318to a proxied server and the expected number of319<link id="proxy_responses">responses</link> is received,320or when it reaches a <link id="proxy_timeout">timeout</link>.321</para>322323</directive>324325326<directive name="proxy_responses">327<syntax><value>number</value></syntax>328<default/>329<context>stream</context>330<context>server</context>331<appeared-in>1.9.13</appeared-in>332333<para>334Sets the number of datagrams expected from the proxied server335in response to a client datagram336if the <link doc="ngx_stream_core_module.xml" id="udp">UDP</link>337protocol is used.338The number serves as a hint for session termination.339By default, the number of datagrams is not limited.340</para>341342<para>343If zero value is specified, no response is expected.344However, if a response is received and the345session is still not finished, the response will be handled.346</para>347348</directive>349350351<directive name="proxy_session_drop">352<syntax><literal>on</literal> | <literal>off</literal></syntax>353<default>off</default>354<context>stream</context>355<context>server</context>356<appeared-in>1.15.8</appeared-in>357358<para>359Enables terminating all sessions to a proxied server360after it was removed from the group or marked as permanently unavailable.361This can occur because of362<link doc="ngx_stream_core_module.xml" id="resolver">re-resolve</link>363or with the API364<link doc="../http/ngx_http_api_module.xml" id="deleteStreamUpstreamServer"><literal>DELETE</literal></link>365command.366A server can be marked as permanently unavailable if it is considered367<link doc="ngx_stream_upstream_hc_module.xml" id="health_check">unhealthy</link>368or with the API369<link doc="../http/ngx_http_api_module.xml" id="patchStreamUpstreamServer"><literal>PATCH</literal></link>370command.371Each session is terminated when the next372read or write event is processed for the client or proxied server.373</para>374375<para>376<note>377This directive is available as part of our378<commercial_version>commercial subscription</commercial_version>.379</note>380</para>381382</directive>383384385<directive name="proxy_socket_keepalive">386<syntax><literal>on</literal> | <literal>off</literal></syntax>387<default>off</default>388<context>stream</context>389<context>server</context>390<appeared-in>1.15.6</appeared-in>391392<para>393Configures the “TCP keepalive” behavior394for outgoing connections to a proxied server.395By default, the operating system’s settings are in effect for the socket.396If the directive is set to the value “<literal>on</literal>”, the397<c-def>SO_KEEPALIVE</c-def> socket option is turned on for the socket.398</para>399400</directive>401402403<directive name="proxy_ssl">404<syntax><literal>on</literal> | <literal>off</literal></syntax>405<default>off</default>406<context>stream</context>407<context>server</context>408409<para>410Enables the SSL/TLS protocol for connections to a proxied server.411</para>412413</directive>414415416<directive name="proxy_ssl_alpn">417<syntax><value>protocol</value> ...</syntax>418<default/>419<context>stream</context>420<context>server</context>421<appeared-in>1.31.0</appeared-in>422423<para>424Specifies the list of protocols to advertise via the425<link url="https://datatracker.ietf.org/doc/html/rfc7301">ALPN</link>426extension when establishing a connection with the proxied server.427For example:428<example>429proxy_ssl_alpn h2 http/1.1;430</example>431</para>432433<para>434Parameter value can contain variables:435<example>436proxy_ssl_alpn $ssl_alpn_protocol;437</example>438</para>439440</directive>441442443<directive name="proxy_ssl_certificate">444<syntax><value>file</value></syntax>445<default/>446<context>stream</context>447<context>server</context>448449<para>450Specifies a <value>file</value> with the certificate in the PEM format451used for authentication to a proxied server.452</para>453454<para id="proxy_ssl_certificate_variables">455Since version 1.21.0, variables can be used in the <value>file</value> name.456</para>457458</directive>459460461<directive name="proxy_ssl_certificate_cache">462<syntax><literal>off</literal></syntax>463<syntax>464<literal>max</literal>=<value>N</value>465[<literal>inactive</literal>=<value>time</value>]466[<literal>valid</literal>=<value>time</value>]</syntax>467<default>off</default>468<context>stream</context>469<context>server</context>470<appeared-in>1.27.4</appeared-in>471472<para>473Defines a cache that stores474<link id="proxy_ssl_certificate">SSL certificates</link> and475<link id="proxy_ssl_certificate_key">secret keys</link>476specified with <link id="proxy_ssl_certificate_key_variables">variables</link>.477</para>478479<para>480The directive has the following parameters:481<list type="tag">482483<tag-name id="proxy_ssl_certificate_cache_max">484<literal>max</literal>485</tag-name>486<tag-desc>487sets the maximum number of elements in the cache;488on cache overflow the least recently used (LRU) elements are removed;489</tag-desc>490491<tag-name id="proxy_ssl_certificate_cache_inactive">492<literal>inactive</literal>493</tag-name>494<tag-desc>495defines a time after which an element is removed from the cache496if it has not been accessed during this time;497by default, it is 10 seconds;498</tag-desc>499500<tag-name id="proxy_ssl_certificate_cache_valid">501<literal>valid</literal>502</tag-name>503<tag-desc>504defines a time during which505an element in the cache is considered valid506and can be reused;507by default, it is 60 seconds.508Certificates that exceed this time will be reloaded or revalidated;509</tag-desc>510511<tag-name id="proxy_ssl_certificate_cache_off">512<literal>off</literal>513</tag-name>514<tag-desc>515disables the cache.516</tag-desc>517518</list>519</para>520521<para>522Example:523<example>524proxy_ssl_certificate $proxy_ssl_server_name.crt;525proxy_ssl_certificate_key $proxy_ssl_server_name.key;526proxy_ssl_certificate_cache max=1000 inactive=20s valid=1m;527</example>528</para>529530</directive>531532533<directive name="proxy_ssl_certificate_key">534<syntax><value>file</value></syntax>535<default/>536<context>stream</context>537<context>server</context>538539<para>540The value541<literal>store</literal>:<value>scheme</value>:<value>id</value>542can be specified instead of the <value>file</value> (1.29.0),543which is used to load a secret key with a specified <value>id</value>544and OpenSSL provider registered URI <value>scheme</value>, such as545<link url="https://datatracker.ietf.org/doc/html/rfc7512"><literal>pkcs11</literal></link>.546</para>547548<para>549Specifies a <value>file</value> with the secret key in the PEM format550used for authentication to a proxied server.551</para>552553<para id="proxy_ssl_certificate_key_variables">554Since version 1.21.0, variables can be used in the <value>file</value> name.555</para>556557</directive>558559560<directive name="proxy_ssl_ciphers">561<syntax><value>ciphers</value></syntax>562<default>DEFAULT</default>563<context>stream</context>564<context>server</context>565566<para>567Specifies the enabled ciphers for connections to a proxied server.568The ciphers are specified in the format understood by the OpenSSL library.569</para>570571<para>572The full list can be viewed using the573“<command>openssl ciphers</command>” command.574</para>575576</directive>577578579<directive name="proxy_ssl_conf_command">580<syntax><value>name</value> <value>value</value></syntax>581<default/>582<context>stream</context>583<context>server</context>584<appeared-in>1.19.4</appeared-in>585586<para>587Sets arbitrary OpenSSL configuration588<link url="https://www.openssl.org/docs/man1.1.1/man3/SSL_CONF_cmd.html">commands</link>589when establishing a connection with the proxied server.590<note>591The directive is supported when using OpenSSL 1.0.2 or higher.592</note>593</para>594595<para>596Several <literal>proxy_ssl_conf_command</literal> directives597can be specified on the same level.598These directives are inherited from the previous configuration level599if and only if there are600no <literal>proxy_ssl_conf_command</literal> directives601defined on the current level.602</para>603604<para>605<note>606Note that configuring OpenSSL directly607might result in unexpected behavior.608</note>609</para>610611</directive>612613614<directive name="proxy_ssl_crl">615<syntax><value>file</value></syntax>616<default/>617<context>stream</context>618<context>server</context>619620<para>621Specifies a <value>file</value> with revoked certificates (CRL)622in the PEM format used to <link id="proxy_ssl_verify">verify</link>623the certificate of the proxied server.624When using intermediate certificates, their CRLs should be625specified in the same file.626</para>627628</directive>629630631<directive name="proxy_ssl_key_log">632<syntax>path</syntax>633<default/>634<context>stream</context>635<context>server</context>636<appeared-in>1.27.2</appeared-in>637638<para>639Enables logging of proxied server connection SSL keys640and specifies the path to the key log file.641Keys are logged in the642<link url="https://datatracker.ietf.org/doc/html/draft-ietf-tls-keylogfile">SSLKEYLOGFILE</link>643format compatible with Wireshark.644</para>645646<para>647<note>648This directive is available as part of our649<commercial_version>commercial subscription</commercial_version>.650</note>651</para>652653</directive>654655656<directive name="proxy_ssl_name">657<syntax><value>name</value></syntax>658<default>host from proxy_pass</default>659<context>stream</context>660<context>server</context>661662<para>663Allows overriding the server name used to664<link id="proxy_ssl_verify">verify</link>665the certificate of the proxied server and to be666<link id="proxy_ssl_server_name">passed through SNI</link>667when establishing a connection with the proxied server.668The server name can also be specified using variables (1.11.3).669</para>670671<para>672By default, the host part of the <link id="proxy_pass"/> address is used.673</para>674675</directive>676677678<directive name="proxy_ssl_password_file">679<syntax><value>file</value></syntax>680<default/>681<context>stream</context>682<context>server</context>683684<para>685Specifies a <value>file</value> with passphrases for686<link id="proxy_ssl_certificate_key">secret keys</link>687where each passphrase is specified on a separate line.688Passphrases are tried in turn when loading the key.689</para>690691</directive>692693694<directive name="proxy_ssl_protocols">695<syntax>696[<literal>SSLv2</literal>]697[<literal>SSLv3</literal>]698[<literal>TLSv1</literal>]699[<literal>TLSv1.1</literal>]700[<literal>TLSv1.2</literal>]701[<literal>TLSv1.3</literal>]</syntax>702<default>TLSv1.2 TLSv1.3</default>703<context>stream</context>704<context>server</context>705706<para>707Enables the specified protocols for connections to a proxied server.708</para>709710<para>711<note>712The <literal>TLSv1.3</literal> parameter is used by default713since 1.23.4.714</note>715</para>716717</directive>718719720<directive name="proxy_ssl_server_name">721<syntax><literal>on</literal> | <literal>off</literal></syntax>722<default>off</default>723<context>stream</context>724<context>server</context>725726<para>727Enables or disables passing of the server name through728<link url="http://en.wikipedia.org/wiki/Server_Name_Indication">TLS729Server Name Indication extension</link> (SNI, RFC 6066)730when establishing a connection with the proxied server.731</para>732733</directive>734735736<directive name="proxy_ssl_session_reuse">737<syntax><literal>on</literal> | <literal>off</literal></syntax>738<default>on</default>739<context>stream</context>740<context>server</context>741742<para>743Determines whether SSL sessions can be reused when working with744the proxied server.745If the errors746“<literal>digest check failed</literal>”747appear in the logs, try disabling session reuse.748</para>749750</directive>751752753<directive name="proxy_ssl_trusted_certificate">754<syntax><value>file</value></syntax>755<default/>756<context>stream</context>757<context>server</context>758759<para>760Specifies a <value>file</value> with trusted CA certificates in the PEM format761used to <link id="proxy_ssl_verify">verify</link>762the certificate of the proxied server.763</para>764765</directive>766767768<directive name="proxy_ssl_verify">769<syntax><literal>on</literal> | <literal>off</literal></syntax>770<default>off</default>771<context>stream</context>772<context>server</context>773774<para>775Enables or disables verification of the proxied server certificate.776</para>777778</directive>779780781<directive name="proxy_ssl_verify_depth">782<syntax><value>number</value></syntax>783<default>1</default>784<context>stream</context>785<context>server</context>786787<para>788Sets the verification depth in the proxied server certificates chain.789</para>790791</directive>792793794<directive name="proxy_timeout">795<syntax><value>timeout</value></syntax>796<default>10m</default>797<context>stream</context>798<context>server</context>799800<para>801Sets the <value>timeout</value> between two successive802read or write operations on client or proxied server connections.803If no data is transmitted within this time, the connection is closed.804</para>805806</directive>807808809<directive name="proxy_upload_rate">810<syntax><value>rate</value></syntax>811<default>0</default>812<context>stream</context>813<context>server</context>814<appeared-in>1.9.3</appeared-in>815816<para>817Limits the speed of reading the data from the client.818The <value>rate</value> is specified in bytes per second.819The zero value disables rate limiting.820The limit is set per a connection, so if the client simultaneously opens821two connections,822the overall rate will be twice as much as the specified limit.823</para>824825<para>826Parameter value can contain variables (1.17.0).827It may be useful in cases where rate should be limited828depending on a certain condition:829<example>830map $slow $rate {8311 4k;8322 8k;833}834835proxy_upload_rate $rate;836</example>837</para>838839</directive>840841</section>842843</module>844845846