stream_proxy module#

Allows proxying data streams over TCP, UDP, and UNIX domain sockets.

Example Configuration#

server {
    listen 127.0.0.1:12345;
    proxy_pass 127.0.0.1:8080;
}

server {
    listen 12345;
    proxy_connect_timeout 1s;
    proxy_timeout 1m;
    proxy_pass example.com:12345;
}

server {
    listen 53 udp reuseport;
    proxy_timeout 20s;
    proxy_pass dns.example.com:53;
}

server {
    listen [::1]:12345;
    proxy_pass unix:/tmp/stream.socket;
}

Directives#

proxy_bind#

Syntax:

proxy_bind address [transparent] | off;

Default:

Context:

stream, server

Makes outgoing connections to a proxied server originate from the specified local IP address. Parameter value can contain variables. The special value off cancels the effect of the proxy_bind directive inherited from the previous configuration level, which allows the system to auto-assign the local IP address.

The transparent parameter allows outgoing connections to a proxied server originate from a non-local IP address, for example, from a real IP address of a client:

proxy_bind $remote_addr transparent;

For this parameter to work, Angie worker processes usually need to run with superuser privileges. On Linux, this is not required: if the transparent parameter is specified, worker processes inherit the CAP_NET_RAW capability from the master process.

Important

The kernel routing table should also be configured to intercept network traffic from the FastCGI server.

proxy_buffer_size#

Syntax:

proxy_buffer_size size;

Default:

proxy_buffer_size 16k;

Context:

stream, server

Sets the size of the buffer used for reading data from the proxied server. Also sets the size of the buffer used for reading data from the client.

proxy_connect_timeout#

Syntax:

proxy_connect_timeout time;

Default:

proxy_connect_timeout 60s;

Context:

stream, server

Defines a timeout for establishing a connection with a proxied server.

proxy_download_rate#

Syntax:

proxy_download_rate rate;

Default:

proxy_download_rate 0;

Context:

stream, server

Limits the speed of reading the data from the proxied server. The rate is specified in bytes per second.

0

disables rate limiting

Note

The limit is set per a connection, so if Angie simultaneously opens two connections to the proxied server, the overall rate will be twice as much as the specified limit.

Parameter value can contain variables. It may be useful in cases where rate should be limited depending on a certain condition:

proxy_download_rate $rate;

map $slow $rate {
    1     4k;
    2     8k;
}

proxy_half_close#

Syntax:

proxy_half_close on | off;

Default:

proxy_half_close off;

Context:

stream, server

Enables or disables closing each direction of a TCP connection independently (“TCP half-close”). If enabled, proxying over TCP will be kept until both sides close the connection.

proxy_next_upstream#

Syntax:

proxy_next_upstream on | off;

Default:

proxy_next_upstream on;

Context:

stream, server

When a connection to the proxied server cannot be established, determines whether a client connection will be passed to the next server in the upstream pool.

Passing a connection to the next server can be limited by the number of tries and by time.

proxy_next_upstream_timeout#

Syntax:

proxy_next_upstream_timeout time;

Default:

proxy_next_upstream_timeout 0;

Context:

stream, server

Limits the time allowed to pass a connection to the next server.

0

turns off this limitation

proxy_next_upstream_tries#

Syntax:

proxy_next_upstream_tries number;

Default:

proxy_next_upstream_tries 0;

Context:

stream, server

Limits the number of possible tries for passing a connection to the next server.

0

turns off this limitation

proxy_pass#

Syntax:

proxy_pass address;

Default:

Context:

server

Sets the address of a proxied server. The address can be specified as a domain name or IP address, and a port:

proxy_pass localhost:12345;

or as a UNIX domain socket path:

proxy_pass unix:/tmp/stream.socket;

If a domain name resolves to several addresses, all of them will be used in a round-robin fashion. In addition, an address can be specified as a server group.

The address can also be specified using variables:

proxy_pass $upstream;

In this case, the server name is searched among the described server groups, and, if not found, is determined using a resolver.

proxy_protocol#

Syntax:

proxy_protocol on | off;

Default:

proxy_protocol off;

Context:

stream, server

Enables the PROXY protocol for connections to a proxied server.

proxy_requests#

Syntax:

proxy_requests number;

Default:

proxy_requests 0;

Context:

stream, server

Sets the number of client datagrams at which binding between a client and existing UDP stream session is dropped. After receiving the specified number of datagrams, next datagram from the same client starts a new session. The session terminates when all client datagrams are transmitted to a proxied server and the expected number of responses is received, or when it reaches a timeout.

proxy_responses#

Syntax:

proxy_responses number;

Default:

Context:

stream, server

Sets the number of datagrams expected from the proxied server in response to a client datagram if the UDP protocol is used. The number serves as a hint for session termination. By default, the number of datagrams is not limited.

If zero value is specified, no response is expected. However, if a response is received and the session is still not finished, the response will be handled.

proxy_socket_keepalive#

Syntax:

proxy_socket_keepalive on | off;

Default:

proxy_socket_keepalive off;

Context:

stream, server

Configures the “TCP keepalive” behavior for outgoing connections to a proxied server.

""

By default, the operating system’s settings are in effect for the socket.

on

The SO_KEEPALIVE socket option is turned on for the socket.

proxy_ssl#

Syntax:

proxy_ssl on | off;

Default:

proxy_ssl off;

Context:

stream, server

Enables the SSL/TLS protocol for connections to a proxied server.

proxy_ssl_certificate#

Syntax:

proxy_ssl_certificate file;

Default:

Context:

stream, server

Specifies a file with the certificate in the PEM format used for authentication to a proxied server. Variables can be used in the file name.

New in version 1.2.0.

When proxy_ssl_ntls enabled, directive accepts two arguments instead of one, sign and encryption parts of certificate:

server {
    proxy_ssl_ntls  on;

    proxy_ssl_certificate      sign.crt enc.crt;
    proxy_ssl_certificate_key  sign.key enc.key;

    proxy_ssl_ciphers "ECC-SM2-WITH-SM4-SM3:ECDHE-SM2-WITH-SM4-SM3:RSA";

    proxy_pass backend:12345;
}

proxy_ssl_certificate_key#

Syntax:

proxy_ssl_certificate_key file;

Default:

Context:

stream, server

Specifies a file with the secret key in the PEM format used for authentication to a proxied server. Variables can be used in the file name.

New in version 1.2.0.

When proxy_ssl_ntls enabled, directive accepts two arguments instead of one: sign and encryption parts of key:

server {
    proxy_ssl_ntls  on;

    proxy_ssl_certificate      sign.crt enc.crt;
    proxy_ssl_certificate_key  sign.key enc.key;

    proxy_ssl_ciphers "ECC-SM2-WITH-SM4-SM3:ECDHE-SM2-WITH-SM4-SM3:RSA";

    proxy_pass backend:12345;
}

proxy_ssl_ciphers#

Syntax:

proxy_ssl_ciphers ciphers;

Default:

proxy_ssl_ciphers DEFAULT;

Context:

stream, server

Specifies the enabled ciphers for requests to a proxied server. The ciphers are specified in the format understood by the OpenSSL library.

The full list can be viewed using the “openssl ciphers” command.

proxy_ssl_conf_command#

Syntax:

proxy_ssl_conf_command name value;

Default:

Context:

stream, server

Sets arbitrary OpenSSL configuration commands when establishing a connection with the proxied server.

Important

The directive is supported when using OpenSSL 1.0.2 or higher.

Several proxy_ssl_conf_command directives can be specified on the same level. These directives are inherited from the previous configuration level if and only if there are no proxy_ssl_conf_command directives defined on the current level.

Caution

Note that configuring OpenSSL directly might result in unexpected behavior.

proxy_ssl_crl#

Syntax:

proxy_ssl_crl file;

Default:

Context:

stream, server

Specifies a file with revoked certificates (CRL) in the PEM format used to verify the certificate of the proxied server.

proxy_ssl_name#

Syntax:

proxy_ssl_name name;

Default:

proxy_ssl_name $proxy_host;

Context:

stream, server

Allows overriding the server name used to verify the certificate of the proxied server and to be passed through SNI when establishing a connection with the proxied server.

By default, the host part of the proxy_pass address is used.

proxy_ssl_ntls#

New in version 1.2.0.

Syntax:

proxy_ssl_ntls on | off;

Default:

proxy_ssl_ntls off;

Context:

stream, server

Enables client-side support for NTLS using TongSuo library.

server {
    proxy_ssl_ntls  on;

    proxy_ssl_certificate      sign.crt enc.crt;
    proxy_ssl_certificate_key  sign.key enc.key;

    proxy_ssl_ciphers "ECC-SM2-WITH-SM4-SM3:ECDHE-SM2-WITH-SM4-SM3:RSA";

    proxy_pass backend:12345;
}

Important

Build Angie using the –with-ntls configure option and link with NTLS-enabled SSL library

./configure --with-openssl=../Tongsuo-8.3.0 \
            --with-openssl-opt=enable-ntls  \
            --with-ntls

proxy_ssl_password_file#

Syntax:

proxy_ssl_password_file file;

Default:

Context:

stream, server

Specifies a file with passphrases for secret keys where each passphrase is specified on a separate line. Passphrases are tried in turn when loading the key.

proxy_ssl_protocols#

Syntax:

proxy_ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2] [TLSv1.3];

Default:

proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

Context:

stream, server

Changed in version 1.2.0: TLSv1.3 parameter added to default set.

Enables the specified protocols for requests to a proxied server.

proxy_ssl_server_name#

Syntax:

proxy_ssl_server_name on | off;

Default:

proxy_ssl_server_name off;

Context:

stream, server

Enables or disables passing the server name set by the proxy_ssl_name directive via the Server Name Indication TLS extension (SNI, RFC 6066) while establishing a connection with the proxied server.

proxy_ssl_session_reuse#

Syntax:

proxy_ssl_session_reuse on | off;

Default:

proxy_ssl_session_reuse on;

Context:

stream, server

Determines whether SSL sessions can be reused when working with the proxied server. If the errors “SSL3_GET_FINISHED:digest check failed” appear in the logs, try disabling session reuse.

proxy_ssl_trusted_certificate#

Syntax:

proxy_ssl_trusted_certificate file;

Default:

Context:

http, server, location

Specifies a file with trusted CA certificates in the PEM format used to verify the certificate of the proxied server.

proxy_ssl_verify#

Syntax:

proxy_ssl_verify on | off;

Default:

proxy_ssl_verify off;

Context:

stream, server

Enables or disables verification of the proxied server certificate.

proxy_ssl_verify_depth#

Syntax:

proxy_ssl_verify_depth number;

Default:

proxy_ssl_verify_depth 1;

Context:

stream, server

Sets the verification depth in the proxied server certificates chain.

proxy_timeout#

Syntax:

proxy_timeout timeout;

Default:

proxy_timeout 10m;

Context:

stream, server

Sets the timeout between two successive read or write operations on client or proxied server connections. If no data is transmitted within this time, the connection is closed.

proxy_upload_rate#

Syntax:

proxy_upload_rate rate;

Default:

proxy_upload_rate 0;

Context:

stream, server

Limits the speed of reading the data from the client. The rate is specified in bytes per second.

0

disables rate limiting

Note

The limit is set per a connection, so if the client simultaneously opens two connections, the overall rate will be twice as much as the specified limit.

Parameter value can contain variables. It may be useful in cases where rate should be limited depending on a certain condition:

map $slow $rate {
    1     4k;
    2     8k;
}

proxy_upload_rate $rate;