Reputation: 115
is it possible to use dynamic connection limits in nginx?
lets say i have the following routes:
/route/1
/route/2
...
/route/*
I do not want to have a global rate limit for /route/* but a specific for each route. Is that possible in nginx?
So that each route have a connection limit of 2 connections in a minute.
What i think of: everything that comes after /route/ should be act as an id. And each id has its own connection limit.
Maybe i could be somehting like:
limit_conn_zone $request_uri zone=addr:10m;
server {
...
limit_conn addr 1;
}
But im not sure, if this works as i expect.
Upvotes: 1
Views: 3504
Reputation: 4445
limit_conn
can be used inside location
block. But limit_conn
restrict number of simultaneous connections. If you want to limit rate, you can use limit_req
module http://nginx.org/en/docs/http/ngx_http_limit_req_module.html which can be used inside location too.
Also, if you want separate limits for each location - there is two ways. First - separate zones (limit_req_zone
) for each location. Second - one zone, but using route as key. First case usually better due memory usage, but in you case (unlimited number of routes) second way is better. So, just extract your ID from route and use it as limit_req_zone
key.
limit_req_zone $myid zone=one:50m rate=2r/m;
...
location ~ ^/route/(?<myid>\d+) {
limit_req zone=one;
}
If you need separate limit for each location for each client IP address, use limit_req_zone $binary_remote_addr$myid ...
key.
Upvotes: 3