非常教程

Nginx参考手册

ngx_http_limit_req_module

ngx_http_limit_req_module

  • 示例配置
  • 指令
  • limit_req
  • limit_req_log_level
  • limit_req_status
  • limit_req_zone

所述ngx_http_limit_req_module模块(0.7.21)用于限制每一个定义的密钥的请求的处理速率,特别是从一个单一的IP地址的请求的处理速率。限制是使用“泄漏桶”方法完成的。

示例配置

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

    ...

    server {

        ...

        location /search/ {
            limit_req zone=one burst=5;
        }

指令

句法:

limit_req zone = name burst = number;

默认:

语境:

http,服务器,位置

设置共享内存区域和请求的最大突发大小。如果请求速率超过为某个区域配置的速率,则它们的处理会延迟,从而使请求按指定速率处理。过多的请求被延迟,直到它们的数量超过最大突发大小,在这种情况下请求被终止并出现错误。默认情况下,最大突发大小等于零。例如,指令

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

server {
    location /search/ {
        limit_req zone=one burst=5;
    }

平均每秒不超过1个请求,并且突发不超过5个请求。

如果在限制请求的同时延迟请求过多,则nodelay不需要使用该参数:

limit_req zone=one burst=5 nodelay;

可能有几个limit_req指令。例如,以下配置将限制来自单个IP地址的请求的处理速率,同时限制虚拟服务器的请求处理速率:

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;
}

当且仅当limit_req在当前级别上没有指令时,这些指令才从前一级继承。

句法:

limit_req_log_level信息| 通知| 警告| 错误;

默认:

limit_req_log_level错误;

语境:

http,服务器,位置

当服务器由于速率超出而拒绝处理请求或延迟请求处理时,设置所需的日志记录级别。记录等级的延误比拒绝记录低一个点; 例如,如果limit_req_log_level notice指定了“ ”,则延迟与info级别一起记录。

句法:

limit_req_status代码;

默认:

limit_req_status 503;

语境:

http,服务器,位置

设置状态码以响应拒绝的请求而返回。

句法:

limit_req_zone key zone = name:size rate = rate;

默认:

语境:

HTTP

为共享内存区域设置参数,该区域将保留各种键的状态。特别是,国家存储当前的过多请求数。该key可以包含文本,变量,他们的组合。不包括具有空键值的请求。

在版本1.7.6之前,一个 key可能只包含一个变量。

用法示例:

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

在这里,状态保持在10兆字节区域“一”,并且该区域的平均请求处理速率不能超过每秒1个请求。

客户端IP地址作为关键字。需要注意的是,而不是$remote_addr,该$binary_remote_addr变量在这里使用。$binary_remote_addr对于IPv4地址,变量的大小始终为4个字节,对于IPv6地址则为16个字节。存储状态在32位平台上始终占用64个字节,在64位平台上占用128个字节。一个兆字节的区域可以保持大约16000个64字节的状态或大约8千个128字节的状态。

如果区域存储耗尽,最近最少使用的状态将被删除。即使在此之后无法创建新状态,该请求也会因错误而终止。

速率以每秒请求数(r / s)指定。如果需要每秒小于一个请求的速率,则按每分钟请求(r / m)指定。例如,每秒半请求是30r / m。

ngx_http_limit_req_module
Nginx

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module