illdo
illdo

Reputation: 53

Nginx limit requests by parameter in body or query?

I need to limit (GET,POST) requests with specific parameter containing in body or query parameters. How to configure NGINX for that?

Current nginx config:

limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
server {
    listen       8044;
    limit_req_log_level warn;
    location /route/{
       limit_req zone=mylimit burst=1000;
       proxy_pass https://the_Cool_server/;
 }}

For example- the request like:

http://localhost:8044/route/path1/path2?param1=param1VALUE_to_limit1&param2VALUEAWESOME

I need the NGINX to limit requests with param1 value per second. So, every request with same param1 should be limited to 10 per second.

Maybe i should use openresty for it?

Upvotes: 2

Views: 3319

Answers (1)

tombak2
tombak2

Reputation: 66

limit_req parameter can be inside http, server or location block, so traditionally you can only limit requests by path, like:

http://localhost:8044/route/path1/path2

but, there is a workaround to map visitor ip to variable only if he hits your query string key (param1), then use that var ($do_limit) in limit_req directive, like:

map $arg_ctl    $do_limit {
    "param1"    $binary_remote_addr;
    default     "";
}
limit_req_zone $do_limit zone=mylimit:10m rate=10r/s;
server {
    listen       8044;
    limit_req_log_level warn;
    location /route/{
       limit_req zone=mylimit burst=1000;
       proxy_pass https://the_Cool_server/;
 }}

Upvotes: 3

Related Questions