The Wayback Machine - https://web.archive.org/web/20200707155008/https://github.com/fcambus/nginx-resources/issues/12
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

Open
f2d opened this issue Jun 19, 2017 · 0 comments
Open

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

f2d opened this issue Jun 19, 2017 · 0 comments

Comments

@f2d
Copy link

@f2d 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
1 participant
You can’t perform that action at this time.