# 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 # Streaming to external clients. The LG webOS app loads jellyfin-web in # an iframe, so X-Frame-Options / frame-ancestors from securityHeaders # blank the screen right after the server URL is entered. "jellyfin.nordhammer.it" = lib.recursiveUpdate (proxy 8096) { locations."/".extraConfig = '' add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Content-Type-Options "nosniff" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; ''; }; "seerr.nordhammer.it" = proxy 5055; # own auth via Jellyfin sign-in # Watch-party relay (services/owncast.nix). Shared basic-auth password # rather than Authelia: the guests have no accounts here, and Owncast # has no viewer auth of its own. The chat websocket is exempt because # browsers don't replay cached basic-auth credentials on a WS # handshake — /ws would 401 forever and chat would silently never # connect. Video stays gated; only the chat socket is reachable by # someone who already knows the hostname. # # /admin and /api/admin are exempt too, for a different reason: # Owncast enforces its own basic auth there (user "admin"). Two # basic-auth realms on one origin deadlock — the browser can only send # one Authorization header and nginx forwards it verbatim, so the # credential that satisfies nginx is rejected by Owncast and vice # versa. Let Owncast's own login be the only gate on those paths. "watch.nordhammer.it" = lib.recursiveUpdate (proxy 8088) { basicAuth = { blizzcon = "Blizzcon"; }; locations = lib.genAttrs [ "/ws" "/admin" "/api/admin" ] (_: { proxyPass = "http://127.0.0.1:8088"; proxyWebsockets = true; extraConfig = "auth_basic off;"; }); }; # 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. # Cookies must pass through: qBit 5.2 caches an uploaded .torrent's # metadata in the session, so parseMetadata and the following # torrents/add need the same QBT_SID or the add fails 400. "torrent.nordhammer.it" = lib.recursiveUpdate (protectedProxy 8080) { locations."/".extraConfig = autheliaAuthConfig + securityHeaders + '' proxy_set_header Referer ""; ''; }; "camera.nordhammer.it" = protectedProxy 1984; # "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; ''; }; "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;"; }; }; }; }; }; }; }