Request-scoped variables

Brian Long brian at
Sat Feb 20 09:50:59 MSK 2010

I might have just figured out the answer to my own question. It looks like
setting v->no_cacheable = 0 is what was required.

Here's my get_handler:

ngx_int_t ngx_x_rid_header_get_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data) {
  u_char *p;

  p = ngx_pnalloc(r->pool, 36);
  if (p == NULL) {
      return NGX_ERROR;

  uuid_t uuid;
  uuid_unparse_lower(uuid, (char*)p);

  v->len = 36;
  v->valid = 1;
  v->no_cacheable = 0;
  v->not_found = 0;
  v->data = p;

  return NGX_OK;

and here's where i add the variable:

static ngx_str_t  ngx_x_rid_header_variable_name = ngx_string("request_id");
static ngx_int_t ngx_x_rid_header_add_variables(ngx_conf_t *cf)
  ngx_http_variable_t* var = ngx_http_add_variable(cf,
&ngx_x_rid_header_variable_name, NGX_HTTP_VAR_NOHASH);
  if (var == NULL) {
      return NGX_ERROR;
  var->get_handler = ngx_x_rid_header_get_variable;
  return NGX_OK;

Does this look like the correct setup? It does appear to be working now.


On Fri, Feb 19, 2010 at 11:32 PM, Brian Long <brian at> wrote:

> Hi everyone,
> I'm attempting to implement a module which generates a "request id (rid)"
> variable. It's basically a uuid. The special requirement I have is that this
> variable (call it $request_id) needs to be different per-request, but also
> contain the same value if referenced multiple times in that request. For
> example, I will use the variable in both a log_format directive as well as
> a proxy_set_header directive. (Obviously the id is to log a request id that
> can be used to correlated frontend requests to activity/exceptions on the
> backends). So obviously I want to $request_id to be the same in log_format
> and proxy_set_header, but at the same time different for every request.
> I have the basic functionality of the variable working, but what I can't
> figure out how to do is to keep the value the same during a single request.
> Right now every time $request_is used, by get_handler is invoked and i
> generated new uuid.
> Is there some way of indicating the variable should be cached the request
> scope, or is there somewhere I can store the variable in ngx_http_request_t*
> for later retrieval?
> I'll be releasing this project on github once I can complete this piece of
> the functionality.
> Thanks,
> Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx-devel mailing list