Apply req limits to fail2ban
All checks were successful
/ dev-shell (push) Successful in 30s
/ rust-packages (push) Successful in 35s
/ terraform-providers (push) Successful in 31s
/ check (push) Successful in 1m11s
/ systems (push) Successful in 13m48s

This commit is contained in:
Kaare Hoff Skovgaard 2025-07-20 23:13:56 +02:00
parent c03a508586
commit 61741b5680
Signed by: khs
GPG key ID: C7D890804F01E9F0

View file

@ -74,6 +74,11 @@ let
description = "Extra configuration to inject into the generated nginx config"; description = "Extra configuration to inject into the generated nginx config";
default = ''''; default = '''';
}; };
rateLimit.enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Enable rate limiting";
};
locations = lib.mkOption { locations = lib.mkOption {
type = lib.types.attrsOf ( type = lib.types.attrsOf (
lib.khscodes.mkSubmodule { lib.khscodes.mkSubmodule {
@ -149,6 +154,16 @@ in
maxretry = 2; maxretry = 2;
}; };
}; };
nginx-req-limit = {
settings = {
filter = "nginx-limit-req";
action = ''${config.services.fail2ban.banaction}[name=HTTP, port="http,https"]'';
logpath = "/var/log/nginx/access.log";
backend = "auto";
findtime = 600;
maxretry = 3;
};
};
}; };
khscodes.networking.aliases = lib.attrsets.attrNames cfg.virtualHosts; khscodes.networking.aliases = lib.attrsets.attrNames cfg.virtualHosts;
khscodes.security.acme.enable = true; khscodes.security.acme.enable = true;
@ -177,6 +192,7 @@ in
recommendedZstdSettings = lib.mkDefault true; recommendedZstdSettings = lib.mkDefault true;
recommendedProxySettings = lib.mkDefault true; recommendedProxySettings = lib.mkDefault true;
appendHttpConfig = '' appendHttpConfig = ''
limit_req_zone $binary_remote_addr zone=nobots:10m rate=50r/s;
map $scheme $hsts_header { map $scheme $hsts_header {
https "max-age=63072000; preload"; https "max-age=63072000; preload";
} }
@ -254,8 +270,12 @@ in
'' ''
else else
''''; '''';
reqLimit = lib.strings.optionalString value.rateLimit.enable ''
limit_req zone=nobots burst=5 nodelay;
'';
extraConfig = '' extraConfig = ''
${mtls} ${mtls}
${reqLimit}
${value.extraConfig} ${value.extraConfig}
''; '';
in in