
Configure nginx with HTTP JavaScript module using the --add-module option:

    ./configure --add-module=<path-to-njs>/nginx

Alternatively, you can build a dynamic version of the njs module

    ./configure --add-dynamic-module=<path-to-njs>/nginx

and add the following line to nginx.conf and reload nginx:

    load_module modules/ngx_http_js_module.so;

Please report your experiences to the NGINX development mailing list
nginx-devel@nginx.org (http://mailman.nginx.org/mailman/listinfo/nginx-devel).

JavaScript objects
------------------

$r
|- uri
|- method
|- httpVersion
|- remoteAddress
|- headers{}
|- args{}
|- response
  |- status
  |- headers{}
  |- contentType
  |- contentLength
  |- sendHeader()
  |- send(data)
  |- finish()


Example
-------

Create nginx.conf:

    worker_processes 1;
    pid logs/nginx.pid;

    events {
        worker_connections  256;
    }

    http {
        js_set $summary "
            var a, s, h;

            s = 'JS summary\n\n';

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

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

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

            s;
            ";

        server {
            listen 8000;

            location / {
                js_run "
                    var res;
                    res = $r.response;
                    res.headers.foo = 1234;
                    res.status = 302;
                    res.contentType = 'text/plain; charset=utf-8';
                    res.contentLength = 15;
                    res.sendHeader();
                    res.send('nginx');
                    res.send('java');
                    res.send('script');
                    res.finish();
                    ";
            }

            location /summary {
                return 200 $summary;
            }
        }
    }

Run nginx & test the output:

$ curl 127.0.0.1:8000

nginxjavascript

$ curl -H "Foo: 1099" '127.0.0.1:8000/summary?a=1&fooo=bar&zyx=xyz'

JS summary

Method: GET
HTTP version: 1.1
Host: 127.0.0.1:8000
Remote Address: 127.0.0.1
URI: /summary
Headers:
  header "Host" is "127.0.0.1:8000"
  header "User-Agent" is "curl/7.43.0"
  header "Accept" is "*/*"
  header "Foo" is "1099"
Args:
  arg "a" is "1"
  arg "fooo" is "bar"
  arg "zyx" is "xyz"


--
NGINX, Inc., http://nginx.com
