Conditional limit_req

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

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. [email protected]