Can a map + regex be used to generate variables for YYYY-MM-DD-HH?

can a map + regex be used to generate variables for YYYY-MM-DD-HH? I
cant
use ‘if’ in the location i am trying this.

I know i can do:
if ($time_iso8601 ~ “^(\d{4})-(\d{2})-(\d{2})”) {
set $year $1;
set $month $2;
set $day $3;
}

or with perl regex:
if ($time_iso8601 ~ “^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})”)
{
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
set $minutes $5;
set $seconds $6;
}

Posted at Nginx Forum:

On Thu, Oct 23, 2014 at 02:00:44AM -0400, newnovice wrote:

Hi there,

can a map + regex be used to generate variables for YYYY-MM-DD-HH? I cant
use ‘if’ in the location i am trying this.

One map sets one variable.

Can you use your “if” outside all locations?

f

Francis D. [email protected]

Where I am going with this is - I want to write error & access - logs
out
with the ‘YYYY-MM-DD-HH’ suffix. So i need these variables.

The ‘if’ statement is not allowed outside the server{} - I am at a loss
due
to this. Please con you show me some examples of how to set these
variables
with a map regex?
I can have 4 seoerate maps to get it done - doesn’t matter.

Posted at Nginx Forum:

On Thu, Oct 23, 2014 at 07:17:20PM -0400, newnovice wrote:

Hi there,

Where I am going with this is - I want to write error & access - logs out
with the ‘YYYY-MM-DD-HH’ suffix. So i need these variables.

I’m not aware that error_log takes variables.

You may be happier moving the log files to time-named versions and then
signalling nginx to re-open the log files, every hour.

http://nginx.org/en/docs/control.html

The ‘if’ statement is not allowed outside the server{} - I am at a loss due
to this.

if() doesn’t work for you inside location{}, and it is not allowed
outside server{}. What happens if you put it inside server{} but outside
location{}?

Please con you show me some examples of how to set these variables
with a map regex?

Untested, but given that you already have

if ($time_iso8601 ~ “^(\d{4})-(\d{2})-(\d{2})”) {
set $year $1;
set $month $2;
set $day $3;
}

then I would expect that something like

map $time_iso8601 $year {
“~^(?P\d{4})-(\d{2})-(\d{2})” $one;
}

would do the right thing.

I can have 4 seoerate maps to get it done - doesn’t matter.

If you want to use map for this, then you would need multiple ones. But
I suspect that variable-in-log-file-name is not what you want.

Good luck with it,

f

Francis D. [email protected]

  1. variable in logname is what i want: access_log.YYYY-MM-DD-HH
    I have other log munging daemons looking for this date formatted file in
    real time (other options i have tried, hard linking files, used named
    pipes…)
  2. (currently i use this method) ‘if’ - inside server - outside location

works. Only when i get non ssl traffic to 443 port it writes out
access_log.---- instead of the usual: ‘access_log.2014-10-24-17’
2.1 is this a bug ?
3. also tried map inside server block - outside location & its not
allowed
there.

Posted at Nginx Forum:

cool so adding the maps & access_log config at http level solved both
problems, all logs now goto variable named file. there is no file with

in the name anymore.

thanks!

Posted at Nginx Forum:

On Fri, Oct 24, 2014 at 01:44:19PM -0400, newnovice wrote:

Hi there,

  1. variable in logname is what i want: access_log.YYYY-MM-DD-HH

http://nginx.org/r/access_log

That can work.

  1. (currently i use this method) ‘if’ - inside server - outside location -
    works.

http://nginx.org/r/if

That’s good. Note that “if” inside “location” should not be used unless
you “return” or “rewrite…last”, unless you know what you are doing.

But where you have it, inside server{}, is fine.

Only when i get non ssl traffic to 443 port it writes out
access_log.---- instead of the usual: ‘access_log.2014-10-24-17’
2.1 is this a bug ?

It looks to me like it might be.

  • I would naively have expected error_log output there, not access_log.
  • The number of “-” in the file name looks wrong.
  1. also tried map inside server block - outside location & its not allowed
    there.

Module ngx_http_map_module – correct, “map” only goes at http level.

You can refer to the variable that it creates, at server level.

f

Francis D. [email protected]