Professional Documents
Culture Documents
Examples
Hello World
english
URL Decoding
русский
URL Encoding
Internal Redirect
news
Returning Fastest Response from Proxy
about
Creating HS JWT
download
Accessing API from a Subrequest
security
Creating secure_link Hash
documentation
Logging the Number of Requests Per Client
faq
Subrequests Chaining
books
The examples work since 0.4.0. support
events {} unit
njs
http {
js_import http.js;
server {
listen 8000;
location / {
js_content http.hello;
}
}
}
http.js:
function hello(r) {
r.return(200, "Hello world!");
}
URL Decoding
nginx.conf:
https://nginx.org/en/docs/njs/examples.html#redirect 1/7
1/15/2021 Examples
js_import http.js;
http.js:
function decoded_foo(r) {
return decodeURIComponent(r.args.foo);
}
URL Encoding
nginx.conf:
js_import http.js;
location / {
proxy_pass http://example.com?foo=$encoded_foo;
}
http.js:
function encoded_foo(r) {
return encodeURIComponent('foo & bar?');
}
Internal Redirect
nginx.conf:
js_import http.js;
location /redirect {
js_content http.redirect;
}
location @named {
return 200 named;
}
http.js:
function redirect(r) {
r.internalRedirect('@named');
https://nginx.org/en/docs/njs/examples.html#redirect 2/7
1/15/2021 Examples
}
nginx.conf:
js_import http.js;
location /start {
js_content http.content;
}
location /foo {
proxy_pass http://backend1;
}
location /bar {
proxy_pass http://backend2;
}
http.js:
function content(r) {
var n = 0;
function done(res) {
if (n++ == 0) {
r.return(res.status, res.responseBody);
}
}
Creating HS JWT
nginx.conf:
js_import http.js;
http.js:
https://nginx.org/en/docs/njs/examples.html#redirect 3/7
1/15/2021 Examples
var s = [header, claims].map(JSON.stringify)
.map(v=>v.toUTF8())
.map(v=>v.toString('base64url'))
.join('.');
function jwt(r) {
var claims = {
iss: "nginx",
sub: "alice",
foo: 123,
bar: "qq",
zyx: false
};
nginx.conf:
js_import http.js;
keyval_zone zone=foo:10m;
...
location /keyval {
js_content http.set_keyval;
}
location /version {
js_content http.version;
}
location /api {
api write=on;
}
http.js:
function set_keyval(r) {
r.subrequest('/api/5/http/keyvals/foo',
{ method: 'POST',
body: JSON.stringify({ foo: 789, bar: "ss dd 00" })},
function(res) {
if (res.status >= 300) {
r.return(res.status, res.responseBody);
return;
}
https://nginx.org/en/docs/njs/examples.html#redirect 4/7
1/15/2021 Examples
r.return(500);
});
}
function version(r) {
r.subrequest('/api/5/nginx', { method: 'GET' }, function(res) {
if (res.status != 200) {
r.return(res.status);
return;
}
nginx.conf:
js_import http.js;
location / {
secure_link $cookie_foo;
secure_link_md5 "$uri mykey";
...
}
location @login {
add_header Set-Cookie "foo=$new_foo; Max-Age=60";
return 302 /;
}
http.js:
function create_secure_link(r) {
return require('crypto').createHash('md5')
.update(r.uri).update(" mykey")
.digest('base64url');
}
nginx.conf:
js_import http.js;
https://nginx.org/en/docs/njs/examples.html#redirect 5/7
1/15/2021 Examples
js_set $num_requests http.num_requests;
keyval_zone zone=foo:10m;
server {
listen 8000;
location / {
root html;
}
}
http.js:
function num_requests(r)
{
var n = r.variables.foo;
n = n ? Number(n) + 1 : 1;
r.variables.foo = n;
return n;
}
Subrequests Chaining
nginx.conf:
js_import http.js;
location /start {
js_content http.content;
}
location /auth {
proxy_pass http://auth_backend;
}
location /backend {
proxy_pass http://backend;
}
http.js:
function content(r) {
r.subrequest('/auth')
https://nginx.org/en/docs/njs/examples.html#redirect 6/7
1/15/2021 Examples
.then(reply => JSON.parse(reply.responseBody))
.then(response => {
if (!response['token']) {
throw new Error("token is not available");
}
return reply['token'];
})
.then(token => {
r.subrequest('/backend', `token=${token}`)
.then(reply => r.return(reply.status, reply.responseBody));
})
.catch(_ => r.return(500));
}
https://nginx.org/en/docs/njs/examples.html#redirect 7/7