NGINX 仅对成功请求启用速率限制

Zak*_*Zak 2 reverse-proxy nginx rate-limiting

有没有办法只对成功的请求(即 HTTP 状态代码 200)启用速率限制?

例如,在我的配置的以下片段中......

http {
    limit_req_zone $binary_remote_addr zone=test:10m rate=2r/m;

    server {
        location / {
             limit_req zone=test;
             proxy_pass http://localhost:3000/;
             ...
        }

        ...
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

...请求成功速率受到限制(每分钟最多两个请求)。

但是,由于这是用于向我发送电子邮件的联系表单,因此如果返回错误,我不关心速率限制,http://localhost:3000/因为不会发送任何电子邮件。

Lar*_*.He 5

不,没有。

Nginx 从读取请求到发送响应分 11 个阶段处理 HTTP 请求:读后、服务器重写、查找配置、重写、重写后、预访问、访问、后访问、尝试文件、内容、日志。

proxy_passis in content phasewhile limit_reqis in pre-access phase(refer ngx_http_limit_req_module.c),pre-access phasehandlers 在 handlers 之前执行content phase,因此 limit_req handler 无法检查响应代码是否正确。