I'd like to include cookie data in an nginx access log like so:

(simplified example)

log_format foo '$remote_addr "$request" $cookie_bar';
access_log /var/log/nginx/access.log foo;

This works great on requests that already have a cookie "bar", but for the first request to my server nginx will report "-" as the value of "bar".

It seems like my problem is that nginx is looking at the request headers for the cookie value. Is there a way check for a Set-Cookie in the response and use that as a fallback?

link|improve this question
feedback

2 Answers

I found a hacky solution to my problem, but it doesn't solve the problem generally.

nginx lets you pass response header elements into the log file, like Set-Cookie:

log_format foo '$remote_addr "$request" '
               '$cookie_bar set_cookie=$sent_http_set_cookie';

Unfortunately it only outputs the first Set-Cookie command, so I need to make sure my app server always sets the bar cookie first. And with a little regexp I can make sure to grab the cookie set in $sent_http_set_cookie if $cookie_bar isn't set.

link|improve this answer
feedback

Have you considered using a redirect after setting the cookie and do the appropriate actions in the next request?

link|improve this answer
I have considered using redirects, but they aren't a good fit here. This is something I want to log for every URI. – etoleb Jan 18 '11 at 0:39
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.