blob: bebd8dfcd7610cd2cc972948d162893eeb51336f [file] [log] [blame]
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