How to set a custom log format for nginx, so that request got parsed and its parts are logged separate?
We serve a picture file to count mail opens. The url to the picture varies, but follows the rule:
http://www.server.com/counter/XXXXX/YYYYY/dailymail.gif
XXXXX – id of email campaign;
YYYYY – recipient id.
There’s a separate log for /counter location, which I’d like to give a format like
XXXXX YYYYY DATETIME
Location section looks like
location ~* ^counter/([0-9]+)/([^/]+)/dailymail\.gif$ {
access_log /var/log/mailopened.log
alias /var/www/site.com/1x1.gif?cid=$1&uid=$2&type=daily;
}
So I have the values in variables $1 and $2. How can I use them in the log format?
log_formatdirective is allowed athttplevel only, so you have to define it in terms of other variables, e.g.Later, in your location, just set these variables and log in
trackingformat:With recent versions of Nginx and PCRE library it’s possible to omit
setcalls and name the captures explicitly inlocation(thanks to @kolbyjack):