5

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?

2 Answers 2

5

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.

0

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

1
  • 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, 2011 at 0:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .