Skip to content

Commit 31476b9

Browse files
committed
nginx: service needs to be restarted for modsecurity log rotation
Because modsecurity is not re-opening its logfile after rotation and continues to write into the same file descriptor, nginx needs to be restarted for the rotation to take effect. Better handling of that situation is stuck upstream for several years. We use the presence of `/var/log/modesc_*.log` as a heuristic for modsecurity being enabled, these files are now rotated with a restart of nginx. Note that, due to overlapping wildcard matches, this specific case got a higher logrotate match priority and needs an `ignoreduplicates`. Restarting nginx can have the impact of a very brief downtime and connections being dropped. logrotate.timer runs hourly, but as we've empirically observed that the majority of actual rotations takes place during the night, we refrain from restricting the logrotate runs to the night for now. PL-132296
1 parent 27d44bf commit 31476b9

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

nixos/services/nginx/default.nix

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,16 +469,29 @@ in
469469
inherit virtualHosts;
470470
};
471471

472-
services.logrotate.settings = {
473-
"/var/log/nginx/*.log" = {
472+
services.logrotate.settings = let
473+
commonRotate = {
474474
rotate = cfg.rotateLogs;
475475
create = "0644 ${nginxCfg.masterUser} nginx";
476476
su = "${nginxCfg.masterUser} nginx";
477+
};
478+
in {
479+
"/var/log/nginx/modsec_*.log" = {
480+
# need higher prio, because more-specific match.
481+
# Our platform header options use priority 900, we need to chose a
482+
# higher number here for using them.
483+
ignoreduplicates = true;
484+
priority = 901;
485+
postrotate = ''
486+
systemctl restart nginx
487+
'';
488+
} // commonRotate;
489+
"/var/log/nginx/*.log" = {
477490
postrotate = ''
478491
systemctl kill nginx -s USR1 --kill-who=main || systemctl reload nginx
479492
chown ${nginxCfg.masterUser}:nginx /var/log/nginx/*
480493
'';
481-
};
494+
} // commonRotate;
482495
};
483496

484497
# Z: Recursively change permissions if they already exist.

0 commit comments

Comments
 (0)