js_filter

The `js_filter` directive allows integration of JavaScript filters in NGINX stream/server contexts.

Syntaxjs_filter function_name;
Defaultnone
Contextstream, stream server
Arguments1

Description

The js_filter directive is a feature of the NGINX JavaScript module, primarily designed to integrate JavaScript-based processing logic directly into the NGINX stream or server processing stages. This directive accepts a single argument, which is typically a JavaScript function that will be executed whenever a relevant request is received. The purpose of this function is to offer developers the flexibility to manipulate request and response data as it flows through the NGINX server.

When specified, js_filter invokes the JavaScript function defined as its argument for each matching request. This function is executed with the request context, allowing access to request properties and enabling dynamic response generation or modification, such as altering headers or response bodies. Additionally, because it operates within the NGINX context, it can efficiently handle large amounts of data and process events in a non-blocking manner, maintaining high performance and scalability.

Moreover, scenarios such as filtering certain types of traffic based on custom logic, transforming responses, or implementing lightweight data validation rules become notably streamlined with js_filter. It enhances the capability of NGINX, making it adaptable to a broader spectrum of applications, especially those requiring complex request handling or real-time modifications. Application of this directive should be carefully crafted to prevent performance bottlenecks from misuse of blocking operations within the JavaScript code.

Config Example

stream {
    js_filter my_js_filter;
}

server {
    listen 80;
    js_filter my_js_filter;
}

Ensure that the JavaScript function specified is correctly defined and accessible in the NGINX context.

The code within the JavaScript filter should avoid using blocking operations to maintain performance.

Test the filter thoroughly to prevent unintended data modifications or logic errors.

← Back to all directives