ngx lua响应状态码异常

463 views
Skip to first unread message

laowang

unread,
Dec 19, 2012, 10:21:14 PM12/19/12
to open...@googlegroups.com
错误日志发现如下问题:[error] 5772#0: *99870 attempt to set status 400 via ngx.exit after sending out the response status 200

local say = function(content, status)
    pcall(function() ngx.say(content) end)
    if status then
        ngx.exit(status)
    end
end

say(cjson.encode(failure), ngx.HTTP_BAD_REQUEST)

其中,之所以用pcall是为了防止客户端主动断开后ngx.say报错,其他地方没有输出HTTP_OK响应头,请问原因?

smallfish

unread,
Dec 19, 2012, 10:38:59 PM12/19/12
to open...@googlegroups.com
请参考官方例子中的说明哦:http://wiki.nginx.org/HttpLuaModule#ngx.exit

类似的应该这样输出:
ngx.status = 错误码
ngx.say("内容")
ngx.exit(ngx.HTTP_OK)

比如:
HTTP/1.1 400 Bad Request
Server: ngx_openresty/1.2.4.9
Date: Thu, 20 Dec 2012 03:37:15 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

hello



2012/12/20 laowang <ynb...@gmail.com>

kindy

unread,
Dec 19, 2012, 10:40:23 PM12/19/12
to openresty
请先设置 status (ngx.status = status) 然后再输出 (ngx.say, ngx.print)


2012/12/20 laowang <ynb...@gmail.com>

--



--
- - - - - - - - - - - -
林青(Kindy Lin)

laowang

unread,
Dec 20, 2012, 2:25:22 AM12/20/12
to open...@googlegroups.com
感谢二位的热心帮助,文档我读过,但是竟然对这个没有印象,非常惭愧。

在 2012年12月20日星期四UTC+8上午11时21分14秒,laowang写道:

Simon

unread,
Dec 21, 2012, 1:58:27 AM12/21/12
to open...@googlegroups.com
这个地方前两天刚发现一个bug,在使用http1.0的情况下,status不会被修改成期望的错误码。

https://github.com/chaoslawful/lua-nginx-module/issues/193

在 2012年12月20日星期四UTC+8上午11时38分59秒,smallfish写道:

agentzh

unread,
Dec 22, 2012, 3:43:38 AM12/22/12
to open...@googlegroups.com
Hello!

2012/12/20 Simon:


> 这个地方前两天刚发现一个bug,在使用http1.0的情况下,status不会被修改成期望的错误码。
>
> https://github.com/chaoslawful/lua-nginx-module/issues/193
>

这是 HTTP 1.0 buffering 模式下出现的行为不一致。已修正 :)

Best regards,
-agentzh

Reply all
Reply to author
Forward
0 new messages