openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.31k stars 2.04k forks source link

HTTP/2 protocol error if headers are sent before a Lua error #2039

Open LoganDark opened 2 years ago

LoganDark commented 2 years ago

Relevant section of the OpenResty 1.19.9.1 config:

location @error {
    content_by_lua_file /var/www/lua/error.lua;
}

location / {
    error_page 500 @error;
    content_by_lua_file /var/www/lua/content.lua;
}

error.lua:

ngx.say(tostring(ngx.status))

content.lua:

ngx.send_headers()
error('hi')

Trying to curl https://localhost results in:

curl: (92) HTTP/2 stream 0 was not closed cleanly: INTERNAL_ERROR (err 2)

curl https://localhost --http1.1 (or --http1.0):

curl: (52) Empty reply from server

Commenting out the call to ngx.send_headers() results in the request completing successfully and printing 500.

Using echo instead of content_by_lua* in the @error named location doesn't solve the issue.

LoganDark commented 2 years ago

ugh I'm just generally having issues whenever multiple phases are involved. i.e. calling ngx.exit from access_by_lua_file also causes invalid responses. ngx.exec mostly works though

zhuizhuhaomeng commented 2 years ago

because the header has been sent out and there is no chance to change the HTTP status. So close the connection directly.