I've noticed that when calling ngx.exit() within a header_filter block, the filter seems to run multiple times. Given the following config:
2016/01/11 23:11:24 [debug] 23589#0: *2 lua header filter for user lua code, uri "/header-filter-test"
2016/01/11 23:11:24 [info] 23589#0: *2 [lua] header_filter_by_lua:2: We are running in header_filter_by_lua!, client: 127.0.0.1, server: localhost, request: "GET /header-filter-test HTTP/1.1", host: "localhost"
2016/01/11 23:11:24 [debug] 23589#0: *2 lua exit with code 403
2016/01/11 23:11:24 [debug] 23589#0: *2 http special response: 403, "/header-filter-test?"
2016/01/11 23:11:24 [debug] 23589#0: *2 http set discard body
2016/01/11 23:11:24 [debug] 23589#0: *2 lua header filter for user lua code, uri "/header-filter-test"
2016/01/11 23:11:24 [info] 23589#0: *2 [lua] header_filter_by_lua:2: We are running in header_filter_by_lua!, client: 127.0.0.1, server: localhost, request: "GET /header-filter-test HTTP/1.1", host: "localhost"
2016/01/11 23:11:24 [debug] 23589#0: *2 lua exit with code 403
2016/01/11 23:11:24 [debug] 23589#0: *2 lua capture header filter, uri "/header-filter-test"
2016/01/11 23:11:24 [debug] 23589#0: *2 HTTP/1.1 403 Forbidden
Date: Tue, 12 Jan 2016 07:11:24 GMT
Content-Type: text/html
Content-Length: 174
Connection: keep-alive
I do see the following note in the documentation:
"When being used in the context of header_filter_by_lua, ngx.exit() is an asynchronous operation and will return immediately. This behavior may change in future and it is recommended that users always use return in combination as suggested above."
Given this, is this behavior expected? Is there any workaround to prevent header_filter_by_lua being called multiple times in this context? I tried checking ngx.req.is_internal within the header_filter block, but it does not evaluate to true in this context. Thanks!