From b4bd52b91eebaea028f1e352a0f908aa12753a38 Mon Sep 17 00:00:00 2001 From: "Ricardo (XenGi) Band" Date: Wed, 2 Dec 2026 13:29:13 +0100 Subject: [PATCH] setup anubis in front of forgejo --- flake.nix | 2 +- hosts/git/forgejo.nix | 83 +++++++++++++++++++++++++------------------ services/anubis.nix | 31 ++++++++++++++++ 3 files changed, 81 insertions(+), 35 deletions(-) create mode 100644 services/anubis.nix diff --git a/flake.nix b/flake.nix index 1fd8b7c..ecda850 100644 --- a/flake.nix +++ b/flake.nix @@ -272,7 +272,7 @@ modules = [ agenix.nixosModules.default { - age.secrets = {}; + age.secrets = { }; } ./hosts/prusa-connect ]; diff --git a/hosts/git/forgejo.nix b/hosts/git/forgejo.nix index 84516aa..a415086 100644 --- a/hosts/git/forgejo.nix +++ b/hosts/git/forgejo.nix @@ -10,6 +10,7 @@ let in { imports = [ + ../../services/anubis.nix ../../services/nginx.nix ../../services/prometheus-nginx.nix ]; @@ -74,24 +75,24 @@ in }; }; + anubis.instances.${fqdn}.settings.TARGET = "unix:///run/nginx/${fqdn}.sock"; + nginx = { appendHttpConfig = '' limit_req_zone $binary_remote_addr zone=forgejo_archive:10m rate=10r/m; limit_conn_zone $binary_remote_addr zone=forgejo_archive_conn:10m; ''; - virtualHosts."${fqdn}" = { - quic = true; - kTLS = true; - forceSSL = true; - enableACME = true; - extraConfig = '' - merge_slashes off; # Required to handle URL-encoded slashes - proxy_set_header Forwarded "$proxy_forwarded_for_element;$proxy_forwarded_by_element;host=$host;proto=$scheme"; - error_log syslog:server=unix:/dev/log,tag=${builtins.replaceStrings [ "." ] [ "_" ] fqdn} warn; - access_log syslog:server=unix:/dev/log vhost_combined; - ''; - locations = { - "/" = { + virtualHosts = { + "forgejo.local" = { + listen = [ { addr = "unix:/run/nginx/${fqdn}.sock"; } ]; + serverName = fqdn; + extraConfig = '' + merge_slashes off; # Required to handle URL-encoded slashes + proxy_set_header Forwarded "$proxy_forwarded_for_element;$proxy_forwarded_by_element;host=$host;proto=$scheme"; + error_log syslog:server=unix:/dev/log,tag=${builtins.replaceStrings [ "." ] [ "_" ] fqdn} warn; + access_log syslog:server=unix:/dev/log vhost_combined; + ''; + locations."/" = { recommendedProxySettings = true; proxyWebsockets = true; proxyPass = "http://unix:${config.services.forgejo.settings.server.HTTP_ADDR}"; @@ -99,28 +100,42 @@ in client_max_body_size 512M; ''; }; - "/metrics" = { - recommendedProxySettings = true; - proxyPass = "http://unix:${config.services.forgejo.settings.server.HTTP_ADDR}"; - extraConfig = '' - allow 195.160.173.14; - allow 2001:678:760:cccb::14; - deny all; - ''; - }; - "~ ^/[^/]+/[^/]+/archive/" = { - recommendedProxySettings = true; - proxyPass = "http://unix:${config.services.forgejo.settings.server.HTTP_ADDR}"; - extraConfig = '' - limit_req zone=forgejo_archive burst=10 nodelay; - limit_conn forgejo_archive_conn 2; - limit_req_status 429; - ''; - }; - "= /robots.txt".extraConfig = '' - default_type text/plain; - return 200 "User-agent: *\nDisallow: /*/*/archive/\n"; + }; + "${fqdn}" = { + quic = true; + kTLS = true; + forceSSL = true; + enableACME = true; + extraConfig = '' + merge_slashes off; # Required to handle URL-encoded slashes + proxy_set_header Forwarded "$proxy_forwarded_for_element;$proxy_forwarded_by_element;host=$host;proto=$scheme"; + error_log syslog:server=unix:/dev/log,tag=${builtins.replaceStrings [ "." ] [ "_" ] fqdn} warn; + access_log syslog:server=unix:/dev/log vhost_combined; ''; + locations = { + "/" = { + recommendedProxySettings = true; + proxyWebsockets = true; + #proxyPass = "http://unix:${config.services.forgejo.settings.server.HTTP_ADDR}"; + proxyPass = "http://unix:${config.services.anubis.instances.${fqdn}.settings.BIND}"; + extraConfig = '' + client_max_body_size 512M; + ''; + }; + "/metrics" = { + recommendedProxySettings = true; + proxyPass = "http://unix:${config.services.forgejo.settings.server.HTTP_ADDR}"; + extraConfig = '' + allow 195.160.173.14; + allow 2001:678:760:cccb::14; + deny all; + ''; + }; + "= /robots.txt".extraConfig = '' + default_type text/plain; + return 200 "User-agent: *\nDisallow: /*/*/archive/\n"; + ''; + }; }; }; }; diff --git a/services/anubis.nix b/services/anubis.nix new file mode 100644 index 0000000..5d8209a --- /dev/null +++ b/services/anubis.nix @@ -0,0 +1,31 @@ +{ config, ... }: + +{ + users.users.nginx.extraGroups = [ + config.services.anubis.defaultOptions.group + ]; + + services.anubis.defaultOptions = { + group = config.services.nginx.group; + policy.extraBots = [ + { + name = "XenGis uptime-kuma"; + user_agent_regex = "Uptime-Kuma/.*"; + remote_addresses = [ + "159.195.204.204/32" + "2a0a:4cc0:c2:b028::/64" + ]; + action = "ALLOW"; + } + ]; + settings = { + BIND_NETWORK = "unix"; + DIFFICULTY = 4; + SERVE_ROBOTS_TXT = true; + WEBMASTER_EMAIL = "admin@berlin.ccc.de"; + OG_PASSTHROUGH = true; + OG_EXPIRY_TIME = "24h"; + REDIRECT_DOMAINS = "*.berlin.ccc.de,berlin.ccc.de"; + }; + }; +}