Skip to content

Commit

Permalink
nginx: fix continued writes to rotated modsecurity log files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
osnyx committed Apr 4, 2024
1 parent 27d44bf commit 760d9ee
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions nixos/services/nginx/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,27 @@ in
inherit virtualHosts;
};

services.logrotate.settings = {
"/var/log/nginx/*.log" = {
services.logrotate.settings = let
commonRotate = {
rotate = cfg.rotateLogs;
create = "0644 ${nginxCfg.masterUser} nginx";
su = "${nginxCfg.masterUser} nginx";
};
in {
"/var/log/nginx/modsec_*.log" = {
# need higher prio, because more-specific match.
# Our platform header options use priority 900, we need to chose a
# higher number here for using them.
ignoreduplicates = true;
priority = 901;
copytruncate = true;
} // commonRotate;
"/var/log/nginx/*.log" = {
postrotate = ''
systemctl kill nginx -s USR1 --kill-who=main || systemctl reload nginx
chown ${nginxCfg.masterUser}:nginx /var/log/nginx/*
'';
};
} // commonRotate;
};

# Z: Recursively change permissions if they already exist.
Expand Down

0 comments on commit 760d9ee

Please sign in to comment.