Rate limiting for try_files

Francis Daly francis at daoine.org
Sun Jan 27 21:42:01 UTC 2019


On Sat, Jan 26, 2019 at 10:05:03PM +0100, Marco Colli wrote:

Hi there,

> I cannot figure out how to apply a rate limit *only to static files* served
> with try_files (and not for @app location). Is it possible?

The request is handled in a location. Only the config in, or inherited
in to, that location matters. For try_files files, the location is the
one that the try_files is in.

> server {

> try_files $uri @app;

Does it break anything to put that inside "location / {}"?

> location @app {

> I know that I can use the following:
> limit_req zone=mylimit burst=50 nodelay;
> 
> However where should I put that line?

In the location that you want it to apply.

> If I put it in the "server", then
> also my @app will use that settings, which is not what I want... I just
> want to put a limit specific for files served using try_files. Note that
> static files can be in any location, even the root folder (e.g.
> /favicon.ico and many others).

Put it in each of those location{}s too.

> Any help would be greatly appreciated

The other option is to apply the limit_req at server level, so that it
will inherit in to all locations{}s that do not have their own limit_req
directive.

And then add a "no limit" limit_req directive in those location{}s.

I think that you can do *that*, by adding something like

  limit_req_zone "" zone=off:32k rate=7r/m;

(where "" is a key that will always be empty and so will not limit
anything; "off" is a string to use as the zone name, "32k" is the
minimum size allowed on my test machine (8x page size), and the "rate"
is something that is odd enough that you will hopefully recognise if if
things go wrong, and this *does* ever start limiting anything.)

and then adding "limit_req zone=off;" in the appropriate places.

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list