changeset 2454:253641e268a8

Added js_requests.js example in njs.
author Yaroslav Zhuravlev <yar@nginx.com>
date Thu, 31 Oct 2019 15:33:19 +0300
parents 1b0cc44de0fd
children a81518fa3802
files xml/en/docs/http/ngx_http_keyval_module.xml xml/en/docs/njs/examples.xml xml/ru/docs/njs/examples.xml
diffstat 3 files changed, 102 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/xml/en/docs/http/ngx_http_keyval_module.xml
+++ b/xml/en/docs/http/ngx_http_keyval_module.xml
@@ -9,14 +9,16 @@
 <module name="Module ngx_http_keyval_module"
         link="/en/docs/http/ngx_http_keyval_module.html"
         lang="en"
-        rev="6">
+        rev="7">
 
 <section id="summary">
 
 <para>
 The <literal>ngx_http_keyval_module</literal> module (1.13.3) creates variables
 with values taken from key-value pairs managed by the
-<link doc="ngx_http_api_module.xml" id="http_keyvals_">API</link>.
+<link doc="ngx_http_api_module.xml" id="http_keyvals_">API</link>
+or a variable (1.15.10) that can also be set with
+<link doc="../njs/examples.xml" id="requests">njs</link>.
 </para>
 
 <para>
--- a/xml/en/docs/njs/examples.xml
+++ b/xml/en/docs/njs/examples.xml
@@ -9,7 +9,7 @@
 <article name="Examples"
         link="/en/docs/njs/examples.html"
         lang="en"
-        rev="6">
+        rev="7">
 
 <section id="helloword" name="Hello World">
 
@@ -305,4 +305,52 @@ function create_secure_link(r) {
 
 </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>
--- a/xml/ru/docs/njs/examples.xml
+++ b/xml/ru/docs/njs/examples.xml
@@ -9,7 +9,7 @@
 <article name="Примеры использования"
         link="/ru/docs/njs/examples.html"
         lang="ru"
-        rev="6">
+        rev="7">
 
 <section id="helloword" name="Hello World">
 
@@ -305,4 +305,52 @@ function create_secure_link(r) {
 
 </section>
 
+
+<section id="requests" name="Запись в лог количества запросов от клиента">
+
+<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>
+Директивы <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> и
+<link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/>
+доступны как часть
+<commercial_version>коммерческой подписки</commercial_version>.
+</note>
+</para>
+
+</section>
+
 </article>