manu_dilip_shah
manu_dilip_shah

Reputation: 900

How can we set a variable inside a structure in nginx using lua

I have a requirement of setting a variable inside (the nginx internal C structure) ngx_http_request_s using lua.

This variable will have a unique value for every request.

Can this be done? Does the field need to be exposed as a variable or is there some other way to do this?

Upvotes: 1

Views: 539

Answers (1)

xiaochen
xiaochen

Reputation: 1305

The first way you have mentioned is to expose a variable to be modified in lua. And then the variable callback (ngx_http_variable_s::set_handler) can change nginx internal C structure.

The second way is to create lua C bindings. And lua-nginx-module exports some C API for binding C to lua. Here is an example from dyups module: binding C to ngx.lua and calling C from ngx.lua. Note that you can get ngx_http_request_t * pointer via ngx_http_lua_get_req(L) in C function.

The third way I prefer is to call C function directly in ngx.lua via FFI module. But it cannot work on your case, there is no good way to get current lua_State * pointer or ngx_http_request_t * pointer in FFI-called C function.

Upvotes: 1

Related Questions