access_by_lua error

agentzh agentzh at
Mon Apr 18 12:40:33 MSD 2011

On Mon, Apr 18, 2011 at 6:27 AM, Richard Kearsley
<Richard.Kearsley at> wrote:
> Hi, not sure if I'm doing this the right way round but getting this error:
> [error] 11346#0: *1 content_by_lua aborted: runtime error: [string "access_by_lua"]:22: attempt to call ngx.exit after sending out the headers while sending to client
> On the lua code:
> elseif (res.status == ngx.HTTP_OK) then
>        ngx.print(res.body)
>        ngx.exit(res.status)
> I'm sure it's something simple im doing wrong?

The error message explains all. Please use ngx.exit(ngx.OK) or
ngx.exit(0) instead of ngx.exit(res.status) here.

Your response headers have already been sent once you call
ngx.print(res.body) and it's too late to do ngx.exit(200) on the next
line to attempt to change the http status code in the headers that
have already been sent to the client.

Unlike PHP, ngx_lua emits response headers and bodies in unbuffered
mode, i.e., streamingly, by default.


More information about the nginx mailing list