Richard Hart

Head of Something @ Somewhere
Kent, UK

My Music
My Photos

LinkedIn
Mastodon

Logging nginx cookies with dashes

Strangely nginx does not support variable names with dashes or other characters other than lower/uppercase letters, digits or underscores. This means that if you are trying to log cookie values which contain dashes, as in my case, you can’t do it.

Two workarounds are logging the all the cookies in one go, which can be overwhelming:


log_format main '"cookies=$http_cookie;"';

The other option is to map your cookie names to a variable that can be used by nginx:


map $http_cookie $my_cookie {
  default '';
  ~Cookie-With-Dash=(?[^\;]+) $mc;
}

log_format main '"Cookie-With-Dash=$my_cookie;"';