Hello,
I am trying to implement variable support in nginx lua module which persists across HTTP requests.
[Disclaimer: Its an attempted hack to verify the implementation possibility]
The idea is exactly similar to the ngx.ctx.XYZ construct available in the module.
I have done the following:
------------------------------------
1. Used the similar construct as ngx.ctx.XYZ such that the context which stores the reference to the table returned by luaL_ref() is stored off ngx_connection_t (r->connection->lua_ref, where lua_ref is the ref returned by luaL_ref())
2. The cleanup handler is attached to connection pool thereby giving the persistence across HTTP requests
As of now I am getting:
---------------------------------
I call this construct ngx.conn_ctx.XYZ
So I correctly get the persistence of variables across HTTP requests as long as my script does not use ngx.conn_ctx.XYZ in the HTTP header filter.
More precisely:
location / {
rewrite_by_lua '
if not ngx.conn_ctx.foo then ngx.conn_ctx.foo = 17 end ngx.conn_ctx.foo = ngx.conn_ctx.foo + 1 if ngx.var.uri == "/foo/" then return ngx.redirect("https://www.foo.com/".. ngx.conn_ctx.foo) end'
proxy_pass ....
header_filter_by_lua ' if ngx.var.uri == "/bar/" then ngx.conn_ctx.foo = ngx.conn_ctx.foo + 3 ngx.header.FOO = ngx.conn_ctx.foo end'
}
For 3 requests per connection:
1. GET /foo/, I get as expected
https://www.foo.com/18,
https://www.foo.com/19
https://www.foo.com/20
2. GET /bar/, I dont get expected persistence value in the FOO header in HTTP response
FOO: 21
FOO: 21
FOO: 21
Seems like the ngx.conn_ctx.foo table is accessed correctly across the 3 coroutines created as part of rewrite directive, but the table seems to be cleared after the header_filter is executed.
The magic seems to be in ngx_http_lua_header_filter_by_lua_env().
Yichun, any comment from top of your head?
I will try to send the diff as well. I dont have access to my dev source tree now.
Best
Suraj