Skip to content

Commit 76c3668

Browse files
committed
nginx: fix continued writes to rotatrd modsecurity log files
Because modsecurity is not re-opening its logfile after rotation and continues to write into the same file descriptor, we need to use `copytruncate`. Better handling of that situation is stuck upstream for several years. owasp-modsecurity/ModSecurity-nginx#121 We use the presence of `/var/log/modesc_*.log` as a heuristic for modsecurity being enabled, these files are now rotated with copytruncate. All other nginx logs are still rotated by moving and reloading. Note that, due to overlapping wildcard matches, this specific case got a higher logrotate match priority and needs an `ignoreduplicates`. `copytruncate` is non-atomic and might loose some logs written between copying and the truncation being done. PL-132296
1 parent 27d44bf commit 76c3668

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

nixos/services/nginx/default.nix

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,16 +469,27 @@ 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+
copytruncate = true;
486+
} // commonRotate;
487+
"/var/log/nginx/*.log" = {
477488
postrotate = ''
478489
systemctl kill nginx -s USR1 --kill-who=main || systemctl reload nginx
479490
chown ${nginxCfg.masterUser}:nginx /var/log/nginx/*
480491
'';
481-
};
492+
} // commonRotate;
482493
};
483494

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

0 commit comments

Comments
 (0)