Module ngx_http_js_module

The ngx_http_js_module module is used to implement location and variable handlers in njs — a subset of the JavaScript language.

Download and install instructions are available here.

Example Configuration

The example works since 0.4.0.

http {
    js_import http.js;

    js_set $foo     http.foo;
    js_set $summary http.summary;

    server {
        listen 8000;

        location / {
            add_header X-Foo $foo;
            js_content http.baz;
        }

        location = /summary {
            return 200 $summary;
        }

        location = /hello {
            js_content http.hello;
        }
    }
}

The http.js file:

function foo(r) {
    r.log("hello from foo() handler");
    return "foo";
}

function summary(r) {
    var a, s, h;

    s = "JS summary\n\n";

    s += "Method: " + r.method + "\n";
    s += "HTTP version: " + r.httpVersion + "\n";
    s += "Host: " + r.headersIn.host + "\n";
    s += "Remote Address: " + r.remoteAddress + "\n";
    s += "URI: " + r.uri + "\n";

    s += "Headers:\n";
    for (h in r.headersIn) {
        s += "  header '" + h + "' is '" + r.headersIn[h] + "'\n";
    }

    s += "Args:\n";
    for (a in r.args) {
        s += "  arg '" + a + "' is '" + r.args[a] + "'\n";
    }

    return s;
}

function baz(r) {
    r.status = 200;
    r.headersOut.foo = 1234;
    r.headersOut['Content-Type'] = "text/plain; charset=utf-8";
    r.headersOut['Content-Length'] = 15;
    r.sendHeader();
    r.send("nginx");
    r.send("java");
    r.send("script");

    r.finish();
}

function hello(r) {
    r.return(200, "Hello world!");
}

export default {foo, summary, baz, hello};

Directives

Syntax:js_content function | module.function;
Default:
Context:location , limit_except

Sets an njs function as a location content handler. Since 0.4.0, a module function can be referenced.

Syntax:js_import module.js | export_name from module.js;
Default:
Context:http

This directive appeared in version 0.4.0.

Imports a module that implements location and variable handlers in njs. The export_name is used as a namespace to access module functions. If the export_name is not specified, the module name will be used as a namespace.

js_import http.js;

Here, the module name http is used as a namespace while accessing exports. If the imported module contains foo() , http.foo is used to refer to it.

Several js_import directives can be specified.

Syntax:js_include file;
Default:
Context:http

Specifies a file that implements location and variable handlers in njs:

nginx.conf:
js_include http.js;
location   /version {
    js_content version;
}

http.js:
function version(r) {
    r.return(200, njs.version);
}

The directive is deprecated since 0.4.0, the js_import directive should be used instead.

Syntax:js_path path;
Default:
Context:http

This directive appeared in version 0.3.0.

Sets an additional path for njs modules.

Syntax:js_set $variable function | module.function;
Default:
Context:http

Sets an njs function for the specified variable. Since 0.4.0, a module function can be referenced.

Request Argument

Each HTTP njs handler receives one argument, a request object.