# Different limit\_req rules for different user agents

**URL:** <https://www.ruby-forum.com/t/different-limit-req-rules-for-different-user-agents/220713>\
**Category:** NGINX\
**Created:** [June 18, 2012, 1:14pm UTC](https://www.ruby-forum.com/t/different-limit-req-rules-for-different-user-agents/220713 "2012-06-18T13:14:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 18, 2012, 1:14pm UTC](https://www.ruby-forum.com/t/different-limit-req-rules-for-different-user-agents/220713/1 "2012-06-18T13:14:40Z")

</div>

Hello.

I need to check client’s user agent and apply different limit\_req rules  
for different UAs.  
I’ve been trying to do it this way:

http {  
limit\_req\_zone $binary\_remote\_addr zone=bots:10m rate=1r/m;  
server {  
…  
location / {  
if ($http\_user\_agent ~\* (google|bing|yandex|msnbot) )  
{  
limit\_req zone=bots burst=5 nodelay;  
}  
include balancer.conf;  
}}}

But apparently I’m missing something as nginx -t says ‘[emerg]:  
“limit\_req” directive is not allowed here’. Most likely due to poor  
understanding of configfile syntax. Please advice me on how to achieve  
my goal.

Thanks in advance.

Posted at Nginx Forum:

> **[Different limit\_req rules for different user agents](https://forum.nginx.org/read.php?2,227641,227641#msg-227641)**
>
> Hello. I need to check client's user agent and apply different limit\_req rules for different UAs. I've been trying to do it this way: http { limit\_req\_zone $binary\_remote\_addr zone=bots:10m rate=1r/m; server { ... location / { i

---

<div class="post-metadata">

**Author:** ![LexxTheFox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@LexxTheFox](https://www.ruby-forum.com/u/LexxTheFox)\
**Post date:** [June 18, 2012, 3:07pm UTC](https://www.ruby-forum.com/t/different-limit-req-rules-for-different-user-agents/220713/2 "2012-06-18T15:07:27Z")

</div>

On Monday 18 June 2012 15:14:12 LexxTheFox wrote:

> location / {  
> if ($http\_user\_agent ~\* (google|bing|yandex|msnbot) )  
> {  
> limit\_req zone=bots burst=5 nodelay;  
> }  
> include balancer.conf;  
> }}}

http {

```
  map $http_user_agent $limit_bots {
      default '';
      ~*(google|bing|yandex|msnbot) $binary_remote_addr;
  }

  limit_req_zone $limit_bots zone=bots:10m rate=1r/m;

  server {
      location / {
          limit_req zone=bots burst=5 nodelay;
      }
  }

```

}

> But apparently I’m missing something as nginx -t says ‘[emerg]:  
> “limit\_req” directive is not allowed here’. Most likely due to poor  
> understanding of configfile syntax.

> **[Module ngx\_http\_limit\_req\_module](http://nginx.org/en/docs/http/ngx_http_limit_req_module.html#limit_req)**
>
> The ngx\_http\_limit\_req\_module module (0.7.21) is used to limit the request processing rate per a defined key, in particular, the processing rate of requests coming from a single IP address. The limitation is done using the “leaky bucket” method.

context: http, server, location

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![LexxTheFox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9a28c/32.png) [@LexxTheFox](https://www.ruby-forum.com/u/LexxTheFox)\
**Post date:** [June 21, 2012, 1:46pm UTC](https://www.ruby-forum.com/t/different-limit-req-rules-for-different-user-agents/220713/3 "2012-06-21T13:46:47Z")

</div>

## Valentin V. Bartenev Wrote:

> ```
> server {
> location / {
> limit_req zone=bots burst=5 nodelay;
> }
> }
> 
> ```
> 
> }
> 
> wbr, Valentin V. Bartenev

Hello.

I still don’t really get the ‘map’ magic. This example does not work for  
me - bots still connect as often as they please. But your example gave  
me an idea. After some reading and fiddling I managed to solve the  
riddle this way:

http {  
limit\_req\_zone $limit\_bots zone=bots:10m rate=1r/m;  
server {  
location / {  
set $bot ‘’;  
if ($http\_user\_agent ~\* (google|bing|yandex|msnbot) ) {set  
$limit\_bots $binary\_remote\_addr;}  
limit\_req zone=bots burst=5 nodelay;  
}  
}  
}

I will continue reading docs and hope to figure everything out  
eventually. By now it works for me.

Thank you for your help!

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,227641,227759#msg-227759](http://forum.nginx.org/read.php?2,227641,227759#msg-227759)
