新手问题:header_filter_by_lua抛的异常为什么会影响响应结果

123 views
Skip to first unread message

蔡源

unread,
Jul 9, 2019, 6:36:04 AM7/9/19
to openresty
测试是在1.13.6.2上进行,系统是centos 7

简单的重现的配置/代码是这样的:
server {
  listen
8080;
  location
/ {
    content_by_lua
'ngx.say("test")';
    header_filter_by_lua
'error';
 
}
}




测试结果这样的,等到keepalive_timeout 的60s:
[root@host1 ~]# time curl -v localhost:8080
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
>
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server


real    1m0.010s
user    0m0.004s
sys     0m0.008s
[root@host1 ~]#


access日志中也显示响应体的大小是0,响应码200后面那个字段
127.0.0.1 [09/Jul/2019:17:27:52 +0800] "GET http://localhost HTTP/1.1" 78 200 0 0.000 "" "" "" "" "" "curl/7.29.0"


DeJiang Zhu

unread,
Jul 19, 2019, 12:27:02 PM7/19/19
to open...@googlegroups.com
因为 header_filter 是用来处理 http response  header 的,包括相应码
你这个是在 header_filter 里跑出异常了,所以什么也不会有了,header 都没有了,body 自然也不会有了

蔡源 <jils...@gmail.com> 于2019年7月9日周二 下午6:36写道:
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/a757515f-d0b7-492c-97a7-399c43905f43%40googlegroups.com
Message has been deleted

蔡源

unread,
Jul 26, 2019, 12:55:26 AM7/26/19
to openresty
感谢答疑,我原来的理解是header/body_filter阶段的动作只是修改/新增指定的内容,如果失败了不会影响主流程(响应内容或者响应头);看来需要更多的异常检查了...

在 2019年7月20日星期六 UTC+8上午12:27:02,doujiang写道:

DeJiang Zhu

unread,
Jul 26, 2019, 9:24:34 PM7/26/19
to open...@googlegroups.com
是的, Lua 的运行异常,总是应该避免的

蔡源 <jils...@gmail.com> 于2019年7月26日周五 下午12:55写道:
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
Message has been deleted
Message has been deleted

蔡源

unread,
Jul 26, 2019, 9:53:34 PM7/26/19
to openresty
事实上之前还有另外一层疑惑:之前一般在前面的rewrite/access_by_lua抛异常,返回的都是500(内部错误),在header_filter这里返回的是200;

之前用header_filter关键字在issue里搜了下,发现了这个:

貌似这一楼的测试例子有点类似:在不同阶段不同的exit(代码抛异常应该是一个ngx.ERROR吗?)带来的结果是不太一样的,这么理解对吗?


在 2019年7月27日星期六 UTC+8上午9:24:34,doujiang写道:

DeJiang Zhu

unread,
Jul 26, 2019, 10:04:39 PM7/26/19
to open...@googlegroups.com
嗯,是的,log_by_lua 里的 Lua 异常,影响是最小的

蔡源 <jils...@gmail.com> 于2019年7月27日周六 上午9:53写道:
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com

蔡源

unread,
Jul 26, 2019, 11:50:58 PM7/26/19
to openresty
再次感谢,清楚很多了

在 2019年7月27日星期六 UTC+8上午10:04:39,doujiang写道:
Reply all
Reply to author
Forward
0 new messages