js_access

The js_access directive allows custom JavaScript to handle access control in NGINX stream contexts.

Syntaxjs_access function_name;
Defaultnone
Contextstream, stream server
Arguments1

Description

The js_access directive is part of the NGINX JavaScript module, which enables the use of JavaScript for various operations within NGINX configurations. Specifically, in the context of the stream module, this directive allows you to specify a JavaScript function that will be invoked to determine access controls for network connections. This facilitates the implementation of complex access rules that go beyond standard configuration options, leveraging JavaScript's flexibility and expressiveness.

This directive requires one argument: the name of the JavaScript function that will handle the access logic. When a client connection is made, NGINX will call the specified JavaScript function, passing along connection details, which can be used to make runtime decisions on whether to allow or deny access. The flexibility of JavaScript allows you to work with various parameters and implement sophisticated logic, such as checking IP addresses against a dynamic list, integrating with external services, or applying rate limiting.

Using js_access effectively requires a good understanding of both NGINX's event model and the JavaScript execution environment provided by the NJS module. It is essential to ensure that the JavaScript code is non-blocking to maintain high performance and scalability for incoming connections.

Config Example

stream {
    js_access my_access_function;
    
    server {
        listen 12345;
        proxy_pass backend_servers;
    }
}

Ensure the JavaScript function is defined and properly loaded before it's referenced in the configuration.

Performance can be impacted if the JavaScript function contains blocking operations or long-running logic.

Debugging JavaScript errors may require additional logging configuration to capture errors returned from the function.

← Back to all directives