# Conditional limit\_req

**URL:** <https://www.ruby-forum.com/t/conditional-limit-req/219469>\
**Category:** NGINX\
**Created:** [May 3, 2012, 9:35pm UTC](https://www.ruby-forum.com/t/conditional-limit-req/219469 "2012-05-03T21:35:36Z")\
**Posts on this page:** 2\
**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:** [May 3, 2012, 9:35pm UTC](https://www.ruby-forum.com/t/conditional-limit-req/219469/1 "2012-05-03T21:35:36Z")

</div>

Hello,

I’m using httpluamodule+redis to make a dynamic proxy to use in a mass  
vhost environment. I need to limit requests/s for specifics http\_host. I  
tried to do something like that:

* * *

limit\_req\_zone $http\_host zone=one:10m rate=1r/s;

upstream redisbackend {  
server 127.0.0.1:6379;  
}

server {

```
    listen xxxxx:80 default_server;

    location = /redis {
        internal;
        redis2_query get $arg_key;
        redis2_pass redisbackend;
    }

    location / {
        default_type 'text/html';
        set $backendserver '';
        set $limit '';

        access_by_lua '
            local key = ngx.var.http_host
            local res = ngx.location.capture(
                "/redis", { args = { key = key } }
            )

            m = ngx.re.match(res.body, 

```

“([a-z]+):([0-9.]+):([0-9]+)”)  
if m ~= nil then  
user = m[1]  
ngx.var.backendserver = m[2]  
ngx.var.limited = tonumber(m[3])  
end  
';

```
        if ($limited = 1) {
            limit_req zone=one burst=2;
        }

        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass http://$backendserver:888;
        break;

    }

```

}

* * *

When I reload nginx I’m getting the following error:

nginx: [emerg] “limit\_req” directive is not allowed here in  
/etc/nginx/conf.d/default.conf:64

Is there a way to limit specific websites using just 1 virtual host  
(server  
directive)?

Regards,

Guilherme

---

<div class="post-metadata">

**Author:** ![guilherme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guilherme](https://www.ruby-forum.com/u/guilherme)\
**Post date:** [May 5, 2012, 1:08pm UTC](https://www.ruby-forum.com/t/conditional-limit-req/219469/2 "2012-05-05T13:08:35Z")

</div>

On Thu, May 03, 2012 at 04:34:57PM -0300, Guilherme wrote:

Hi there,

all of this is untested by me, so consider it as a “maybe it’s worth  
trying” rather than a “here is how to do it”.

> I’m using httpluamodule+redis to make a dynamic proxy to use in a mass  
> vhost environment. I need to limit requests/s for specifics http\_host. I  
> tried to do something like that:

You want to limit incoming requests for specific http\_host values.

This means that somewhere, you will have to enumerate which http\_host  
values should be restricted (or which should be unrestricted –  
whichever  
list is easier).

I would probably use multiple server blocks – one as “default\_server”  
and the other with the server\_name list to be restricted.

Then include limit\_req in one server block, and not in the other.

But…

> Is there a way to limit specific websites using just 1 virtual host (server  
> directive)?

…if you don’t want to do that, then…

maybe use “map” to set a variable “$my\_limit\_variable” which is either  
$http\_host or empty; then use “limit\_req\_zone $my\_limit\_variable”, and  
“limit\_req” unconditionally – expecting that the empty variable will  
not be restricted?

Good luck with it,

## f

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