# Limit robots

**URL:** <https://www.ruby-forum.com/t/limit-robots/215713>\
**Category:** NGINX\
**Created:** [January 13, 2012, 2:36pm UTC](https://www.ruby-forum.com/t/limit-robots/215713 "2012-01-13T14:36:38Z")\
**Posts on this page:** 5\
**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:** [January 13, 2012, 2:36pm UTC](https://www.ruby-forum.com/t/limit-robots/215713/1 "2012-01-13T14:36:38Z")

</div>

Hello,

Is there a chance to limit robots to 1 request per second?  
The down below does not work: [emerg] “limit\_req” directive is not  
allowed here

Thanks a lot  
Markus

http {  
limit\_req\_zone $http\_user\_agent zone=useragenttrack:1m  
rate=1r/s;  
server {  
listen 80;  
location / {  
if ($http\_user\_agent ~\* “[a-z]bot[^a-z]”) {  
limit\_req zone=useragenttrack burst=100 nodelay;  
}  
}  
}  
}

Posted at Nginx Forum:

> **[Limit robots](https://forum.nginx.org/read.php?2,221162,221162#msg-221162)**
>
> Hello, Is there a chance to limit robots to 1 request per second? The down below does not work: \[emerg\] "limit\_req" directive is not allowed here Thanks a lot Markus http { limit\_req\_zone $http\_user\_agent

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 13, 2012, 2:52pm UTC](https://www.ruby-forum.com/t/limit-robots/215713/2 "2012-01-13T14:52:10Z")

</div>

On 13 Jan 2012 13h36 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> Is there a chance to limit robots to 1 request per second?  
> The down below does not work: [emerg] “limit\_req” directive is not  
> allowed here

Yes. Try:

At the _http_ level:

limit\_req\_zone $http\_user\_agent zone=useragenttrack:1m rate=1r/s;

map $http\_user\_agent $is\_bot {  
default 0;  
~[a-z]bot[^a-z] 1;  
}

At the server level:

location / {  
error\_page 418 @bots;

```
if ($is_bot) {
  return 418;
}
...

```

}

location @bots {  
limit\_req zone=useragenttrack burst=100 nodelay;  
…  
}

Cf: [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpLimitReqModule)

— appa

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 17, 2012, 6:15pm UTC](https://www.ruby-forum.com/t/limit-robots/215713/3 "2012-01-17T18:15:50Z")

</div>

Your solution works great for rate limiting, but I need the requests to  
still be 200 return code when they are not limited. Any ideas?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 17, 2012, 9:07pm UTC](https://www.ruby-forum.com/t/limit-robots/215713/4 "2012-01-17T21:07:14Z")

</div>

Thanks, that worked great for me.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 17, 2012, 8:28pm UTC](https://www.ruby-forum.com/t/limit-robots/215713/5 "2012-01-17T20:28:40Z")

</div>

On 17 Jan 2012 17h15 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Your solution works great for rate limiting, but I need the requests  
> to still be 200 return code when they are not limited. Any ideas?

Yes. Try:

- error\_page 418 @bots;

- error\_page 418 =200 @bots;

I.e.:

location / {  
error\_page 418 =200 @bots;

```
if ($is_bot) {
  return 418;
}
...

```

}

location @bots {  
limit\_req zone=useragenttrack burst=100 nodelay;  
…  
}

— appa
