Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer

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

share|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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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