grpc_set_header

Sets a gRPC header for the request. — NGINX HTTP Core

grpc_set_header
httpserverlocation
Синтаксисgrpc_set_header name value;
По умолчаниюnone
Контекстhttp, server, location
МодульNGINX HTTP Core
Аргументы2

Описание

The `grpc_set_header` directive allows you to add or modify HTTP/2 headers for gRPC requests passed to a backend server. This is particularly useful in a microservices architecture where specific metadata may need to be sent along with the request. The directive takes two parameters: the first is the name of the header to be set, and the second is the value to assign to that header. This allows for dynamic generation of header values based on the context of the request or predefined variables. When the `grpc_set_header` directive is used in a particular context (http, server, or location), it will apply headers at that level of configuration. For instance, if declared in a server block, all gRPC requests handled by that server will carry the specified headers. If declared inside a location block, only those requests matching that location will have the headers set. It’s also possible to use variables within the directive to set header values dynamically, increasing flexibility in how metadata is passed to the backend services.

Пример конфига

location /rpc {
    grpc_pass grpc://backend;
    grpc_set_header api-key $api_key;
    grpc_set_header user-id $http_user_id;
}

Ensure that headers are set correctly without typos, as gRPC is sensitive to header names.

Avoid using unsupported header names that may be filtered by the gRPC protocol or the backend server.