Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log rotation directly within Nginx configuration file: map instead of if #12

Closed
f2d opened this issue Jun 19, 2017 · 1 comment
Closed

Comments

@f2d
Copy link

f2d commented Jun 19, 2017

With this code in my server blocks (included as a snippet):

if ($time_iso8601 ~ "^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})") {}

access_log /var/log/nginx/$server_log_prefix.access.$year-$month-$day.log;

For some requests (probably something automated, probably invalid, maybe even malformed) nginx ends up with just this filename: ".access.--.log"

Depending on whether it is desired to rotate such logs (or even drop these requests or whatever), following code in http block will always give the date, and without using an "if":

map $time_iso8601 $year {
	default				'date';
	'~^(?<yyyy>\d{4})-'		$yyyy;
}
map $time_iso8601 $month {
	default				'not';
	'~^\d{4}-(?<mm>\d{2})-'		$mm;
}
map $time_iso8601 $day {
	default				'found';
	'~^\d{4}-\d{2}-(?<dd>\d{2})'	$dd;
}

Just a tip which I found no better place to tell.
Tested on nginx/1.10.3.

Docs say, the lowest required version is the same 0.9.6, and that with at least 1.11.0 it could combine all parts into one $date variable in one map expression, like this:

map $time_iso8601 $map_date {
	default							'date-not-found';
	'~^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})'	$year-$month-$day;
}

But I didn't test this.

P.S. On second thought, this particular case does not need complex capturing and this works just fine in 0.9.6 and later:

map $time_iso8601 $map_date_now {
	default				'date-not-found';
	'~^(?<ymd>\d{4}-\d{2}-\d{2})'	$ymd;
}
@fcambus
Copy link
Owner

fcambus commented Mar 28, 2021

Sorry it took this long, I edited the article to point to this issue.

Thanks!

@fcambus fcambus closed this as completed Mar 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants