#StackBounty: #c #variables #nginx #module nginx module to set variable value for log module

Bounty: 50

I want to define an nginx variable, set its value during a request, and use the variable inside a log format.

I am currently implementing a custom module in C (not interested in LUA at the moment).

Desired flow:
1. configure new log format: log_format my_format '$my_var1'; (nginx.conf)
2. configure new access log using that format: access_log /tmp/out my_format (nginx.conf)
3. set value during request handler, function from the following type:
typedef ngx_int_t (*ngx_http_handler_pt)(ngx_http_request_t *r);

Current flow:
1. Set variable to an initial value: set $my_var1 'empty_value'; (nginx.conf)
2. Find variable by index with ngx_http_get_flushed_variable(r, index)
3. Change variable value by assigning to data field.

When I print my variable to the debug log, I see the variable is set correctly to the changed value. However, the log module outputs the initial variable value instead of the changed value to the custom access log.

Can someone point me to what am I missing here?
Should I use ngx_http_add_variable function or use script compile /complex ?

short snippet from the current flow described above:

ngx_http_variable_value_t *v;
v = ngx_http_get_flushed_variable(r, ale_srv_cf->output_variable_index);
// ... error handling if not found ....
v->data = new_value;
v->len = new_value_length;


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.