Skip to content

Commit 0e589b6

Browse files
committed
[add] http/logging/num_requests example
1 parent 36f9069 commit 0e589b6

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

‎README.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ Note: the examples below work with njs >= `0.7.0 <http://nginx.org/en/docs/njs/c
1414

1515
Running inside Docker
1616
---------------------
17+
Public nginx docker image contains open source version of nginx. To run examples for NGINX-PLUS, you have to `build <https://www.nginx.com/blog/deploying-nginx-nginx-plus-docker/>`_ your own docker image.
1718

1819
.. code-block:: shell
1920
2021
git clone https://github.com/nginx/njs-examples
2122
cd njs-examples
2223
EXAMPLE='http/hello'
2324
docker run --rm --name njs_example -v $(pwd)/conf/$EXAMPLE.conf:/etc/nginx/nginx.conf:ro -v $(pwd)/njs/:/etc/nginx/njs/:ro -p 80:80 -p 443:443 -d nginx
25+
# for NGINX-PLUS examples,
26+
# docker run ... -d mynginxplus
2427
2528
# Stopping.
2629
docker stop njs_example
@@ -1093,6 +1096,73 @@ Checking:
10931096
curl http://localhost/
10941097
hello world
10951098
1099+
Logging
1100+
-------
1101+
1102+
Logging the Number of Requests Per Client [http/logging/num_requests]
1103+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1104+
1105+
.. note:: The `keyval <http://nginx.org/en/docs/http/ngx_http_keyval_module.html#keyval>`_ and `keyval_zone <http://nginx.org/en/docs/http/ngx_http_keyval_module.html#keyval_zone>`_ directives are available as part of our `commercial subscription <https://www.nginx.com/products/nginx/>`_.
1106+
1107+
In this example `keyval <http://nginx.org/en/docs/http/ngx_http_keyval_module.html#keyval>`_ is used to count (accross all nginx workers) the incoming requests from the same ip address.
1108+
1109+
nginx.conf:
1110+
1111+
.. code-block:: nginx
1112+
1113+
...
1114+
1115+
http {
1116+
js_path "/etc/nginx/njs/";
1117+
1118+
js_import main from http/logging/num_requests.js;
1119+
1120+
js_set $num_requests http.num_requests;
1121+
1122+
keyval_zone zone=foo:10m;
1123+
1124+
keyval $remote_addr $foo zone=foo;
1125+
1126+
log_format bar '$remote_addr [$time_local] $num_requests';
1127+
1128+
access_log logs/access.log bar;
1129+
1130+
server {
1131+
listen 80;
1132+
1133+
location / {
1134+
return 200;
1135+
}
1136+
}
1137+
}
1138+
1139+
example.js:
1140+
1141+
.. code-block:: js
1142+
1143+
function num_requests(r) {
1144+
var n = r.variables.foo;
1145+
n = n ? Number(n) + 1 : 1;
1146+
r.variables.foo = n;
1147+
return n;
1148+
}
1149+
1150+
export default {num_requests};
1151+
1152+
Checking:
1153+
1154+
.. code-block:: shell
1155+
1156+
curl http://localhost/aa; curl http://localhost/aa; curl http://localhost/aa
1157+
curl --interface 127.0.0.2 http://localhost/aa; curl --interface 127.0.0.2 http://localhost/aa
1158+
1159+
docker logs njs_example
1160+
127.0.0.1 [22/Nov/2021:16:55:06 +0000] 1
1161+
127.0.0.1 [22/Nov/2021:16:55:07 +0000] 2
1162+
127.0.0.1 [22/Nov/2021:16:55:29 +0000] 3
1163+
127.0.0.2 [22/Nov/2021:18:20:24 +0000] 1
1164+
127.0.0.2 [22/Nov/2021:18:20:25 +0000] 2
1165+
10961166
Stream
10971167
======
10981168

‎conf/http/logging/num_requests.conf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load_module modules/ngx_http_js_module.so;
2+
3+
events { }
4+
5+
http {
6+
js_path "/etc/nginx/njs/";
7+
8+
js_import main from http/logging/num_requests.js;
9+
10+
js_set $num_requests http.num_requests;
11+
12+
keyval_zone zone=foo:10m;
13+
14+
keyval $remote_addr $foo zone=foo;
15+
16+
log_format bar '$remote_addr [$time_local] $num_requests';
17+
18+
access_log logs/access.log bar;
19+
20+
server {
21+
listen 80;
22+
23+
location / {
24+
return 200;
25+
}
26+
}
27+
}

‎njs/http/logging/num_requests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function num_requests(r) {
2+
var n = r.variables.foo;
3+
n = n ? Number(n) + 1 : 1;
4+
r.variables.foo = n;
5+
return n;
6+
}
7+
8+
export default {num_requests};

0 commit comments

Comments
 (0)