ngx_lua post data error/bug?

agentzh agentzh at
Sat Oct 22 01:25:39 UTC 2011

On Sat, Oct 22, 2011 at 3:43 AM, Nginx User <nginx at> 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

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

    # 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"
             # content handler config goes here...

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


More information about the nginx mailing list