Mercurial > hg > nginx-site
view xml/en/docs/njs/examples.xml @ 2455:a81518fa3802
Removed obsolete note from Russian njs Reference.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Thu, 31 Oct 2019 15:34:18 +0300 |
parents | 253641e268a8 |
children | b60e5be733cd |
line wrap: on
line source
<?xml version="1.0"?> <!-- Copyright (C) Nginx, Inc. --> <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> <article name="Examples" link="/en/docs/njs/examples.html" lang="en" rev="7"> <section id="helloword" name="Hello World"> <para> <path>nginx.conf</path>: <example> load_module modules/ngx_http_js_module.so; events {} http { js_include hello_world.js; server { listen 8000; location / { js_content hello; } } } </example> </para> <para> <literal>hello_world.js</literal>: <example> function hello(r) { r.return(200, "Hello world!"); } </example> </para> </section> <section id="urldecode" name="URL Decoding"> <para> <path>nginx.conf</path>: <example> js_include urldecode.js; js_set $decoded_foo decoded_foo; </example> </para> <para> <path>urldecode.js</path>: <example> function decoded_foo(r) { return decodeURIComponent(r.args.foo); } </example> </para> </section> <section id="urlencode" name="URL Encoding"> <para> <path>nginx.conf</path>: <example> js_include urlencode.js; js_set $encoded_foo encoded_foo; ... location / { proxy_pass http://example.com?foo=$encoded_foo; } </example> </para> <para> <path>urlencode.js</path>: <example> function encoded_foo(r) { return encodeURIComponent('foo & bar?'); } </example> </para> </section> <section id="redirect" name="Internal Redirect"> <para> <path>nginx.conf</path>: <example> js_include redirect.js; location /redirect { js_content redirect; } location @named { return 200 named; } </example> </para> <para> <path>redirect.js</path>: <example> function redirect(r) { r.internalRedirect('@named'); } </example> </para> </section> <section id="fast_response" name="Returning Fastest Response from Proxy"> <para> <path>nginx.conf</path>: <example> js_include fastresponse.js; location /start { js_content content; } location /foo { proxy_pass http://backend1; } location /bar { proxy_pass http://backend2; } </example> </para> <para> <path>fastresponse.js</path>: <example> function content(r) { var n = 0; function done(res) { if (n++ == 0) { r.return(res.status, res.responseBody); } } r.subrequest('/foo', r.variables.args, done); r.subrequest('/bar', r.variables.args, done); } </example> </para> </section> <section id="jwt" name="Creating HS JWT"> <para> <path>nginx.conf</path>: <example> js_include hs_jwt.js; js_set $jwt jwt; </example> </para> <para> <path>hs_jwt.js</path>: <example> function create_hs256_jwt(claims, key, valid) { var header = { "typ" : "JWT", "alg" : "HS256", "exp" : Math.floor(Date.now()/1000) + valid }; var s = JSON.stringify(header).toBytes().toString('base64url') + '.' + JSON.stringify(claims).toBytes().toString('base64url'); var h = require('crypto').createHmac('sha256', key); return s + '.' + h.update(s).digest().toString('base64url'); } function jwt(r) { var claims = { "iss" : "nginx", "sub" : "alice", "foo" : 123, "bar" : "qq", "zyx" : false }; return create_hs256_jwt(claims, 'foo', 600); } </example> </para> </section> <section id="subrequest" name="Accessing API from a Subrequest"> <para> <path>nginx.conf</path>: <example> js_include subrequest.js; keyval_zone zone=foo:10m; ... location /keyval { js_content set_keyval; } location /version { js_content version; } location /api { api write=on; } </example> </para> <para> <path>subrequest.js</path>: <example> 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; } r.return(500); }); } function version(r) { r.subrequest('/api/5/nginx', { method: 'GET' }, function(res) { if (res.status != 200) { r.return(res.status); return; } var json = JSON.parse(res.responseBody); r.return(200, json.version); }); } </example> </para> </section> <section id="secure_link" name="Creating secure_link Hash"> <para> <path>nginx.conf</path>: <example> js_include hash.js; js_set $new_foo create_secure_link; ... location / { secure_link $cookie_foo; secure_link_md5 "$uri mykey"; ... } location @login { add_header Set-Cookie "foo=$new_foo; Max-Age=60"; return 302 /; } </example> </para> <para> <path>hash.js</path>: <example> function create_secure_link(r) { return require('crypto').createHash('md5') .update(r.uri).update(" mykey") .digest('base64url'); } </example> </para> </section> <section id="requests" name="Logging the Number of Requests Per Client"> <para> <path>nginx.conf</path>: <example> js_include js_requests.js; js_set $num_requests num_requests; keyval_zone zone=foo:10m; keyval $remote_addr $foo zone=foo; log_format bar '$remote_addr [$time_local] $num_requests'; access_log logs/access.log bar; server { listen 8000; location / { root html; } } </example> </para> <para> <path>js_requests.js</path>: <example> function num_requests(r) { var n = r.variables.foo; n = n ? Number(n) + 1 : 1; r.variables.foo = n; return n; } </example> <note> The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> and <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> directives are available as part of our <commercial_version>commercial subscription</commercial_version>. </note> </para> </section> </article>