# HG changeset patch # User Yaroslav Zhuravlev # Date 1479480674 -10800 # Node ID 15632fc2d548b76d662585b3368abdc2ada215d5 # Parent 4035f9146bbf2defbc368e26bd496c473d504561 Documented http and stream nginScript modules. diff --git a/xml/en/GNUmakefile b/xml/en/GNUmakefile --- a/xml/en/GNUmakefile +++ b/xml/en/GNUmakefile @@ -59,6 +59,7 @@ REFS = \ http/ngx_http_hls_module \ http/ngx_http_image_filter_module \ http/ngx_http_index_module \ + http/ngx_http_js_module \ http/ngx_http_limit_conn_module \ http/ngx_http_limit_req_module \ http/ngx_http_log_module \ @@ -99,6 +100,7 @@ REFS = \ stream/ngx_stream_core_module \ stream/ngx_stream_geo_module \ stream/ngx_stream_geoip_module \ + stream/ngx_stream_js_module \ stream/ngx_stream_limit_conn_module \ stream/ngx_stream_log_module \ stream/ngx_stream_map_module \ diff --git a/xml/en/docs/http/ngx_http_js_module.xml b/xml/en/docs/http/ngx_http_js_module.xml new file mode 100644 --- /dev/null +++ b/xml/en/docs/http/ngx_http_js_module.xml @@ -0,0 +1,277 @@ + + + + + + + + +
+ + +The ngx_http_js_module module is used to implement +location and variable handlers in JavaScript. + + + +This module is not built by default, it should be compiled with +nginx JavaScript module using the +--add_module configuration parameter: + +./configure --add-module=path-to-njs/nginx + +The repository +with nginx JavaScript 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. + + +
+ + +
+ + + +js_include http.js; + +server { + listen 8000; + + location / { + js_set $foo foo; + add_header X-Foo $foo; + js_content baz; + } + + location /summary { + js_set $summary summary; + return 200 $summary; + } +} + + + + +The http.js file: + +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(); +} + + + +
+ + +
+ + +file + +http +server +location + + +Specifies a file that implements location and variable handlers in JavaScript. + + + + + + +function + +location +limit_except + + +Sets a JavaScript function as a location content handler. + + + + + + + +$variable function + +http +server +location + + +Sets a JavaScript function for the specified variable. + + + + +
+ + +
+ +Each HTTP JavaScript handler receives two arguments, request and response. + + + +The request object has the following properties: + + +uri + +current URI in a request, read-only + + +method + +request method, read-only + + +httpVersion + +HTTP version, read-only + + +remoteAddress + +client address, read-only + + +headers{} + +request headers object, read-only. + +For example, the Header-Name header +can be accessed with the syntax headers['Header-Name'] +or headers.Header_name + + + +args{} + +request arguments object, read-only + + +variables{} + +nginx variables object, read-only + + +log(string) + +writes a string to the error log + + + + + +The response object has the following properties: + + +status + +response status, writable + + +headers{} + +response headers object + + +contentType + +the response
Content-Type
header field value, writable +
+ +contentLength + +the response
Content-Length
header field value, writable +
+
+
+ + +The response object has the following methods: + + +sendHeader() + +sends the HTTP header to the client + + +send(string) + +sends a part of the response body to the client + + +finish() + +finishes sending a response to the client + + + + +
+ +
diff --git a/xml/en/docs/index.xml b/xml/en/docs/index.xml --- a/xml/en/docs/index.xml +++ b/xml/en/docs/index.xml @@ -8,7 +8,7 @@
@@ -303,6 +303,11 @@ ngx_http_index_module + +ngx_http_js_module + + + ngx_http_limit_conn_module @@ -515,6 +520,11 @@ ngx_stream_geoip_module + +ngx_stream_js_module + + + ngx_stream_limit_conn_module diff --git a/xml/en/docs/stream/ngx_stream_js_module.xml b/xml/en/docs/stream/ngx_stream_js_module.xml new file mode 100644 --- /dev/null +++ b/xml/en/docs/stream/ngx_stream_js_module.xml @@ -0,0 +1,320 @@ + + + + + + + + +
+ + +The ngx_stream_js_module module is used to +implement handlers in JavaScript. + + + +This module is not built by default, it should be compiled with +nginx JavaScript module using the +--add_module configuration parameter: + +./configure --add-module=path-to-njs/nginx + +The repository +with nginx JavaScript 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; + + server { + listen 12345; + + js_preread qux; + + js_set $foo foo; + js_set $bar bar; + + 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) { + 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; + } +} + + + +
+ + +
+ + +function + +stream +server + + +Sets a JavaScript function which will be called at the +access phase. + + + + + + +function + +stream +server + + +Sets a data filter. + + + + + + +file + +stream +server + + +Specifies a file that implements server and variable handlers in JavaScript. + + + + + + +function + +stream +server + + +Sets a JavaScript function which will be called at the +preread phase. + + + + + + + +$variable function + +stream +server + + +Sets a JavaScript function for the specified variable. + + + + +
+ + +
+ +Each stream JavaScript handler receives one argument, a stream session object. + + + +The session object has the following properties: + + + +remoteAddress + +client address, read-only + + +eof + +a boolean read-only property, true if the current buffer is the last buffer + + +fromUpstream + +a boolean read-only property, +true if the current buffer is from the upstream server to the client + + +buffer + +the current buffer, writable + + +variables{} + +nginx variables object, read-only + + +OK + +the OK return code + + +DECLINED + +the DECLINED return code + + +AGAIN + +the AGAIN return code + + +ERROR + +the ERROR return code + + +ABORT + +the ABORT return code + + + + + +The session object has the following methods: + + + +log(string) + +writes a sent string to the error log + + + + +
+ +