You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+111
Original file line number
Diff line number
Diff line change
@@ -1349,6 +1349,117 @@ Checking:
1349
1349
127.0.0.2 [22/Nov/2021:18:20:24 +0000] 1
1350
1350
127.0.0.2 [22/Nov/2021:18:20:25 +0000] 2
1351
1351
1352
+
Shared Dictionary
1353
+
-----------------
1354
+
1355
+
HTTP Rate limit[http/rate-limit/simple]
1356
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1357
+
1358
+
In this example `js_shared_dict_zone <https://nginx.org/en/docs/http/ngx_http_js_module.html#js_shared_dict_zone>`_ is used to implement a simple rate limit and can be set in different contexts.
1359
+
The rate limit is implemented using a shared dictionary zone and a simple javascript function that is called for each request and increments the counter for the current window.
1360
+
If the counter exceeds the limit, the function returns the number of seconds until the end of the window. The function is called using
1361
+
`js_set <https://nginx.org/en/docs/http/ngx_http_js_module.html#js_set>`_ and the result is stored in a variable that is used to return a 429 response if the limit is exceeded.
1362
+
1363
+
nginx.conf:
1364
+
1365
+
.. code-block:: nginx
1366
+
1367
+
http {
1368
+
js_path "/etc/nginx/njs/";
1369
+
js_import main from http/rate-limit/simple.js;
1370
+
# optionally set timeout so NJS resets and deletes all data for ratelimit counters
0 commit comments