lua possibilities/limitations

agentzh agentzh at
Mon Jul 4 13:30:50 MSD 2011

On Tue, Feb 15, 2011 at 12:27 PM, agentzh <agentzh at> wrote:
> On Tue, Feb 15, 2011 at 7:46 AM, Richard Kearsley
> <Richard.Kearsley at> wrote:
>> Is there something special with $limit_rate stopping me to set it? Is it a
>> string/int issue?
> Yeah, $limit_rate is a special variable defined in the http core module:
> For now, ngx_lua's " = xxx" assignment does not support
> such special variables. This will change in a future release.

I've just committed a fix for nginx variable assignment in Lua to
ngx_lua's git master HEAD. Now all the nginx variables with a
set_handler is properly handled (like $args and $limit_rate).
Furthermore, only changeable nginx variables will be honored, so
writing to $arg_PARAM will result in a Lua exception (ready to be
caught by pcall/xpcall if LuaJIT 2.0 is used).

Here's the modified version of your sample config that should work for you:

    location /proxy {

    location /main {
        set $limit_rate 99;
        rewrite_by_lua '
            local res = ngx.location.capture("/proxy",{ args = { bla =
"moo" } })
            if res.status == ngx.HTTP_OK then
                for k,v in pairs(res.header) do
                     if k == "X-Limit-Rate" then
                          ngx.var.limit_rate = v



    add_header X-Limit $limit_rate;

My modifications mainly correct the following mistake in your previous sample:

* content_by_lua always runs in the content phase while ngx_rewrite's
"set" command always runs at the earlier rewrite phase. So your
content_by_lua will always run *after* your "set" commands even if you
put it before them.

As a bonus, I also add reading support for nginx regex group capturing
variables like $1, $2, $3, and etc, in Lua.

Happy Lua hacking!


More information about the nginx mailing list