<div dir="ltr">Ah, thanks, map{} is probably the best solution. We got it "working" by using rewrite_by_lua_file, which let us set new headers<span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">:</span><div>

<div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">    # Set an HTTP header that is read by conn_zone</div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">

    rewrite_by_lua_file user.lua;</div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">    limit_conn_zone $http_user_binary ...;</div>

<div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">Then in the lua file, add something like:</div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">

<br></div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">    ngx.set_header('User-Binary', ngx.md5_bin($remote_user));</div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">

<br></div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">This is almost certainly not an ideal thing to do, we'll look to rewrite it using map.</div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">

<br></div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">Thanks,</div><div style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">Kevin</div></div></div><div class="gmail_extra"><br clear="all">

<div><div><br></div><div>----</div>Kevin Burke | 415-723-4116 | <a href="http://www.twilio.com" target="_blank">www.twilio.com</a></div>
<br><br><div class="gmail_quote">On Wed, May 22, 2013 at 6:59 AM, Maxim Dounin <span dir="ltr"><<a href="mailto:mdounin@mdounin.ru" target="_blank">mdounin@mdounin.ru</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hello!<br>
<div><div class="h5"><br>
On Tue, May 21, 2013 at 12:31:05PM -0700, Kevin Burke wrote:<br>
<br>
> Hi,<br>
> We're trying to use the limit_conn_zone directive to throttle incoming<br>
> HTTP requests.<br>
><br>
> We'd like to throttle based on the http basic auth variable<br>
> ($remote_user), however, we must do processing on this value so the<br>
> zone does not overflow with illegitimate values. Ideally we'd want to<br>
> do something like<br>
><br>
> set $safe_remote_user "";<br>
> content_by_lua '<br>
> -- Some code to filter $remote_user values, simplified to one line here<br>
> ngx.var.safe_remote_user = $remote_user<br>
> '<br>
> limit_conn_zone $safe_remote_user zone:user 10m;<br>
><br>
> However this runs into a problem that we can only set variables inside<br>
> of the location context, but limit_conn_zone must be defined in the<br>
> http context. So, as we understand it we cannot use a variable defined<br>
> by lua in the limit_conn_zone directive. We were curious if anyone has<br>
> run into this problem, and if there are workarounds that could help us<br>
> solve this problem.<br>
<br>
</div></div>For variables processing independant on a particular request<br>
handling point there is the map{} and perl_set directives in<br>
nginx (see <a href="http://nginx.org/r/map" target="_blank">http://nginx.org/r/map</a>, <a href="http://nginx.org/r/perl_set" target="_blank">http://nginx.org/r/perl_set</a>).<br>
<br>
Not sure if there is something similar in lua module, but map<br>
should be enough for a particula task.<br>
<br>
With map you may do something like this:<br>
<br>
    map $remote_user $limit {<br>
        default      invalid;<br>
        ~^[a-z0-9]+$ $remote_user;<br>
    }<br>
<br>
This way only valid (according to a regex) user names are mapped<br>
to their own limits, while everything else maps to predefined<br>
value "invalid".<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Maxim Dounin<br>
<a href="http://nginx.org/en/donation.html" target="_blank">http://nginx.org/en/donation.html</a><br>
<br>
_______________________________________________<br>
nginx mailing list<br>
<a href="mailto:nginx@nginx.org">nginx@nginx.org</a><br>
<a href="http://mailman.nginx.org/mailman/listinfo/nginx" target="_blank">http://mailman.nginx.org/mailman/listinfo/nginx</a><br>
</font></span></blockquote></div><br></div>