1 Star 1 Fork 2

ngwsx / njs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-2-Clause
Configure nginx with HTTP and Stream JavaScript modules using the --add-module
option:

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

Alternatively, you can build a dynamic version of the modules

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

and add the following lines to nginx.conf to load them

    load_module modules/ngx_http_js_module.so;
    load_module modules/ngx_stream_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).


HTTP JavaScript module
----------------------

Each HTTP JavaScript handler receives two arguments - request and response.

    function foo(req, res) {
        ..
    }

The following properties are available:

req
    - uri
    - method
    - httpVersion
    - remoteAddress
    - headers{}
    - args{}
    - variables{}
    - log()

res
    - status
    - headers{}
    - contentType
    - contentLength
    - sendHeader()
    - send()
    - finish()


Example nginx.conf:

    # load dynamic HTTP JavaScript module
    load_module modules/ngx_http_js_module.so;

    worker_processes 1;
    pid logs/nginx.pid;

    events {
        worker_connections  256;
    }

    http {
        # include JavaScript file
        js_include http.js;

        server {
            listen 8000;

            location / {
                # create $foo variable and set JavaScript function foo()
                # from the included JavaScript file as its handler
                js_set $foo foo;

                add_header X-Foo $foo;

                # register JavaScript function bar() as content handler
                js_content baz;
            }

            location /summary {
                js_set $summary summary;

                return 200 $summary;
            }
        }
    }


http.js:

    function foo(req, res) {
        req.log("hello from foo() handler");
        return "foo";
    }

    function summary(req, res) {
        var a, s, h;

        s = "JS summary\n\n";

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

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

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

        return s;
    }

    function baz(req, res) {
        res.headers.foo = 1234;
        res.status = 200;
        res.contentType = "text/plain; charset=utf-8";
        res.contentLength = 15;
        res.sendHeader();
        res.send("nginx");
        res.send("java");
        res.send("script");

        res.finish();
    }


Stream JavaScript module
------------------------

Each Stream JavaScript handler receives one argument - stream session object.

    function foo(s) {
        ..
    }

The following properties are available in the session object:

     - remoteAddress
     - eof
     - fromUpstream
     - buffer
     - variables{}
     - log()
     - OK
     - DECLINED
     - AGAIN
     - ERROR
     - ABORT


Example nginx.conf:

    # load dynamic Stream JavaScript module
    load_module modules/ngx_stream_js_module.so;

    worker_processes 1;
    pid logs/nginx.pid;

    events {
        worker_connections  256;
    }

    stream {
        # include JavaScript file
        js_include stream.js;

        server {
            listen 8000;

            # preread data with qux()
            js_preread qux;

            # create $foo and $bar variables and set JavaScript
            # functions foo() and bar() from the included JavaScript
            # file as their handlers
            js_set $foo foo;
            js_set $bar bar;

            # echo-server: return preread data
            return foo;
        }

        server {
            listen 8001;

            # check access in xyz()
            js_access xyz;

            proxy_pass 127.0.0.1:9000;

            # add JavaScript filter baz() from the included
            # JavaScript file
            js_filter baz;
        }
    }

    http {
        server {
            listen 9000;
            location / {
                return 200 $http_foo\n;
            }
        }
    }


stream.js:

    var req = '';
    var matched = 0;
    var line = '';

    function qux(s) {
        n = s.buffer.indexOf('\n');
        if (n == -1) {
            return s.AGAIN;
        }

        line = s.buffer.substr(0, n);
    }

    function foo(s) {
        return line;
    }

    function bar(s) {
        var v = s.variables;
        s.log("hello from bar() handler!");
        return "foo-var" + v.remote_port + "; pid=" + v.pid;
    }

    // The filter processes one buffer per call.
    // The buffer is available in s.buffer both for
    // reading and writing.  Called for both directions.

    function baz(s) {
        if (s.fromUpstream || matched) {
            return;
        }

        // Disable certain addresses.

        if (s.remoteAddress.match('^192.*')) {
            return s.ERROR;
        }

        // Read HTTP request line.
        // Collect bytes in 'req' until request
        // line is read.  Clear current buffer to
        // disable output.

        req = req + s.buffer;
        s.buffer = '';

        n = req.search('\n');

        if (n != -1) {
            // Inject a new HTTP header.
            var rest = req.substr(n + 1);
            req = req.substr(0, n + 1);

            addr = s.remoteAddress;

            s.log('req:' + req);
            s.log('rest:' + rest);

            // Output the result and skip further
            // processing.

            s.buffer = req + 'Foo: addr_' + addr + '\r\n' + rest;
            matched = 1;
        }
    }

    function xyz(s) {
        if (s.remoteAddress.match('^192.*')) {
            return s.ABORT;
        }
    }

--
NGINX, Inc., http://nginx.com
/* * Copyright (C) 2015-2016 Igor Sysoev * Copyright (C) 2015-2016 NGINX, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */

简介

nginScript njs nJScript fork 展开 收起
C
BSD-2-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/ngwsx2008/njs.git
git@gitee.com:ngwsx2008/njs.git
ngwsx2008
njs
njs
master

搜索帮助