# Limit\_req and @named locations

**URL:** <https://www.ruby-forum.com/t/limit-req-and-named-locations/236673>\
**Category:** NGINX\
**Created:** [September 24, 2013, 3:13pm UTC](https://www.ruby-forum.com/t/limit-req-and-named-locations/236673 "2013-09-24T15:13:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [September 24, 2013, 3:13pm UTC](https://www.ruby-forum.com/t/limit-req-and-named-locations/236673/1 "2013-09-24T15:13:41Z")

</div>

Hi list

I am experiencing some problems with a rate-limiting setup.

I have a “global” limit\_req declared in my http block.

I also have additional limit\_req declarations in various locations, both  
@named and unnamed, to provide proper protection to different backend  
endpoints.

It seems that additional limit\_req is working fine in unnamed locations,  
but  
being ignored in @named locations.

I’ve linked to an example config exhibiting the problem:

> <https://gist.github.com/anonymous/bf347d5302b3463f970b>

If I remove the limit\_req in the http block, the @dynamic limit\_req  
works  
perfectly. If I enable limit\_req in the http block, the @dynamic  
limit\_req  
is ignored. In both cases, the limit\_req in the unnamed location works  
perfectly!

Am I approaching this in the wrong way? How can I make limit\_req work  
properly in the @dynamic location?

thanks

Rob

Posted at Nginx Forum:

> **[limit\_req and @named locations](https://forum.nginx.org/read.php?2,243093,243093#msg-243093)**
>
> Hi list I am experiencing some problems with a rate-limiting setup. I have a "global" limit\_req declared in my http block. I also have additional limit\_req declarations in various locations, both @named and unnamed, to provide proper...

---

<div class="post-metadata">

**Author:** ![robw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8edcca/32.png) [@robw](https://www.ruby-forum.com/u/robw)\
**Post date:** [September 24, 2013, 4:39pm UTC](https://www.ruby-forum.com/t/limit-req-and-named-locations/236673/2 "2013-09-24T16:39:01Z")

</div>

Hello!

On Tue, Sep 24, 2013 at 09:12:54AM -0400, robw wrote:

> It seems that additional limit\_req is working fine in unnamed locations, but  
> Am I approaching this in the wrong way? How can I make limit\_req work  
> properly in the @dynamic location?

The limit\_req directives are executed only once per request  
lifetime. If a request was checked against any limits configured  
in a location - regardless of future internal redirects further  
limits won’t be checked. This is a simple way to protect requests  
from checking against the same limits again and again after  
redirections.

In your configuration limit\_req STATIC\_LIMIT\_REQ is checked for  
all requests matching location /, and requests are redirected to  
@dynamic location via try\_files only after this.

There are two basic ways to fix things:

1. Select a location before limit\_req’s are executed. Recommended  
way is to use separate URL prefixes and use prefix string  
locations, like you already do with /admin. (Alternatively, you  
may also use return + error\_page to switch to a named location as  
rewrite module directives are executed before limit\_req’s, but  
this kind of defeats simplicity of try\_files.)

2. Avoid using any limits in locations which aren’t a final match.  
This is essentially what happens if you remove the limit\_req in the  
http block.

–  
Maxim D.  
[http://nginx.org/en/donation.html](http://nginx.org/en/donation.html)

---

<div class="post-metadata">

**Author:** ![robw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8edcca/32.png) [@robw](https://www.ruby-forum.com/u/robw)\
**Post date:** [September 24, 2013, 9:24pm UTC](https://www.ruby-forum.com/t/limit-req-and-named-locations/236673/3 "2013-09-24T21:24:16Z")

</div>

Thank you for clarifying

Posted at Nginx Forum:

> **[Re: limit\_req and @named locations](https://forum.nginx.org/read.php?2,243093,243105#msg-243105)**
>
> Hi list I am experiencing some problems with a rate-limiting setup. I have a "global" limit\_req declared in my http block. I also have additional limit\_req declarations in various locations, both @named and unnamed, to provide proper...
