# Execution order of HttpLimitReq, proxy\_pass and rewrites

**URL:** <https://www.ruby-forum.com/t/execution-order-of-httplimitreq-proxy-pass-and-rewrites/205138>\
**Category:** NGINX\
**Created:** [March 30, 2011, 3:28pm UTC](https://www.ruby-forum.com/t/execution-order-of-httplimitreq-proxy-pass-and-rewrites/205138 "2011-03-30T15:28:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 30, 2011, 3:28pm UTC](https://www.ruby-forum.com/t/execution-order-of-httplimitreq-proxy-pass-and-rewrites/205138/1 "2011-03-30T15:28:29Z")

</div>

Hello,

I have tried to rate limit a resource using HttpLimitReq that is passed  
on to a backend Apache server. Now when I try to limit this resource,  
nothing is blocked and all requests go through. When using static  
resources, the limit\_req works as expected. Here is my config:

location /cgi-bin/test.pl {  
limit\_req zone=one nodelay burst=3;  
}

location /cgi-bin/ {  
proxy\_pass http://default\_backend/cgi-bin/;  
}

I think this might be related to the execution order or these modules.  
Which of the commands limit\_req and proxy\_pass is evaluated first? How  
can I limit the access to resources in a backend server?

Thank you for your help!

Best regards,  
Jonas Kaufmann

Posted at Nginx Forum:

> **[Execution order of HttpLimitReq, proxy\_pass and rewrites](https://forum.nginx.org/read.php?2,186876,186876#msg-186876)**
>
> Hello, I have tried to rate limit a resource using HttpLimitReq that is passed on to a backend Apache server. Now when I try to limit this resource, nothing is blocked and all requests go through. When using static resources, the limit\_req works as...

---

<div class="post-metadata">

**Author:** ![j0nes2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@j0nes2k](https://www.ruby-forum.com/u/j0nes2k)\
**Post date:** [March 30, 2011, 3:42pm UTC](https://www.ruby-forum.com/t/execution-order-of-httplimitreq-proxy-pass-and-rewrites/205138/2 "2011-03-30T15:42:43Z")

</div>

On Wed, Mar 30, 2011 at 09:28:04AM -0400, j0nes2k wrote:

Hi there,

A general rule of nginx is that one request is handled by one location  
block. Only the configuration in, or inherited by, that location  
matters.

(An internal redirect counts as a new request, more or less.)

> when I try to limit this resource,  
> nothing is blocked and all requests go through.

> location /cgi-bin/test.pl {  
> limit\_req zone=one nodelay burst=3;  
> }

There is a limit\_req in this location.

> location /cgi-bin/ {  
> proxy\_pass http://default\_backend/cgi-bin/;  
> }

There’s no limit\_req in this location.

Does that (indirectly) answer your question?

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![j0nes2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@j0nes2k](https://www.ruby-forum.com/u/j0nes2k)\
**Post date:** [March 30, 2011, 3:51pm UTC](https://www.ruby-forum.com/t/execution-order-of-httplimitreq-proxy-pass-and-rewrites/205138/3 "2011-03-30T15:51:59Z")

</div>

Hello Francis,

okay, I see. So nginx seems to (randomly?) choose one of the location  
blocks - in my case it was the lower one using the proxy\_pass directive.

However I have several scripts running under /cgi-bin that need  
different limit\_req settings - there are scripts that need to have a  
strong limit and others do not need a limit at all. I have tried  
something like

location /cgi-bin/ {  
if ($request\_filename ~ test.pl) {  
limit\_req zone=one nodelay burst=3;  
}  
proxy\_pass http://default\_backend/cgi-bin/;  
}

…but unfortunately limit\_req is not allowed in this context. Is there  
another option how I could get limit\_req to work for me?

Thanks!

Best regards,  
Jonas Kaufmann

Posted at Nginx Forum:

> **[Re: Execution order of HttpLimitReq, proxy\_pass and rewrites](https://forum.nginx.org/read.php?2,186876,186880#msg-186880)**
>
> Hello, I have tried to rate limit a resource using HttpLimitReq that is passed on to a backend Apache server. Now when I try to limit this resource, nothing is blocked and all requests go through. When using static resources, the limit\_req works as...

---

<div class="post-metadata">

**Author:** ![j0nes2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@j0nes2k](https://www.ruby-forum.com/u/j0nes2k)\
**Post date:** [March 30, 2011, 4:09pm UTC](https://www.ruby-forum.com/t/execution-order-of-httplimitreq-proxy-pass-and-rewrites/205138/4 "2011-03-30T16:09:17Z")

</div>

On Wed, Mar 30, 2011 at 09:51:16AM -0400, j0nes2k wrote:

> location /cgi-bin/ {  
> if ($request\_filename ~ test.pl) {  
> limit\_req zone=one nodelay burst=3;  
> }  
> proxy\_pass http://default\_backend/cgi-bin/;  
> }
> 
> …but unfortunately limit\_req is not allowed in this context. Is there  
> another option how I could get limit\_req to work for me?

location /cgi-bin/ {  
proxy\_pass http://default\_backend;  
}

location = /cgi-bin/test.pl {  
limit\_req zone=one nodelay burst=3;  
proxy\_pass http://default\_backend;  
}

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![j0nes2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@j0nes2k](https://www.ruby-forum.com/u/j0nes2k)\
**Post date:** [March 30, 2011, 5:36pm UTC](https://www.ruby-forum.com/t/execution-order-of-httplimitreq-proxy-pass-and-rewrites/205138/5 "2011-03-30T17:36:27Z")

</div>

On Wed, Mar 30, 2011 at 09:51:16AM -0400, j0nes2k wrote:

Hi there,

> okay, I see. So nginx seems to (randomly?) choose one of the location  
> blocks - in my case it was the lower one using the proxy\_pass directive.

There shouldn’t be any randomness involved.

There’s a Fine Manual at [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpCoreModule#location)

> However I have several scripts running under /cgi-bin that need  
> different limit\_req settings - there are scripts that need to have a  
> strong limit and others do not need a limit at all.

As Igor showed, use multiple location blocks.

> I have tried something like
> 
> location /cgi-bin/ {  
> if ($request\_filename ~ test.pl) {

Note that there’s also a special warning about if() inside location{}  
at [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/IfIsEvil) – anything other than “return” or  
“rewrite … last” is likely to cause confusion.

> …but unfortunately limit\_req is not allowed in this context. Is there  
> another option how I could get limit\_req to work for me?

If your scripts don’t use PATH\_INFO, then something like

location = /script/one {  
limit\_req …;  
proxy\_pass …;  
}

and if they do, then something like

location ^~ /script/two {} or location ^~ /script/two/ {}

with the same contents.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![j0nes2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@j0nes2k](https://www.ruby-forum.com/u/j0nes2k)\
**Post date:** [March 30, 2011, 8:52pm UTC](https://www.ruby-forum.com/t/execution-order-of-httplimitreq-proxy-pass-and-rewrites/205138/6 "2011-03-30T20:52:21Z")

</div>

Hello Igor and Francis,

amazing, thank you for your help! Everything works right now!

Best regards  
Jonas Kaufmann

Posted at Nginx Forum:

> **[Re: Execution order of HttpLimitReq, proxy\_pass and rewrites](https://forum.nginx.org/read.php?2,186876,186955#msg-186955)**
>
> Hello, I have tried to rate limit a resource using HttpLimitReq that is passed on to a backend Apache server. Now when I try to limit this resource, nothing is blocked and all requests go through. When using static resources, the limit\_req works as...
