ngx_stream_js_module 模块

The ngx_stream_js_module module is used to implement 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.

stream {
   js_import stream.js;

   js_set $bar stream.bar;
   js_set $req_line stream.req_line;

   server {
       listen 12345;

       js_preread stream.preread;
       return     $req_line;
   }

   server {
       listen 12346;

       js_access  stream.access;
       proxy_pass 127.0.0.1:8000;
       js_filter  stream.header_inject;
   }
}

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

The stream.js file:

var line = '';

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

function preread(s) {
   s.on('upload', function (data, flags) {
       var n = data.indexOf('\n');
       if (n != -1) {
           line = data.substr(0, n);
           s.done();
       }
   });
}

function req_line(s) {
   return line;
}

// Read HTTP request line.
// Collect bytes in 'req' until
// request line is read.
// Injects HTTP header into a client's request

var my_header =  'Foo: foo';
function header_inject(s) {
   var req = '';
   s.on('upload', function(data, flags) {
       req += data;
       var n = req.search('\n');
       if (n != -1) {
           var rest = req.substr(n + 1);
           req = req.substr(0, n + 1);
           s.send(req + my_header + '\r\n' + rest, flags);
           s.off('upload');
       }
   });
}

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

   s.allow();
}

export default {bar, preread, req_line, header_inject, access};

Directives

Syntax:

js_access function | module.function;
   

Default:

—    

Context:

stream, server
   

Sets an njs function which will be called at the access phase. Since 0.4.0, a module function can be referenced.

Syntax:

js_fetch_ciphers ciphers;
   

Default:

js_fetch_ciphers HIGH:!aNULL:!MD5;
   

Context:

stream, server
   

This directive appeared in version 0.7.0.

Specifies the enabled ciphers for HTTPS connections with Fetch API. The ciphers are specified in the format understood by the OpenSSL library.

The full list can be viewed using the “openssl ciphers” command.

Syntax:

js_fetch_protocols [TLSv1] [TLSv1.1] [TLSv1.2] [TLSv1.3];
   

Default:

js_fetch_protocols TLSv1 TLSv1.1 TLSv1.2;
   

Context:

stream, server
   

This directive appeared in version 0.7.0.

Enables the specified protocols for HTTPS connections with Fetch API.

Syntax:

js_fetch_trusted_certificate file;
   

Default:

—    

Context:

stream, server
   

This directive appeared in version 0.7.0.

Specifies a file with trusted CA certificates in the PEM format used to verify the HTTPS certificate with Fetch API.

Syntax:

js_fetch_verify_depth number;
   

Default:

js_fetch_verify_depth 100;
   

Context:

stream, server
   

This directive appeared in version 0.7.0.

Sets the verification depth in the HTTPS server certificates chain with Fetch API.

Syntax:

js_filter function | module.function;
   

Default:

—    

Context:

stream, server
   

Sets a data filter. Since 0.4.0, a module function can be referenced.

Syntax:

js_import module.js | export_name from module.js;
   

Default:

—    

Context:

stream
   

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 stream.js;

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

Several js_import directives can be specified.

Syntax:

js_include file;
   

Default:

—    

Context:

stream
   

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

nginx.conf:
js_include stream.js;
js_set     $js_addr address;
server {
   listen 127.0.0.1:12345;
   return $js_addr;
}

stream.js:
function address(s) {
   return s.remoteAddress;
}

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

Syntax:

js_path path;
   

Default:

—    

Context:

stream
   

This directive appeared in version 0.3.0.

Sets an additional path for njs modules.

Syntax:

js_preread function | module.function;
   

Default:

—    

Context:

stream, server
   

Sets an njs function which will be called at the preread phase. Since 0.4.0, a module function can be referenced.

Syntax:

js_set $variable function | module.function;
   

Default:

—    

Context:

stream
   

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

The function is called when the variable is referenced for the first time for a given request. The exact moment depends on a phase at which the variable is referenced. This can be used to perform some logic not related to variable evaluation. For example, if the variable is referenced only in the log_format directive, its handler will not be executed until the log phase. This handler can be used to do some cleanup right before the request is freed.

As the js_set handler returns its result immediately, it supports only synchronous callbacks. Thus, asynchronous callbacks such as ngx.fetch() or setTimeout() are not supported.

Syntax:

js_var $variable [value];
   

Default:

—    

Context:

stream
   

This directive appeared in version 0.5.3.

Declares a writable variable. The value can contain text, variables, and their combination.

Session Object Properties

Each stream njs handler receives one argument, a stream session object.

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号