Mercurial > hg > nginx-site
diff xml/en/docs/njs/examples.xml @ 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 | 86b0dd6e7208 |
children | b60e5be733cd |
line wrap: on
line diff
--- a/xml/en/docs/njs/examples.xml Tue Oct 29 19:21:03 2019 +0300 +++ b/xml/en/docs/njs/examples.xml Thu Oct 31 15:33:19 2019 +0300 @@ -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 @@ </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>