Score:0

Is it possible to use mapped variables in the nginx error log filename?

us flag

I've configured a site like this:

map $time_iso8601 $yyyy {
    default '0000';
    "~^(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})" $y;
}

map $time_iso8601 $mm {
    default '00';
    "~^(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})" $m;
}

map $time_iso8601 $dd {
    default '00';
    "~^(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})" $d;
}

server {
    listen 80;
    server_name myapp.initech.com;
    error_log /sitelogs/initech/myapp/error.$yyyy-$mm-$dd.log;
    access_log /sitelogs/initech/myapp/access.$yyyy-$mm-$dd.log;

    location / {
        root /site/initech/data/myapp;
        autoindex on;
    }
}

Having reloaded the server and made some test requests, my log directory looks like this:

$ ls -1 /sitelogs/initech/myapp
access.2022-10-28.log
error.$yyyy-$mm-$dd.log

It looks to me like i can use that map directive to set up a variable to be used in the name of the access log, but not the error log. Is that the case?

I could rationalise that as the map directives only running in the context of a particular HTTP request, whereas in some sense the error log has to be set up across all HTTP requests.

If this is the case, is there any way to get a dated error log filename? Our firm has existing log archiving processes which don't play well with filename collisions, so it would be really, really helpful to have dated error log files.

anx avatar
fr flag
anx
Don't! In theory you can instruct nginx to stop caching your maps (`volatile`) but that will make things.. unpredictable at best. For things that change merely *once* per day you absolutely want nginx to not worry about it and instead write to a really simple, fast interface; then have another program [do one thing well](https://en.wikipedia.org/wiki/Unix_philosophy). Configure that in journald/rsyslog/logrotate or whatever is responsible for log archival on your host.
us flag
The standard practice is to let the log rotator rename the files. You can configure the log rotator to use dates in the filenames.
Score:1
fr flag
anx

variable to be used in the name of the access log, but not the error log

Correct, Nginx documentation would note explicitly when a directive accepts variables (and what limitations and performance impacts to expect when doing so). Many directives do not.


Your distribution may ship one tailored to your system, if not look at the /etc/logrotate.d/nginx file provided in Debian for a template. Logrotate can also use the day you want (the day prior to rotation time) in the filename when setup like this:

/sitelogs/*/*/*.log {
    nocompress
    dateext
    dateformat .%Y-%m-%d.%s
    daily
    dateyesterday
    postrotate
      ...
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.