The ngx_stream_js_module
module is used to implement handlers in nginScript — a subset of the JavaScript language.
This module is not built by default, it should be compiled with the nginScript module using the --add_module
configuration parameter:
./configure --add-module=path-to-njs
/nginx
The repository with the nginScript module can be cloned with the following command (requires Mercurial client):
hg clone http://hg.nginx.org/njs
This module can also be built as dynamic:
./configure --add-dynamic_module=path-to-njs
/nginx
The module is experimental, caveat emptor applies.
stream { js_include stream.js; js_set $foo foo; js_set $bar bar; server { listen 12345; js_preread qux; return $foo; } server { listen 12346; js_access xyz; proxy_pass 127.0.0.1:8000; js_filter baz; } } http { server { listen 8000; location / { return 200 $http_foo\n; } } }
The stream.js
file:
var req = ''; var matched = 0; var line = ''; function qux(s) { var 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 = ''; var n = req.search('\n'); if (n != -1) { // Inject a new HTTP header. var rest = req.substr(n + 1); req = req.substr(0, n + 1); var 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; } }
Syntax: | js_access |
---|---|
Default: | — |
Context: | stream , server |
Sets an nginScript function which will be called at the access phase.
Syntax: | js_filter |
---|---|
Default: | — |
Context: | stream , server |
Sets a data filter.
Syntax: | js_include |
---|---|
Default: | — |
Context: | stream |
Specifies a file that implements server and variable handlers in nginScript.
Syntax: | js_preread |
---|---|
Default: | — |
Context: | stream , server |
Sets an nginScript function which will be called at the preread phase.
Syntax: | js_set
|
---|---|
Default: | — |
Context: | stream |
Sets an nginScript function for the specified variable.
Each stream nginScript handler receives one argument, a stream session object.
The session object has the following properties:
remoteAddress
eof
fromUpstream
buffer
variables{}
OK
OK
return code DECLINED
DECLINED
return code AGAIN
AGAIN
return code ERROR
ERROR
return code ABORT
ABORT
return code The session object has the following methods:
log(string
)
string
to the error log
© 2002-2017 Igor Sysoev
© 2011-2017 Nginx, Inc.
Licensed under the BSD License.
https://nginx.org/en/docs/stream/ngx_stream_js_module.html