Skip to content

An official read-only mirror of http://hg.nginx.org/njs/ which is updated hourly. Pull requests on GitHub cannot be accepted and will be automatically closed. The proper way to submit changes to nginx is via the nginx development mailing list, see http://nginx.org/en/docs/contributing_changes.html

License

AsamQi/njs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

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

About

An official read-only mirror of http://hg.nginx.org/njs/ which is updated hourly. Pull requests on GitHub cannot be accepted and will be automatically closed. The proper way to submit changes to nginx is via the nginx development mailing list, see http://nginx.org/en/docs/contributing_changes.html

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 97.1%
  • Makefile 1.6%
  • Other 1.3%