js_header_filter

The `js_header_filter` directive allows you to define a JavaScript handler that processes HTTP response headers sent back to the client.

Syntaxjs_header_filter function_name;
Defaultnone
Contextlocation, if in location, limit_except
Arguments1

Description

The js_header_filter directive is used to specify a JavaScript function that will be executed during the response header filtering phase of an NGINX request processing. This function can manipulate HTTP response headers before they are sent to the client. It receives all relevant request data and allows developers to perform complex operations using the flexibility of JavaScript, such as conditionally adding headers, modifying existing headers, or even removing headers altogether based on custom logic.

When defining this directive within a block, it can only accept one argument: the name of the JavaScript function that will manage the header filtering. The context in which this directive can be used includes location, if within a location, and limit_except, making it flexible for various scenarios where header manipulation is required. As it is part of the NGINX JavaScript module, it leverages the njs language, which is a subset of JavaScript, ensuring familiarity for JavaScript developers.

Config Example

location /api {
    js_header_filter my_header_filter_function;
}

Ensure the JavaScript function is properly defined and accessible within the NGINX configuration context.

Be aware that header manipulations may affect caching behaviors, so test thoroughly before deploying changes.

Using synchronous blocking calls within the JavaScript function can lead to performance issues; always prefer non-blocking patterns.

← Back to all directives