# Nginx 0.7 and limit\_req

**URL:** <https://www.ruby-forum.com/t/nginx-0-7-and-limit-req/155286>\
**Category:** NGINX\
**Created:** [December 10, 2008, 7:46am UTC](https://www.ruby-forum.com/t/nginx-0-7-and-limit-req/155286 "2008-12-10T07:46:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Omar\_Kilani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8edcca/32.png) [@Omar\_Kilani](https://www.ruby-forum.com/u/Omar_Kilani)\
**Post date:** [December 10, 2008, 7:46am UTC](https://www.ruby-forum.com/t/nginx-0-7-and-limit-req/155286/1 "2008-12-10T07:46:21Z")

</div>

Hi Igor,

I’ve recently started using the new limit\_req module in nginx 0.7 to  
try to throttle requests to the API of our web service.

We’ve been having some issues in that ‘delayed’ requests seem to be  
returned with a 503 HTTP response header, but with the correct body  
(the limit\_req is under a ‘location’ block with proxy\_pass).

The configuration is:

limit\_req\_zone $binary\_remote\_addr zone=api\_one:10m rate=1r/s;

location /services/rest {  
limit\_req zone=api\_one burst=5;  
proxy\_pass http://rtm\_api;  
…  
}

My intention is to limit requests to an average of 1 request a second,  
burstable to 5 requests, and supporting delaying requests until  
they’re within that 1 request / second threshold.

Is the 503 response a known issue?

Also, could you tell me how to test this particular configuration to  
make sure it’s correct?

I’ve tried ab/http\_load/siege, etc.

Thanks for your time!

Regards,  
Omar

---

<div class="post-metadata">

**Author:** ![Omar\_Kilani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8edcca/32.png) [@Omar\_Kilani](https://www.ruby-forum.com/u/Omar_Kilani)\
**Post date:** [December 10, 2008, 8:44am UTC](https://www.ruby-forum.com/t/nginx-0-7-and-limit-req/155286/2 "2008-12-10T08:44:52Z")

</div>

On Wed, Dec 10, 2008 at 05:36:22PM +1100, Omar Kilani wrote:

> I’ve recently started using the new limit\_req module in nginx 0.7 to  
> try to throttle requests to the API of our web service.
> 
> We’ve been having some issues in that ‘delayed’ requests seem to be  
> returned with a 503 HTTP response header, but with the correct body  
> (the limit\_req is under a ‘location’ block with proxy\_pass).

The 503 response should have special body.

> My intention is to limit requests to an average of 1 request a second,  
> burstable to 5 requests, and supporting delaying requests until  
> they’re within that 1 request / second threshold.
> 
> Is the 503 response a known issue?

limit\_req delays excess request until they wil be more than burst value.  
This means that if you send 6 simultaneous requests, then 1 will be  
processed, next 4 will be delayed for 1s, 2s, 3, and 4s, and 6th will be  
reject with 503 code.

---

<div class="post-metadata">

**Author:** ![Omar\_Kilani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8edcca/32.png) [@Omar\_Kilani](https://www.ruby-forum.com/u/Omar_Kilani)\
**Post date:** [December 10, 2008, 9:38am UTC](https://www.ruby-forum.com/t/nginx-0-7-and-limit-req/155286/3 "2008-12-10T09:38:47Z")

</div>

Hey Igor,

On Wed, Dec 10, 2008 at 6:32 PM, Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wed, Dec 10, 2008 at 05:36:22PM +1100, Omar Kilani wrote:
> 
> > I’ve recently started using the new limit\_req module in nginx 0.7 to  
> > try to throttle requests to the API of our web service.
> > 
> > We’ve been having some issues in that ‘delayed’ requests seem to be  
> > returned with a 503 HTTP response header, but with the correct body  
> > (the limit\_req is under a ‘location’ block with proxy\_pass).
> 
> The 503 response should have special body.

Okay, then I have a strange issue.

Basically, when the nginx error.log says a request is ‘delayed’, nginx  
will sometimes output a 503 HTTP response code but with the same body  
as a 200 (the request goes to the backend, and the backend response is  
sent to the client).

When nginx error.log says a request is ‘limited’, then I’m getting a  
503 + the 503 error (and the request doesn’t go to the backend) as  
expected.

> > My intention is to limit requests to an average of 1 request a second,  
> > burstable to 5 requests, and supporting delaying requests until  
> > they’re within that 1 request / second threshold.
> > 
> > Is the 503 response a known issue?
> 
> limit\_req delays excess request until they wil be more than burst value.  
> This means that if you send 6 simultaneous requests, then 1 will be  
> processed, next 4 will be delayed for 1s, 2s, 3, and 4s, and 6th will be  
> reject with 503 code.

Great, that’s what I expected.

BTW, have you seen any issues with truncated output of ‘delayed’  
responses when using limit\_req?

We’ve had a (admittedly low) number of requests which return a  
truncated body. These are logged in error.log as a client timeout, so  
it may just be that.

I personally haven’t been able to reproduce these, so it could be some  
weird interaction with a particular HTTP library/client.

> > Also, could you tell me how to test this particular configuration to  
> > make sure it’s correct?
> > 
> > I’ve tried ab/http\_load/siege, etc.
> 
> –  
> Igor S.  
> [http://sysoev.ru/en/](http://sysoev.ru/en/)

Regards,  
Omar

---

<div class="post-metadata">

**Author:** ![Omar\_Kilani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8edcca/32.png) [@Omar\_Kilani](https://www.ruby-forum.com/u/Omar_Kilani)\
**Post date:** [December 10, 2008, 11:35am UTC](https://www.ruby-forum.com/t/nginx-0-7-and-limit-req/155286/4 "2008-12-10T11:35:02Z")

</div>

On Wed, Dec 10, 2008 at 07:28:22PM +1100, Omar Kilani wrote:

> > > (the limit\_req is under a ‘location’ block with proxy\_pass).
> > 
> > The 503 response should have special body.
> 
> Okay, then I have a strange issue.
> 
> Basically, when the nginx error.log says a request is ‘delayed’, nginx  
> will sometimes output a 503 HTTP response code but with the same body  
> as a 200 (the request goes to the backend, and the backend response is  
> sent to the client).

In “delayed” case nginx should return response without 503 at all.  
The request is simple delayed before passing it to a backend.

> > > ```
> > > proxy_pass http://rtm_api;
> > > 
> > > ```
> > 
> > This means that if you send 6 simultaneous requests, then 1 will be  
> > it may just be that.
> 
> I personally haven’t been able to reproduce these, so it could be some  
> weird interaction with a particular HTTP library/client.

If you use ab it may be its issue: ab sends more requests than you  
have specified and then closes simply excess ones.

---

<div class="post-metadata">

**Author:** ![Omar\_Kilani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8edcca/32.png) [@Omar\_Kilani](https://www.ruby-forum.com/u/Omar_Kilani)\
**Post date:** [March 25, 2012, 7:06pm UTC](https://www.ruby-forum.com/t/nginx-0-7-and-limit-req/155286/5 "2012-03-25T19:06:43Z")

</div>

Extended description of HttpLimitZoneModule and HttpLimitReqModule usage  
with examples and how to exclude a whitelist of IPs can be found here

> **[Limit requests per IP on Nginx using HttpLimitZoneModule and...](https://gadelkareem.com/2012/03/25/limit-requests-per-ip-on-nginx-using-httplimitzonemodule-and-httplimitreqmodule-except-whitelist/)**
>
> – Make sure to check Nginx, PHP posts for information on Nginx and PHP setup and configuration. Nginx offers two modules, HttpLimitReqModule and HttpLimitZoneModule, to limit simultaneous connections for the...
