Forum: NGINX Adding cachekey to log_format directive

Posted by Wouter van der Schagt (Guest)
on 2012-11-13 06:56
(Received via mailing list)
Good morning,

I'm trying to add the generated cache key of a proxied request to a
log_format directive. From what I can tell, this variable is not 
normally
available when logging requests so I've to modify the proxy module in
ngx_http_proxy_module.c.

So far I've added "cachekey" to the typedef struct: 
ngx_http_proxy_vars_t so
that ngx_http_proxy_set_var can set it.

I've added the declaration:

static ngx_int_t ngx_http_proxy_cachekey_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data); and in the corresponding
function (which is basically a copy of ngx_http_proxy_host_variable), I 
do:

    v->len =  ctx->vars.cachekey.len;
    v->data = ctx->vars.cachekey.data;

I've also added:

  { ngx_string("proxy_cachekey"), NULL, 
ngx_http_proxy_cachekey_variable, 0,
    NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 
0
},

to ngx_http_proxy_vars[] so that proxy_cachekey is available for use in 
the
log_format directive.

Finally in; ngx_http_proxy_set_var(ngx_url_t *u, ngx_http_proxy_vars_t 
*v)
I'm guessing I've to set the key somewhere here:

    v->cachekey.len = ??;
    v->cachekey.data = ??;

The question I have is, how do I get the generated cache key in the
v.cachekey.data field? If I populate these variables with arbitrary data 
I
can see its being logged correctly so am confident I'm in the right 
place.

Any suggestion would be appreciated,

Sincerely,

- WS

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,232747,232747#msg-232747
Posted by gmccullough (Guest)
on 2012-11-16 18:24
(Received via mailing list)
That depends on what you want...

If you want the raw cache key, it is stored as an array of strings
(r->cache->keys) that must be concatenated together to populate the 
variable
to log.

If you want the md5 hash of the raw cache key, it is available in two
places:

The binary md5 value in r->cache->key, which would require using
ngx_hex_dump to make it into a hexadecimal string to log.

A bit of a kludge, but the filename in the string r->cache->file.name
contains it, but you'd have to backwards search it for the last '/' to 
get
just the hash value.

Graham McCullough
Senior Software Engineer
Internap Network Services

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,232747,232921#msg-232921
Posted by Wouter van der Schagt (Guest)
on 2012-11-23 17:59
(Received via mailing list)
Hi Graham,

Thank you for your reply, the r->cache->file.name was particularly 
useful
for this use-case, thank you for the insight!

Sincerely,
- Wouter van der Schagt

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,232747,233189#msg-233189
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.