# services/nginx.nix — Native nginx reverse proxy with ACME wildcard cert { config, lib, ... }: let # Authelia forward-auth snippet injected into protected locations autheliaAuthConfig = '' auth_request /authelia; auth_request_set $user $upstream_http_remote_user; auth_request_set $email $upstream_http_remote_email; error_page 401 =302 https://auth.nordhammer.it/?rd=$scheme://$http_host$request_uri; ''; # Internal location that queries Authelia's verification endpoint autheliaLocation = { "/authelia" = { proxyPass = "http://127.0.0.1:9091/api/verify"; extraConfig = '' internal; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header X-Original-URL $scheme://$http_host$request_uri; proxy_set_header X-Forwarded-Method $request_method; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $http_host; proxy_set_header X-Forwarded-Uri $request_uri; proxy_set_header X-Forwarded-For $remote_addr; ''; }; }; ssl = { useACMEHost = "nordhammer.it"; forceSSL = true; }; # Security headers, shared across every location. add_header does NOT # merge with an ancestor context if a location defines its own add_header, # so any location below that sets add_header must re-include this string. securityHeaders = '' add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always; add_header Content-Security-Policy "default-src 'self' https: wss: data: blob: 'unsafe-inline' 'unsafe-eval'; frame-ancestors 'self';" always; ''; # Simple reverse proxy vhost proxy = port: ssl // { locations."/" = { proxyPass = "http://127.0.0.1:${toString port}"; proxyWebsockets = true; }; }; # Reverse proxy protected by Authelia forward auth protectedProxy = port: ssl // { locations = autheliaLocation // { "/" = { proxyPass = "http://127.0.0.1:${toString port}"; proxyWebsockets = true; extraConfig = autheliaAuthConfig; }; }; }; in { config = lib.mkIf (config.networking.hostName == "FredOS-Mediaserver") { # Wildcard TLS cert via Cloudflare DNS-01 challenge security.acme = { acceptTerms = true; defaults.email = "fredrik@nordhammer.it"; certs."nordhammer.it" = { domain = "*.nordhammer.it"; extraDomainNames = [ "nordhammer.it" ]; dnsProvider = "cloudflare"; # AdGuard's local rewrite swallows SOA lookups, breaking lego's zone detection dnsResolver = "1.1.1.1:53"; credentialFiles = { "CF_DNS_API_TOKEN_FILE" = "/var/secrets/cloudflare-token"; }; }; }; # Give Cloudflare authoritative NS more time to propagate TXT records systemd.services."acme-order-renew-nordhammer.it".environment.CLOUDFLARE_PROPAGATION_TIMEOUT = "600"; users.users.nginx.extraGroups = [ "acme" ]; services.nginx = { enable = true; recommendedProxySettings = true; recommendedTlsSettings = true; recommendedOptimisation = true; recommendedGzipSettings = true; # File-based access log for fail2ban + fix proxy_headers_hash warning appendHttpConfig = '' proxy_headers_hash_max_size 1024; access_log /var/log/nginx/access.log; '' + securityHeaders; virtualHosts = { # --- Unprotected (own auth, or by design) --- "auth.nordhammer.it" = proxy 9091; # Authelia portal itself "jellyfin.nordhammer.it" = proxy 8096; # streaming to external clients "seerr.nordhammer.it" = proxy 5055; # own auth via Jellyfin sign-in # wings daemon (see services/pelican.nix). Deliberately NOT behind # Authelia: the panel container calls this server-to-server and has no # session cookie to present. wings authenticates every request with its # own token/JWT, so an unauthenticated caller gets nothing. Uploads # through the file manager are whole server archives, hence no # body-size cap; the console websocket idles between keystrokes, hence # the long read timeout. "games.nordhammer.it" = lib.recursiveUpdate (proxy 8443) { locations."/".extraConfig = '' client_max_body_size 0; proxy_read_timeout 7d; ''; }; # --- Protected by Authelia --- "bazarr.nordhammer.it" = protectedProxy 6767; "sonarr.nordhammer.it" = protectedProxy 8989; "radarr.nordhammer.it" = protectedProxy 7878; "prowlarr.nordhammer.it" = protectedProxy 9696; # qBit's CSRF check rejects any request whose Referer origin differs # from the Host — after Authelia's redirect the Referer is # auth.nordhammer.it, which trips the check. Strip it so qBit skips. # Cookie stripped too so cached SID cookies don't fight localhost-bypass. "torrent.nordhammer.it" = lib.recursiveUpdate (protectedProxy 8080) { locations."/".extraConfig = autheliaAuthConfig + securityHeaders + '' proxy_set_header Referer ""; proxy_set_header Cookie ""; proxy_hide_header Set-Cookie; ''; }; "camera.nordhammer.it" = protectedProxy 1984; "homepage.nordhammer.it" = protectedProxy 8084; # "7dtd.nordhammer.it" = protectedProxy 8090; # 7DTD disabled "adguard.nordhammer.it" = protectedProxy 3000; # Router management UI (services/router-ui.nix). proxy_buffering off so # the apply log streams live instead of arriving in one lump at the end. "router.nordhammer.it" = lib.recursiveUpdate (protectedProxy 8086) { locations."/".extraConfig = autheliaAuthConfig + securityHeaders + '' proxy_buffering off; proxy_read_timeout 2h; ''; }; "profilarr.nordhammer.it" = protectedProxy 6868; "shelfarr.nordhammer.it" = protectedProxy 5056; "sabnzbd.nordhammer.it" = protectedProxy 8085; "code.nordhammer.it" = lib.recursiveUpdate (protectedProxy 4444) { locations."/".extraConfig = autheliaAuthConfig + securityHeaders + '' # Prevent browser from restoring a cached page on tab reopen — # forces a fresh request so Authelia can redirect before JS # tries to open a WebSocket with an expired session. add_header Cache-Control "no-store" always; ''; }; "notes.nordhammer.it" = protectedProxy 5230; "reddit.nordhammer.it" = protectedProxy 8087; # Pelican game panel. Can't turn off its own login (no such option # upstream), so Authelia is the outer gate and the Pelican account # behind it is a formality. /api/remote/ must skip forward-auth: wings # polls it with its daemon token and can't follow a 302 to the portal. "panel.nordhammer.it" = ssl // { locations = autheliaLocation // { "/" = { proxyPass = "http://127.0.0.1:8092"; proxyWebsockets = true; extraConfig = autheliaAuthConfig + '' client_max_body_size 100m; ''; }; "/api/remote/" = { proxyPass = "http://127.0.0.1:8092"; extraConfig = "client_max_body_size 100m;"; }; }; }; # --- Local-only: serves update history JSON to Homepage's customapi widget --- "homepage-updates.local" = { listen = [ { addr = "127.0.0.1"; port = 8083; } ]; locations."/".root = "/var/lib/homepage-updates"; extraConfig = '' default_type application/json; add_header Access-Control-Allow-Origin *; '' + securityHeaders; }; }; }; }; }