1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
=encoding utf-8
=head1 NAME
ngx_http_limit_req_module - Module ngx_http_limit_req_module
=head1
The C<ngx_http_limit_req_module> module (0.7.21) is used
to limit the request processing rate per a defined key,
in particular, the processing rate of requests coming
from a single IP address.
The limitation is done using the “leaky bucket” method.
=head1 Example Configuration
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
...
server {
...
location /search/ {
limit_req zone=one burst=5;
}
=head1 Directives
=head2 limit_req
B<syntax:> limit_req I<
C<zone>=I<C<name>>
[C<burst>=I<C<number>>]
[C<nodelay> E<verbar>
C<delay>=I<C<number>>]>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
Sets the shared memory zone
and the maximum burst size of requests.
If the requests rate exceeds the rate configured for a zone,
their processing is delayed such that requests are processed
at a defined rate.
Excessive requests are delayed until their number exceeds the
maximum burst size
in which case the request is terminated with an
error.
By default, the maximum burst size is equal to zero.
For example, the directives
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
location /search/ {
limit_req zone=one burst=5;
}
allow not more than 1 request per second at an average,
with bursts not exceeding 5 requests.
If delaying of excessive requests while requests are being limited is not
desired, the parameter C<nodelay> should be used:
limit_req zone=one burst=5 nodelay;
The C<delay> parameter (1.15.7) specifies a limit
at which excessive requests become delayed.
Default value is zero, i.e. all excessive requests are delayed.
There could be several C<limit_req> directives.
For example, the following configuration will limit the processing rate
of requests coming from a single IP address and, at the same time,
the request processing rate by the virtual server:
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;
server {
...
limit_req zone=perip burst=5 nodelay;
limit_req zone=perserver burst=10;
}
These directives are inherited from the previous configuration level
if and only if there are no C<limit_req> directives
defined on the current level.
=head2 limit_req_dry_run
B<syntax:> limit_req_dry_run I<C<on> E<verbar> C<off>>
B<default:> I<off>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
This directive appeared in version 1.17.1.
Enables the dry run mode.
In this mode, requests processing rate is not limited, however,
in the shared memory zone, the number of excessive requests is accounted
as usual.
=head2 limit_req_log_level
B<syntax:> limit_req_log_level I<
C<info> E<verbar>
C<notice> E<verbar>
C<warn> E<verbar>
C<error>>
B<default:> I<error>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
This directive appeared in version 0.8.18.
Sets the desired logging level
for cases when the server refuses to process requests
due to rate exceeding,
or delays request processing.
Logging level for delays is one point less than for refusals; for example,
if “C<limit_req_log_level notice>” is specified,
delays are logged with the C<info> level.
=head2 limit_req_status
B<syntax:> limit_req_status I<I<C<code>>>
B<default:> I<503>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
This directive appeared in version 1.3.15.
Sets the status code to return in response to rejected requests.
=head2 limit_req_zone
B<syntax:> limit_req_zone I<
I<C<key>>
C<zone>=I<C<name>>:I<C<size>>
C<rate>=I<C<rate>>
[C<sync>]>
B<context:> I<http>
Sets parameters for a shared memory zone
that will keep states for various keys.
In particular, the state stores the current number of excessive requests.
The I<C<key>> can contain text, variables, and their combination.
Requests with an empty key value are not accounted.
B<NOTE>
Prior to version 1.7.6, a I<C<key>> could contain exactly one variable.
Usage example:
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
Here, the states are kept in a 10 megabyte zone “one”, and an
average request processing rate for this zone cannot exceed
1 request per second.
A client IP address serves as a key.
Note that instead of C<$remote_addr>, the
C<$binary_remote_addr> variable is used here.
The C<$binary_remote_addr> variable’s size
is always 4 bytes for IPv4 addresses or 16 bytes for IPv6 addresses.
The stored state always occupies
64 bytes on 32-bit platforms and 128 bytes on 64-bit platforms.
One megabyte zone can keep about 16 thousand 64-byte states
or about 8 thousand 128-byte states.
If the zone storage is exhausted, the least recently used state is removed.
If even after that a new state cannot be created, the request is terminated with
an error.
The rate is specified in requests per second (rE<sol>s).
If a rate of less than one request per second is desired,
it is specified in request per minute (rE<sol>m).
For example, half-request per second is 30rE<sol>m.
The C<sync> parameter (1.15.3) enables
L<synchronization|ngx_stream_zone_sync_module>
of the shared memory zone.
B<NOTE>
The C<sync> parameter is available as part of our
commercial subscription.
B<NOTE>
Additionally, as part of our
commercial subscription,
the
L<status information|ngx_http_api_module>
for each such shared memory zone can be
L<obtained|ngx_http_api_module> or
L<reset|ngx_http_api_module>
with the L<API|ngx_http_api_module> since 1.17.7.
=head1 Embedded Variables
=over
=item C<$limit_req_status>
keeps the result of limiting the request processing rate (1.17.6):
C<PASSED>,
C<DELAYED>,
C<REJECTED>,
C<DELAYED_DRY_RUN>, or
C<REJECTED_DRY_RUN>
=back
|