Mercurial > hg > nginx-site
changeset 2642:c60bcc0de435
Updated TOC in njs examples.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Wed, 20 Jan 2021 09:34:32 +0000 |
parents | ea9f4dc0c801 |
children | 4849fa0fd4b4 |
files | xml/en/docs/njs/examples.xml xml/ru/docs/njs/examples.xml |
diffstat | 2 files changed, 363 insertions(+), 335 deletions(-) [+] |
line wrap: on
line diff
--- a/xml/en/docs/njs/examples.xml Thu Jan 21 12:12:47 2021 +0000 +++ b/xml/en/docs/njs/examples.xml Wed Jan 20 09:34:32 2021 +0000 @@ -56,134 +56,7 @@ </section> -<section id="urldecode" name="URL Decoding"> - -<para> -<path>nginx.conf</path>: -<example> -js_import http.js; - -js_set $decoded_foo http.decoded_foo; -</example> -</para> - -<para> -<path>http.js</path>: -<example> -function decoded_foo(r) { - return decodeURIComponent(r.args.foo); -} - -export default {decoded_foo}; -</example> -</para> - -</section> - - -<section id="urlencode" name="URL Encoding"> - -<para> -<path>nginx.conf</path>: -<example> -js_import http.js; - -js_set $encoded_foo http.encoded_foo; -... - -location / { - proxy_pass http://example.com?foo=$encoded_foo; -} -</example> -</para> - -<para> -<path>http.js</path>: -<example> -function encoded_foo(r) { - return encodeURIComponent('foo & bar?'); -} - -export default {encoded_foo}; -</example> -</para> - -</section> - - -<section id="redirect" name="Internal Redirect"> - -<para> -<path>nginx.conf</path>: -<example> -js_import http.js; - -location /redirect { - js_content http.redirect; -} - -location @named { - return 200 named; -} -</example> -</para> - -<para> -<path>http.js</path>: -<example> -function redirect(r) { - r.internalRedirect('@named'); -} - -export default {redirect}; -</example> -</para> - -</section> - - -<section id="fast_response" name="Returning Fastest Response from Proxy"> - -<para> -<path>nginx.conf</path>: -<example> -js_import http.js; - -location /start { - js_content http.content; -} - -location /foo { - proxy_pass http://backend1; -} - -location /bar { - proxy_pass http://backend2; -} -</example> -</para> - -<para> -<path>http.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); -} - -export default {content}; -</example> -</para> - -</section> +<section id="http_auth" name="HTTP Аuthorization"> <section id="jwt" name="Creating HS JWT"> @@ -232,6 +105,45 @@ </section> +<section id="secure_link" name="Creating secure_link Hash"> + +<para> +<path>nginx.conf</path>: +<example> +js_import http.js; + +js_set $new_foo http.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>http.js</path>: +<example> +function create_secure_link(r) { + return require('crypto').createHash('md5') + .update(r.uri).update(" mykey") + .digest('base64url'); +} + +export default {create_secure_link}; +</example> +</para> + +</section> + + <section id="jwt_field" name="Getting Arbitrary Field from JWT as nginx Variable"> @@ -272,6 +184,11 @@ </section> +</section> + + +<section id="http_proxying" name="HTTP Proxying"> + <section id="subrequest" name="Accessing API from a Subrequest"> @@ -333,25 +250,23 @@ </section> -<section id="secure_link" name="Creating secure_link Hash"> +<section id="fast_response" name="Returning Fastest Response from Proxy"> <para> <path>nginx.conf</path>: <example> js_import http.js; -js_set $new_foo http.create_secure_link; -... - -location / { - secure_link $cookie_foo; - secure_link_md5 "$uri mykey"; - ... +location /start { + js_content http.content; } -location @login { - add_header Set-Cookie "foo=$new_foo; Max-Age=60"; - return 302 /; +location /foo { + proxy_pass http://backend1; +} + +location /bar { + proxy_pass http://backend2; } </example> </para> @@ -359,13 +274,103 @@ <para> <path>http.js</path>: <example> -function create_secure_link(r) { - return require('crypto').createHash('md5') - .update(r.uri).update(" mykey") - .digest('base64url'); +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); +} + +export default {content}; +</example> +</para> + +</section> + + +<section id="subrequests_chaining" name="Subrequests Chaining"> + +<para> +<path>nginx.conf</path>: +<example> +js_import http.js; + +location /start { + js_content http.content; +} + +location /auth { + proxy_pass http://auth_backend; } -export default {create_secure_link}; +location /backend { + proxy_pass http://backend; +} +</example> +</para> + +<para> +<path>http.js</path>: +<example> +function content(r) { + r.subrequest('/auth') + .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)); +} + +export default {content}; +</example> +</para> + +</section> + +</section> + + +<section id="misc" name="Miscellaneous"> + + +<section id="redirect" name="Internal Redirect"> + +<para> +<path>nginx.conf</path>: +<example> +js_import http.js; + +location /redirect { + js_content http.redirect; +} + +location @named { + return 200 named; +} +</example> +</para> + +<para> +<path>http.js</path>: +<example> +function redirect(r) { + r.internalRedirect('@named'); +} + +export default {redirect}; </example> </para> @@ -421,24 +426,43 @@ </section> - -<section id="subrequests_chaining" name="Subrequests Chaining"> +<section id="urldecode" name="URL Decoding"> <para> <path>nginx.conf</path>: <example> js_import http.js; -location /start { - js_content http.content; +js_set $decoded_foo http.decoded_foo; +</example> +</para> + +<para> +<path>http.js</path>: +<example> +function decoded_foo(r) { + return decodeURIComponent(r.args.foo); } -location /auth { - proxy_pass http://auth_backend; -} +export default {decoded_foo}; +</example> +</para> + +</section> + + +<section id="urlencode" name="URL Encoding"> -location /backend { - proxy_pass http://backend; +<para> +<path>nginx.conf</path>: +<example> +js_import http.js; + +js_set $encoded_foo http.encoded_foo; +... + +location / { + proxy_pass http://example.com?foo=$encoded_foo; } </example> </para> @@ -446,26 +470,16 @@ <para> <path>http.js</path>: <example> -function content(r) { - r.subrequest('/auth') - .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)); +function encoded_foo(r) { + return encodeURIComponent('foo & bar?'); } -export default {content}; +export default {encoded_foo}; </example> </para> </section> +</section> + </article>
--- a/xml/ru/docs/njs/examples.xml Thu Jan 21 12:12:47 2021 +0000 +++ b/xml/ru/docs/njs/examples.xml Wed Jan 20 09:34:32 2021 +0000 @@ -55,134 +55,7 @@ </section> -<section id="urldecode" name="Декодирование URL"> - -<para> -<path>nginx.conf</path>: -<example> -js_import http.js; - -js_set $decoded_foo http.decoded_foo; -</example> -</para> - -<para> -<path>http.js</path>: -<example> -function decoded_foo(r) { - return decodeURIComponent(r.args.foo); -} - -export default {decoded_foo}; -</example> -</para> - -</section> - - -<section id="urlencode" name="Кодирование URL"> - -<para> -<path>nginx.conf</path>: -<example> -js_import http.js; - -js_set $encoded_foo http.encoded_foo; -... - -location / { - proxy_pass http://example.com?foo=$encoded_foo; -} -</example> -</para> - -<para> -<path>http.js</path>: -<example> -function encoded_foo(r) { - return encodeURIComponent('foo & bar?'); -} - -export default {encoded_foo}; -</example> -</para> - -</section> - - -<section id="redirect" name="Внутренняя переадресация"> - -<para> -<path>nginx.conf</path>: -<example> -js_import http.js; - -location /redirect { - js_content http.redirect; -} - -location @named { - return 200 named; -} -</example> -</para> - -<para> -<path>http.js</path>: -<example> -function redirect(r) { - r.internalRedirect('@named'); -} - -export default {redirect}; -</example> -</para> - -</section> - - -<section id="fast_response" name="Возвращение самого быстрого ответа от прокси"> - -<para> -<path>nginx.conf</path>: -<example> -js_import http.js; - -location /start { - js_content http.content; -} - -location /foo { - proxy_pass http://backend1; -} - -location /bar { - proxy_pass http://backend2; -} -</example> -</para> - -<para> -<path>http.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); -} - -export default {content}; -</example> -</para> - -</section> +<section id="http_auth" name="HTTP-авторизация"> <section id="jwt" name="Создание HS JWT"> @@ -231,6 +104,45 @@ </section> +<section id="secure_link" name="Создание secure_link хэша"> + +<para> +<path>nginx.conf</path>: +<example> +js_import http.js; + +js_set $new_foo http.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>http.js</path>: +<example> +function create_secure_link(r) { + return require('crypto').createHash('md5') + .update(r.uri).update(" mykey") + .digest('base64url'); +} + +export default {create_secure_link}; +</example> +</para> + +</section> + + <section id="jwt_field" name="Получение произвольного поля JWT как значение переменной nginx"> @@ -271,6 +183,11 @@ </section> +</section> + + +<section id="http_proxying" name="HTTP-проксирование"> + <section id="subrequest" name="Доступ к API из подзапроса"> @@ -332,25 +249,23 @@ </section> -<section id="secure_link" name="Создание secure_link хэша"> +<section id="fast_response" name="Возвращение самого быстрого ответа от прокси"> <para> <path>nginx.conf</path>: <example> js_import http.js; -js_set $new_foo http.create_secure_link; -... - -location / { - secure_link $cookie_foo; - secure_link_md5 "$uri mykey"; - ... +location /start { + js_content http.content; } -location @login { - add_header Set-Cookie "foo=$new_foo; Max-Age=60"; - return 302 /; +location /foo { + proxy_pass http://backend1; +} + +location /bar { + proxy_pass http://backend2; } </example> </para> @@ -358,19 +273,108 @@ <para> <path>http.js</path>: <example> -function create_secure_link(r) { - return require('crypto').createHash('md5') - .update(r.uri).update(" mykey") - .digest('base64url'); +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); } -export default {create_secure_link}; +export default {content}; </example> </para> </section> +<section id="subrequests_chaining" name="Построение цепочки подзапросов"> + +<para> +<path>nginx.conf</path>: +<example> +js_import http.js; + +location /start { + js_content http.content; +} + +location /auth { + proxy_pass http://auth_backend; +} + +location /backend { + proxy_pass http://backend; +} +</example> +</para> + +<para> +<path>http.js</path>: +<example> +function content(r) { + r.subrequest('/auth') + .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)); +} + +export default {content}; +</example> +</para> + +</section> + +</section> + + +<section id="misc" name="Разное"> + + +<section id="redirect" name="Внутренняя переадресация"> + +<para> +<path>nginx.conf</path>: +<example> +js_import http.js; + +location /redirect { + js_content http.redirect; +} + +location @named { + return 200 named; +} +</example> +</para> + +<para> +<path>http.js</path>: +<example> +function redirect(r) { + r.internalRedirect('@named'); +} + +export default {redirect}; +</example> +</para> + +</section> + <section id="requests" name="Запись в лог количества запросов от клиента"> <para> @@ -421,23 +425,43 @@ </section> -<section id="subrequests_chaining" name="Построение цепочки подзапросов"> +<section id="urldecode" name="Декодирование URL"> <para> <path>nginx.conf</path>: <example> js_import http.js; -location /start { - js_content http.content; +js_set $decoded_foo http.decoded_foo; +</example> +</para> + +<para> +<path>http.js</path>: +<example> +function decoded_foo(r) { + return decodeURIComponent(r.args.foo); } -location /auth { - proxy_pass http://auth_backend; -} +export default {decoded_foo}; +</example> +</para> + +</section> + + +<section id="urlencode" name="Кодирование URL"> -location /backend { - proxy_pass http://backend; +<para> +<path>nginx.conf</path>: +<example> +js_import http.js; + +js_set $encoded_foo http.encoded_foo; +... + +location / { + proxy_pass http://example.com?foo=$encoded_foo; } </example> </para> @@ -445,26 +469,16 @@ <para> <path>http.js</path>: <example> -function content(r) { - r.subrequest('/auth') - .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)); +function encoded_foo(r) { + return encodeURIComponent('foo & bar?'); } -export default {content}; +export default {encoded_foo}; </example> </para> </section> +</section> + </article>