ngx_lua post data error/bug?

agentzh agentzh at gmail.com
Sat Oct 22 01:25:39 UTC 2011


On Sat, Oct 22, 2011 at 3:43 AM, Nginx User <nginx at nginxuser.net> wrote:
>
> Testing shows that the error is generated because of the
> 'dofile("/etc/nginx/regex_rules.lua")' line.
>

Lua's "dofile" builtin is implemented as a C function in both Lua 5.1
and LuaJIT 2.0. And when you call ngx.location.capture or ngx.exec or
ngx.exit or ngx.req.read_body or something like those in the
regex_rules.lua, it'll effectively initiate a coroutine yield and that
yield will run across C function boundary, which is disallowed.

I think it's more efficient to use Lua modules and the "require"
builtin because dofile will load the .lua file from disk at *every*
request and it's quite costy while "require" will only load the .lua
module file only once unless lua_code_cache is turned off. Here is an
example,

    -- regex_rules.lua
    module("regex_rules", package.seeall)
    function check()
        ngx.req.read_body()
        -- other processing goes here...
    end

    # nginx.conf
    lua_package_path "/path/to/regex_rules.lua's-parent-directory/?.lua;;"
    server {
        location /foo {
             access_by_lua '
                 local regex_rules = require "regex_rules"
                 regex_rules.check()
             ';
             # content handler config goes here...
        }
    }

BTW, compatibility with Lua 5.2 is not maintained by ngx_lua (yet).

Regards,
-agentzh



More information about the nginx mailing list