1

I have a following nginx config:

http {
(...)

limit_conn_zone $binary_remote_addr zone=limitapinoauth:16m;
limit_conn_zone $remote_user zone=limitapi:32m;

map $remote_user $zone {
    default   'limitapi';
    ''        'limitapinoauth';
}

map $zone $num {
    'limitapi'   100;
    default    200;
}

server {
    listen 80 default;

    root /var/www/example.com/public_html;

    limit_conn $zone $num;
}
(...)
}

Expected behavior is that mapped variables are evaluated to use in last line, but they aren't. Any idea why?

I get the following error:

nginx: [emerg] invalid number of connections "$num"

When I change $num to integer, i.e. 200, the error is:

nginx: [emerg] zero size shared memory zone "$zone"

If I change last line to:

limit_conn limitapi 100;

the config is correct.

1 Answer 1

2

It is not stated clearly in the ngx_http_map_module map Directive documentation, which only says:

Creates a new variable whose value depends on values of one or more of the source variables specified in the first parameter.

I suppose that as ngx_http_map_module.c works in The Module Context ngx_http_map_create_conf (while creating main configuration) and all indexed variables are added during pre-configuration, only built-in variables can be used as source variables for the map directive. (Nginx guru, please confirm.)

Also, I couldn't find any example that uses a variable defined by map directive as a source variable on another map directive, which suggests the same. Therefore, I'd advice to create both variables, $zone and $num, directly based on the $remote_user variable.

map $remote_user $zone {
    default   'limitapi';
    ''        'limitapinoauth';
}

map $remote_user $num {
    default   100;
    ''        200;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .