# Using http\_limit\_conn with a custom variable

**URL:** <https://www.ruby-forum.com/t/using-http-limit-conn-with-a-custom-variable/234969>\
**Category:** NGINX\
**Created:** [May 21, 2013, 9:32pm UTC](https://www.ruby-forum.com/t/using-http-limit-conn-with-a-custom-variable/234969 "2013-05-21T21:32:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [May 21, 2013, 9:32pm UTC](https://www.ruby-forum.com/t/using-http-limit-conn-with-a-custom-variable/234969/1 "2013-05-21T21:32:26Z")

</div>

Hi,  
We’re trying to use the limit\_conn\_zone directive to throttle incoming  
HTTP requests.

We’d like to throttle based on the http basic auth variable  
($remote\_user), however, we must do processing on this value so the  
zone does not overflow with illegitimate values. Ideally we’d want to  
do something like

set $safe\_remote\_user “”;  
content\_by\_lua ’  
– Some code to filter $remote\_user values, simplified to one line here  
ngx.var.safe\_remote\_user = $remote\_user  
’  
limit\_conn\_zone $safe\_remote\_user zone:user 10m;

However this runs into a problem that we can only set variables inside  
of the location context, but limit\_conn\_zone must be defined in the  
http context. So, as we understand it we cannot use a variable defined  
by lua in the limit\_conn\_zone directive. We were curious if anyone has  
run into this problem, and if there are workarounds that could help us  
solve this problem.

Thanks,  
Kevin

* * *

Kevin Burke | 415-723-4116 | [www.twilio.com](http://www.twilio.com)

---

<div class="post-metadata">

**Author:** ![Kevin\_Burke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@Kevin\_Burke](https://www.ruby-forum.com/u/Kevin_Burke)\
**Post date:** [May 22, 2013, 3:59pm UTC](https://www.ruby-forum.com/t/using-http-limit-conn-with-a-custom-variable/234969/2 "2013-05-22T15:59:47Z")

</div>

Hello!

On Tue, May 21, 2013 at 12:31:05PM -0700, Kevin Burke wrote:

> content\_by\_lua ’  
> solve this problem.  
> For variables processing independant on a particular request  
> handling point there is the map{} and perl\_set directives in  
> nginx (see [Module ngx\_http\_map\_module](http://nginx.org/r/map), [Module ngx\_http\_perl\_module](http://nginx.org/r/perl_set)).

Not sure if there is something similar in lua module, but map  
should be enough for a particula task.

With map you may do something like this:

```
map $remote_user $limit {
    default invalid;
    ~^[a-z0-9]+$ $remote_user;
}

```

This way only valid (according to a regex) user names are mapped  
to their own limits, while everything else maps to predefined  
value “invalid”.

–  
Maxim D.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![Kevin\_Burke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@Kevin\_Burke](https://www.ruby-forum.com/u/Kevin_Burke)\
**Post date:** [May 22, 2013, 4:36pm UTC](https://www.ruby-forum.com/t/using-http-limit-conn-with-a-custom-variable/234969/3 "2013-05-22T16:36:00Z")

</div>

Ah, thanks, map{} is probably the best solution. We got it “working” by  
using rewrite\_by\_lua\_file, which let us set new headers:

```
# Set an HTTP header that is read by conn_zone
rewrite_by_lua_file user.lua;

limit_conn_zone $http_user_binary ...;

```

Then in the lua file, add something like:

```
ngx.set_header('User-Binary', ngx.md5_bin($remote_user));

```

This is almost certainly not an ideal thing to do, we’ll look to rewrite  
it  
using map.

Thanks,  
Kevin

* * *

Kevin Burke | 415-723-4116 | [www.twilio.com](http://www.twilio.com)
