1

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.

2
  • 1
    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. Configure that in journald/rsyslog/logrotate or whatever is responsible for log archival on your host. Commented Oct 28, 2022 at 18:15
  • 2
    The standard practice is to let the log rotator rename the files. You can configure the log rotator to use dates in the filenames. Commented Oct 28, 2022 at 21:18

1 Answer 1

1

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 ... } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.