此示例适用于 njs 0.2.4 及更高版本。对于 njs 0.2.3 及更早版本,请使用此示例。
load_module modules/ngx_stream_js_module.so;
...
stream {
js_include stream.js;
js_set $bar bar;
js_set $req_line req_line;
server {
listen 12345;
js_preread preread;
return $req_line;
}
server {
listen 12346;
js_access access;
proxy_pass 127.0.0.1:8000;
js_filter header_inject;
}
}
http {
server {
listen 8000;
location / {
return 200 $http_foo\n;
}
}
}
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.abort();
return;
}
s.allow();
}