# Adding cachekey to log\_format directive

**URL:** <https://www.ruby-forum.com/t/adding-cachekey-to-log-format-directive/224469>\
**Category:** NGINX\
**Created:** [November 13, 2012, 6:56am UTC](https://www.ruby-forum.com/t/adding-cachekey-to-log-format-directive/224469 "2012-11-13T06:56:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 13, 2012, 6:56am UTC](https://www.ruby-forum.com/t/adding-cachekey-to-log-format-directive/224469/1 "2012-11-13T06:56:21Z")

</div>

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:

> **[Adding cachekey to log\_format directive](https://forum.nginx.org/read.php?2,232747,232747#msg-232747)**
>
> 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....

---

<div class="post-metadata">

**Author:** ![Wouter\_van\_der\_Scha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/439d5e/32.png) [@Wouter\_van\_der\_Scha](https://www.ruby-forum.com/u/Wouter_van_der_Scha)\
**Post date:** [November 16, 2012, 6:24pm UTC](https://www.ruby-forum.com/t/adding-cachekey-to-log-format-directive/224469/2 "2012-11-16T18:24:25Z")

</div>

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:

> **[Re: Adding cachekey to log\_format directive](https://forum.nginx.org/read.php?2,232747,232921#msg-232921)**
>
> 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....

---

<div class="post-metadata">

**Author:** ![Wouter\_van\_der\_Scha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/439d5e/32.png) [@Wouter\_van\_der\_Scha](https://www.ruby-forum.com/u/Wouter_van_der_Scha)\
**Post date:** [November 23, 2012, 5:59pm UTC](https://www.ruby-forum.com/t/adding-cachekey-to-log-format-directive/224469/3 "2012-11-23T17:59:12Z")

</div>

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:

> **[Re: Adding cachekey to log\_format directive](https://forum.nginx.org/read.php?2,232747,233189#msg-233189)**
>
> 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....
