eval_override_content_type

The `eval_override_content_type` directive allows you to specify a custom content type for the response of an evaluated subrequest.

Syntaxeval_override_content_type content_type;
Defaultnone
Contexthttp, server, location
Arguments1

Description

The eval_override_content_type directive is used in conjunction with the NGINX eval module, particularly useful when handling subrequests that return content. This directive lets you manually set the Content-Type header for the response generated from an evaluated variable. By default, responses inherit the Content-Type from the original request or subrequest; however, in scenarios where the content returned is different or needs to be treated specially, this directive allows for explicit control over the Content-Type, ensuring that clients interpret the response correctly.

To utilize this directive, specify a single argument that defines the desired content type, such as 'text/plain' or 'application/json'. It can be placed within the HTTP, server, or location contexts, and is crucial when the normal processing of the response would not assign an appropriate type. Since the directive allows for content injection in locations handling dynamic content, it works effectively in scenarios where response types vary based on backend processing or application logic.

When implementing this directive, careful consideration must be given to the expected output of the evaluated variable, and the specified content type should correspond to the format of that content, otherwise clients may misinterpret the data.

Config Example

location /example {
    eval_override_content_type application/json;
    eval $data {
        backend_response;
    }
    if ($data ~ '"key": "value"') {
        add_header X-Custom-Header 'Found value';
    }
}

Make sure the specified content type matches the actual content being returned; otherwise, clients may misinterpret the response.

Misconfiguration may result in unexpected behaviors especially if combined with other content handling directives like proxy_set_header.

Not defining this directive may lead to unwanted content types being sent to clients.

← Back to all directives