跳到主要内容

Limit Req 配置选项

limit-req 插件使用漏桶算法实施 API 限速,以限制请求速率并管理流量。 配置涵盖插件参数。

参数​

有关所有插件均可使用的配置项,请参阅插件通用配置。

  • rate

    number

    必填

    有效值:

    大于 0

    每秒允许的请求数。超过该速率的请求会占用配置的 burst 容量。除非 nodelay 为 true,否则这些请求会延迟处理。

  • burst

    number

    必填

    有效值:

    大于或等于 0

    用于容纳超出 rate 的请求。超出该容量的请求会被拒绝。容量内的请求会延迟处理,除非 nodelay 为 true。该值的单位为请求数,而非每秒请求数。

  • key_type

    string

    默认值:var

    有效值:

    var 或 var_combination

    键的类型。

    如果 key_type 为 var,则 key 被解释为变量。

    如果 key_type 为 var_combination,则 key 被解释为变量组合。

  • key

    string

    必填


    用于计数请求的键。

    如果 key_type 为 var,则 key 被解释为变量。变量不需要以美元符号($)作为前缀。查看内置变量以获取可用变量。

    如果 key_type 为 var_combination,则 key 被解释为变量组合。所有变量都应以美元符号($)作为前缀。例如,要将 key 配置为使用两个请求头 custom-a 和 custom-b 的组合,key 应配置为 $http_custom_a $http_custom_b。

  • rejected_code

    integer

    默认值:503

    有效值:

    介于 200 和 599 之间(含边界值)

    当请求因超过阈值而被拒绝时返回的 HTTP 状态码。

  • rejected_msg

    string

    有效值:

    任意非空字符串

    当请求因超过阈值而被拒绝时返回的响应体。

  • nodelay

    boolean

    默认值:false


    如果为 true,则不延迟 burst 阈值内的请求。

  • allow_degradation

    boolean

    默认值:false


    如果为 true,则在插件或其依赖项不可用时,允许网关继续处理请求而不使用该插件。

  • policy

    string

    默认值:local

    有效值:

    local、redis 或 redis-cluster

    速率限制计数器的策略。如果为 local,则计数器存储在本地内存中。如果为 redis,则计数器存储在 Redis 实例中。如果为 redis-cluster,则计数器存储在 Redis 集群中。

    在 API7 企业版中,该参数自 3.9.0 起为必填项;在 APISIX 中为可选项。

  • redis_host

    string


    Redis 节点的地址。当 policy 为 redis 时必填。

  • redis_port

    integer

    默认值:6379

    有效值:

    大于或等于 1

    Redis 节点的端口。当 policy 为 redis 时使用。

  • redis_username

    string


    如果使用 Redis ACL,则为 Redis 的用户名。如果你使用传统的认证方法 requirepass,则只需配置 redis_password。当 policy 为 redis 时使用。

  • redis_password

    string


    当 policy 为 redis 或 redis-cluster 时 Redis 节点的密码。API7 企业版会对静态存储的该值进行加密;在 APISIX 中,请启用数据加密,以便在存入 etcd 前加密该值。加密能力自 API7 企业版 3.9.16 和 3.10.2,以及 APISIX 3.18.0 起可用。

  • redis_database

    integer

    默认值:0

    有效值:

    大于或等于 0

    Redis 中的数据库编号。当 policy 为 redis 时使用。

  • redis_ssl

    boolean

    默认值:false


    如果为 true,则在 policy 为 redis 时使用 SSL 连接到 Redis。

  • redis_ssl_verify

    boolean

    默认值:false


    如果为 true,则在 policy 为 redis 时验证服务器 SSL 证书。

  • redis_server_name

    string


    用于 SNI 的 TLS 服务器名称;启用证书验证时,也用于主机名验证。适用于 policy 为 redis 且 redis_ssl 为 true 的情况。默认为 redis_host。所选值为 IP 地址时,不发送服务器名称。若通过 IP 地址连接但需要验证 DNS 名称证书,请将该字段设为证书中的 DNS 名称,并启用 redis_ssl_verify。

    该字段在 API7 企业版 3.9.21、3.10.8 和 APISIX 3.19.0 中引入。该字段适用于单节点 Redis 连接器,不适用于 Redis Cluster 或 Sentinel。

  • redis_timeout

    integer

    默认值:1000

    有效值:

    大于或等于 1

    Redis 超时时间(毫秒)。当 policy 为 redis 或 redis-cluster 时使用。

  • redis_keepalive_timeout

    integer

    默认值:10000

    有效值:

    大于或等于 1000

    当 policy 为 redis 或 redis-cluster 时,Redis 的 keepalive 超时时间(毫秒)。

    此参数在 API7 企业版 3.9.x 系列中自 3.9.17 起可用,在 3.10.x 系列中自 3.10.4 起可用,并且在 APISIX 中自 3.15.0 起可用。

  • redis_keepalive_pool

    integer

    默认值:100

    有效值:

    大于或等于 1

    当 policy 为 redis 或 redis-cluster 时,Redis 的 keepalive 连接池大小。

    此参数在 API7 企业版 3.9.x 系列中自 3.9.17 起可用,在 3.10.x 系列中自 3.10.4 起可用,并且在 APISIX 中自 3.15.0 起可用。

  • redis_cluster_nodes

    array[string]


    Redis 集群节点列表,至少包含一个地址。当 policy 为 redis-cluster 时必填。

  • redis_cluster_name

    string


    Redis 集群的名称。当 policy 为 redis-cluster 时必填。

  • redis_cluster_ssl

    boolean

    默认值:false


    如果为 true,则在 policy 为 redis-cluster 时使用 SSL 连接到 Redis 集群。

  • redis_cluster_ssl_verify

    boolean

    默认值:false


    如果为 true,则在 policy 为 redis-cluster 时验证服务器 SSL 证书。