# Access\_by\_lua error

**URL:** <https://www.ruby-forum.com/t/access-by-lua-error/205878>\
**Category:** NGINX\
**Created:** [April 18, 2011, 12:28am UTC](https://www.ruby-forum.com/t/access-by-lua-error/205878 "2011-04-18T00:28:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 18, 2011, 12:28am UTC](https://www.ruby-forum.com/t/access-by-lua-error/205878/1 "2011-04-18T00:28:02Z")

</div>

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?

Thanks

---

<div class="post-metadata">

**Author:** ![rkoskela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rkoskela](https://www.ruby-forum.com/u/rkoskela)\
**Post date:** [April 18, 2011, 10:41am UTC](https://www.ruby-forum.com/t/access-by-lua-error/205878/2 "2011-04-18T10:41:08Z")

</div>

On Mon, Apr 18, 2011 at 6:27 AM, Richard K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

Cheers,  
-agentzh
