gRPC#

Allows passing requests to a gRPC server.

Note

This module requires the HTTP/2 module.

When building from the source code, the module is built by default; it can be disabled with the build option --without-http_grpc_module.

In packages and images from our repositories, the module is included in the build.

Configuration Example#

server {
    listen 9000;

    http2 on;

    location / {
        grpc_pass 127.0.0.1:9000;
    }
}

Directives#

grpc_bind#

Syntax

grpc_bind address [transparent] | off;

Default

—

Context

http, server, location

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

By default, this value is evaluated once when the request is initialized, before a gRPC server is selected. In Angie PRO, evaluation happens after each gRPC server selection (including retries), so the bind address can depend on the selected server; for example, via the $upstream_current_addr variable. If the evaluated value is empty, no binding is made.

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

grpc_bind $remote_addr transparent;

In order for this parameter to work, it is usually necessary to run Angie worker processes with the superuser privileges. On Linux it is not required as if the transparent parameter is specified, worker processes inherit the CAP_NET_RAW capability from the master process.

Note

It is necessary to configure kernel routing table to intercept network traffic from the gRPC server.

grpc_buffer_size#

Syntax

grpc_buffer_size size;

Default

grpc_buffer_size 4k|8k;

Context

http, server, location

Sets the size of the buffer used for reading the first part of the response received from the gRPC server. The response is passed to the client synchronously, as soon as it is received.

grpc_connect_timeout#

Syntax

grpc_connect_timeout time;

Default

grpc_connect_timeout 60s;

Context

http, server, location

Defines a timeout for establishing a connection with a gRPC server. It should be noted that this timeout cannot usually exceed 75 seconds.

Note

This timeout does not govern idle connections: how long an already-open one is kept for reuse is set by keepalive_timeout in an upstream block whose connections are cached with keepalive.

grpc_connection_drop#

Syntax

grpc_connection_drop time | on | off;

Default

grpc_connection_drop off;

Context

http, server, location

Enables termination of all active connections to the proxied server after it has been removed from the group or marked as permanently unavailable by a reresolve process or the API command DELETE. Idle connections in the cache are not affected.

A connection is terminated when the next read or write event is processed for either the client or the proxied server.

Setting time enables a connection termination timeout; with on set, connections are dropped immediately.

grpc_hide_header#

Syntax

grpc_hide_header field;

Default

—

Context

http, server, location

By default, Angie does not pass the header fields Date, Server, and X-Accel-... from the response of a gRPC server to a client. The grpc_hide_header directive sets additional fields that will not be passed. If, on the contrary, the passing of fields needs to be permitted, the grpc_pass_header directive can be used.

grpc_ignore_headers#

Syntax

grpc_ignore_headers field ...;

Default

—

Context

http, server, location

Disables processing of certain response header fields from the gRPC server. The following fields can be ignored: X-Accel-Redirect and X-Accel-Charset.

If not disabled, processing of these header fields has the following effect:

grpc_intercept_errors#

Syntax

grpc_intercept_errors on | off;

Default

grpc_intercept_errors off;

Context

http, server, location

Determines whether gRPC server responses with codes greater than or equal to 300 should be passed to a client or be intercepted and redirected to Angie for processing with the error_page directive.

grpc_next_upstream#

Syntax

grpc_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;

Default

grpc_next_upstream error timeout;

Context

http, server, location

Specifies in which cases a request should be passed to the next server in the upstream group:

error

an error occurred while establishing a connection with the server, passing a request to it, or reading the response header;

timeout

a timeout has occurred while establishing a connection with the server, passing a request to it, or reading the response header;

invalid_header

a server returned an empty or invalid response;

http_500

a server returned a response with the code 500;

http_502

a server returned a response with the code 502;

http_503

a server returned a response with the code 503;

http_504

a server returned a response with the code 504;

http_403

a server returned a response with the code 403;

http_404

a server returned a response with the code 404;

http_429

a server returned a response with the code 429;

non_idempotent

normally, requests with a non-idempotent method (POST, LOCK, PATCH) are not passed to the next server if a request has been sent to an upstream server; enabling this option explicitly allows retrying such requests;

off

disables passing a request to the next server.

Note

One should bear in mind that passing a request to the next server is only possible if nothing has been sent to a client yet. That is, if an error or timeout occurs in the middle of the transferring of a response, fixing this is impossible.

The directive also defines what is considered an unsuccessful attempt of communication with a server.

error, timeout, invalid_header

always considered unsuccessful attempts, even if they are not specified in the directive

http_500, http_502, http_503, http_504, http_429

considered unsuccessful attempts only if they are specified in the directive

http_403, http_404

never considered unsuccessful attempts

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

grpc_next_upstream_timeout#

Syntax

grpc_next_upstream_timeout time;

Default

grpc_next_upstream_timeout 0;

Context

http, server, location

Limits the time during which a request can be passed to the next server.

0

turns off this limitation

grpc_next_upstream_tries#

Syntax

grpc_next_upstream_tries number;

Default

grpc_next_upstream_tries 0;

Context

http, server, location

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

0

turns off this limitation

grpc_pass#

Syntax

grpc_pass address;

Default

—

Context

location, if in location

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

grpc_pass localhost:9000;

or as a UNIX domain socket path:

grpc_pass unix:/tmp/grpc.socket;

Alternatively, the grpc:// scheme can be used:

grpc_pass grpc://127.0.0.1:9000;

To use gRPC over SSL, the grpcs:// scheme should be used:

grpc_pass grpcs://127.0.0.1:443;

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.

Parameter value can contain variables. In this case, if an address is specified as a domain name, the name is searched among the described server groups, and, if not found, is determined using a resolver.

The request URI is passed in the :path pseudo-header field by the same rule as with proxy_pass without a URI: exactly as the client sent it, unless Angie has changed the URI or the arguments of the request.

Note

If grpc_pass is specified in a location with a trailing slash in the prefix (for example, location /name/), and the auto_redirect directive is set to default, requests without a trailing slash will be redirected (/name -> /name/).

grpc_pass_header#

Syntax

grpc_pass_header field;

Default

—

Context

http, server, location

Permits passing otherwise disabled header fields from a gRPC server to a client.

grpc_read_timeout#

Syntax

grpc_read_timeout time;

Default

grpc_read_timeout 60s;

Context

http, server, location

Defines a timeout for reading a response from the gRPC server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the gRPC server does not transmit anything within this time, the connection is closed.

grpc_send_timeout#

Syntax

grpc_send_timeout time;

Default

grpc_send_timeout 60s;

Context

http, server, location

Sets a timeout for transmitting a request to the gRPC server. The timeout is set only between two successive write operations, not for the transmission of the whole request. If the gRPC server does not receive anything within this time, the connection is closed.

grpc_set_header#

Syntax

grpc_set_header field value;

Default

grpc_set_header Content-Length $content_length;

Context

http, server, location

Allows redefining or appending fields to the request header passed to the gRPC server. The value can contain variables. These directives are inherited from the previous configuration level if and only if there are no grpc_set_header directives defined on the current level.

If the value of a header field is an empty string then this field will not be passed to the gRPC server:

grpc_set_header Accept-Encoding "";

grpc_socket_keepalive#

Syntax

grpc_socket_keepalive on | off;

Default

grpc_socket_keepalive off;

Context

http, server, location

Configures the "TCP keepalive" behavior for outgoing connections to a gRPC server.

off

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.

grpc_ssl_certificate#

Syntax

grpc_ssl_certificate file | data:certificate;

Default

—

Context

http, server, location

Specifies the certificate in the PEM format used for authentication to a gRPC SSL server.

The value is interpreted as in ssl_certificate: a file path, or the PEM text of the certificate after the data: prefix. The value can contain variables; they are evaluated each time Angie establishes a connection to the gRPC SSL server, so all variables of the request are available, including those assigned by set. If the evaluated certificate value is empty, no client certificate is sent. To avoid loading certificates for every connection, enable grpc_ssl_certificate_cache.

grpc_ssl_certificate_cache#

Syntax

grpc_ssl_certificate_cache off;

grpc_ssl_certificate_cache max=N [inactive=time] [valid=time];

Default

grpc_ssl_certificate_cache off;

Context

http, server, location

Defines a cache that stores SSL certificates and secret keys specified using variables.

The directive supports the following parameters:

  • max — sets the maximum number of elements in the cache. When the cache overflows, the least recently used (LRU) elements are removed.

  • inactive — defines the time after which an element is removed if it has not been accessed during this time, even if still within its valid period; each access resets this timer. The default is 10 seconds.

  • valid — defines the time during which a cached element is reused without checks. The default is 60 seconds. After this period, an element loaded from a file is reloaded only if the file has changed, and a key loaded with the engine: or store: prefix is loaded again; an element specified with the data: prefix stays valid.

  • off — disables the cache.

Keys protected by a passphrase (see grpc_ssl_password_file) are not cached.

Example:

grpc_ssl_certificate       /etc/angie/ssl/$host.crt;
grpc_ssl_certificate_key   /etc/angie/ssl/$host.key;
grpc_ssl_certificate_cache max=1000 inactive=20s valid=1m;

grpc_ssl_certificate_key#

Syntax

grpc_ssl_certificate_key file | data:key | engine:name:id | store:uri;

Default

—

Context

http, server, location

Specifies the secret key in the PEM format used for authentication to a gRPC SSL server.

The value, including the engine: and store: prefixes, is interpreted as in ssl_certificate_key, and variables in it work as described in grpc_ssl_certificate. A key protected by a passphrase is decrypted with the passphrases from grpc_ssl_password_file.

grpc_ssl_ciphers#

Syntax

grpc_ssl_ciphers ciphers;

Default

grpc_ssl_ciphers DEFAULT;

Context

http, server, location

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

The list of ciphers depends on the version of OpenSSL installed. The full list can be viewed using the openssl ciphers command.

Warning

With OpenSSL, the grpc_ssl_ciphers directive does not configure TLS 1.3 ciphers; set them with the Ciphersuites command of the grpc_ssl_conf_command directive instead. The grpc_ssl_ciphers directive ignores TLS 1.3 cipher names in its value, so such names do not restrict TLS 1.3 connections. If the value consists only of such names, for example TLS_AES_128_GCM_SHA256, the configuration fails to load with the no cipher match error.

  • With LibreSSL, TLS 1.3 ciphers can be configured using grpc_ssl_ciphers.

  • With BoringSSL, TLS 1.3 ciphers cannot be configured at all.

grpc_ssl_conf_command#

Syntax

grpc_ssl_conf_command name value;

Default

—

Context

http, server, location

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

Note

The directive is supported when using OpenSSL 1.0.2 or higher. The Ciphersuites command, which sets TLS 1.3 ciphers, requires OpenSSL 1.1.1 or higher.

Several grpc_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 grpc_ssl_conf_command directives defined on the current level.

Warning

Note that configuring OpenSSL directly might result in unexpected behavior.

grpc_ssl_crl#

Syntax

grpc_ssl_crl file;

Default

—

Context

http, server, location

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

grpc_ssl_name#

Syntax

grpc_ssl_name name;

Default

grpc_ssl_name `host name from grpc_pass;`

Context

http, server, location

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

The server name can also be specified with variables.

By default, the host name from grpc_pass is used.

grpc_ssl_password_file#

Syntax

grpc_ssl_password_file file;

Default

—

Context

http, server, location

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.

grpc_ssl_protocols#

Syntax

grpc_ssl_protocols SSLv2 | SSLv3 | TLSv1 | TLSv1.1 | TLSv1.2 | TLSv1.3 ...;

Default

grpc_ssl_protocols TLSv1.2 TLSv1.3;

Context

http, server, location

Enables the specified protocols for requests to a gRPC SSL server.

grpc_ssl_server_name#

Syntax

grpc_ssl_server_name on | off;

Default

grpc_ssl_server_name off;

Context

http, server, location

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

grpc_ssl_session_reuse#

Syntax

grpc_ssl_session_reuse on | off;

Default

grpc_ssl_session_reuse on;

Context

http, server, location

Determines whether SSL sessions can be reused when working with the gRPC server. If the SSL3_GET_FINISHED:digest check failed error appears in the logs, try disabling session reuse.

grpc_ssl_trusted_certificate#

Syntax

grpc_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 gRPC SSL server.

grpc_ssl_verify#

Syntax

grpc_ssl_verify on | off;

Default

grpc_ssl_verify off;

Context

http, server, location

Enables or disables verification of the gRPC SSL server certificate.

grpc_ssl_verify_depth#

Syntax

grpc_ssl_verify_depth number;

Default

grpc_ssl_verify_depth 1;

Context

http, server, location

Sets the verification depth in the gRPC SSL server certificates chain.