2
votes

Is there are a way to enable rate limiting only for successful requests (i.e. HTTP status code 200)?

For example, in the following snippet from my configuration...

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

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

        ...
    }

    ...
}

...requests are successfully rate limited (up to two requests per minute).

However, as this is for a contact form which sends me emails, I do not care about rate limiting if http://localhost:3000/ returns an error as no email will have been sent.

1

1 Answers

4
votes

No, there isn't.

Nginx processes HTTP request in 11 phases from reading request to sending reponse: post-read, server-rewrite, find-config, rewrite, post-rewrite, pre-access, access, post-access, try-files, content, log.

proxy_pass is in content phase while limit_req is in pre-access phase (refer ngx_http_limit_req_module.c), pre-access phase handlers is executed before content phase handlers, so limit_req handler can not check if the response code is OK.