2026-04-07 15:47:56 +01:00
|
|
|
# services/nginx.nix — Native nginx reverse proxy with ACME wildcard cert
|
|
|
|
|
{ config, lib, ... }:
|
|
|
|
|
let
|
|
|
|
|
# Authelia forward-auth snippet injected into protected locations
|
|
|
|
|
autheliaAuthConfig = ''
|
2026-04-07 20:35:59 +01:00
|
|
|
auth_request /authelia;
|
2026-04-07 15:47:56 +01:00
|
|
|
auth_request_set $user $upstream_http_remote_user;
|
2026-04-07 20:35:59 +01:00
|
|
|
auth_request_set $email $upstream_http_remote_email;
|
|
|
|
|
error_page 401 =302 https://auth.nordhammer.it/?rd=$scheme://$http_host$request_uri;
|
2026-04-07 15:47:56 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
# Internal location that queries Authelia's verification endpoint
|
|
|
|
|
autheliaLocation = {
|
2026-04-07 20:35:59 +01:00
|
|
|
"/authelia" = {
|
|
|
|
|
proxyPass = "http://127.0.0.1:9091/api/verify";
|
2026-04-07 15:47:56 +01:00
|
|
|
extraConfig = ''
|
|
|
|
|
internal;
|
2026-04-07 20:35:59 +01:00
|
|
|
proxy_pass_request_body off;
|
|
|
|
|
proxy_set_header Content-Length "";
|
2026-04-07 15:47:56 +01:00
|
|
|
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;
|
2026-04-07 20:35:59 +01:00
|
|
|
proxy_set_header X-Forwarded-Uri $request_uri;
|
2026-04-07 15:47:56 +01:00
|
|
|
proxy_set_header X-Forwarded-For $remote_addr;
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ssl = {
|
|
|
|
|
useACMEHost = "nordhammer.it";
|
|
|
|
|
forceSSL = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# 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
|
2026-01-20 21:13:33 +00:00
|
|
|
{
|
2026-04-07 15:47:56 +01:00
|
|
|
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";
|
|
|
|
|
credentialFiles = {
|
|
|
|
|
"CF_DNS_API_TOKEN_FILE" = "/var/secrets/cloudflare-token";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-14 22:48:58 +01:00
|
|
|
# Give Cloudflare authoritative NS more time to propagate TXT records
|
|
|
|
|
systemd.services."acme-order-renew-nordhammer.it".environment.CLOUDFLARE_PROPAGATION_TIMEOUT = "600";
|
|
|
|
|
|
2026-04-07 15:47:56 +01:00
|
|
|
users.users.nginx.extraGroups = [ "acme" ];
|
|
|
|
|
|
|
|
|
|
services.nginx = {
|
|
|
|
|
enable = true;
|
|
|
|
|
recommendedProxySettings = true;
|
|
|
|
|
recommendedTlsSettings = true;
|
|
|
|
|
recommendedOptimisation = true;
|
|
|
|
|
recommendedGzipSettings = true;
|
|
|
|
|
|
2026-04-07 17:27:23 +01:00
|
|
|
# File-based access log for fail2ban + fix proxy_headers_hash warning
|
2026-04-07 15:47:56 +01:00
|
|
|
appendHttpConfig = ''
|
2026-04-07 17:27:23 +01:00
|
|
|
proxy_headers_hash_max_size 1024;
|
2026-04-07 15:47:56 +01:00
|
|
|
access_log /var/log/nginx/access.log;
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
virtualHosts = {
|
Put Servarr + qBit + games + search behind Authelia
Only Jellyfin and the Authelia portal itself stay unprotected externally
(Jellyfin because it's streamed to remote clients; Authelia because it
is the login gate). Everything else (sonarr, radarr, bazarr, prowlarr,
torrent/qBittorrent, games, search) now goes through Authelia forward auth.
Internal integrations (Homepage widgets, Prowlarr → Sonarr/Radarr,
Bazarr → Sonarr/Radarr, transcode-hevc qBit queries) use 127.0.0.1:PORT
directly, so they are unaffected.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 11:21:28 +01:00
|
|
|
# --- Unprotected (own auth, or by design) ---
|
|
|
|
|
"auth.nordhammer.it" = proxy 9091; # Authelia portal itself
|
|
|
|
|
"jellyfin.nordhammer.it" = proxy 8096; # streaming to external clients
|
2026-04-07 15:47:56 +01:00
|
|
|
|
|
|
|
|
# --- Protected by Authelia ---
|
Put Servarr + qBit + games + search behind Authelia
Only Jellyfin and the Authelia portal itself stay unprotected externally
(Jellyfin because it's streamed to remote clients; Authelia because it
is the login gate). Everything else (sonarr, radarr, bazarr, prowlarr,
torrent/qBittorrent, games, search) now goes through Authelia forward auth.
Internal integrations (Homepage widgets, Prowlarr → Sonarr/Radarr,
Bazarr → Sonarr/Radarr, transcode-hevc qBit queries) use 127.0.0.1:PORT
directly, so they are unaffected.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-24 11:21:28 +01:00
|
|
|
"bazarr.nordhammer.it" = protectedProxy 6767;
|
|
|
|
|
"sonarr.nordhammer.it" = protectedProxy 8989;
|
|
|
|
|
"radarr.nordhammer.it" = protectedProxy 7878;
|
|
|
|
|
"prowlarr.nordhammer.it" = protectedProxy 9696;
|
2026-04-24 20:04:04 +01:00
|
|
|
# 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.
|
2026-04-24 14:57:55 +01:00
|
|
|
"torrent.nordhammer.it" = lib.recursiveUpdate (protectedProxy 8080) {
|
|
|
|
|
locations."/".extraConfig = autheliaAuthConfig + ''
|
2026-04-24 20:04:04 +01:00
|
|
|
proxy_set_header Referer "";
|
2026-04-24 14:57:55 +01:00
|
|
|
proxy_set_header Cookie "";
|
|
|
|
|
proxy_hide_header Set-Cookie;
|
|
|
|
|
'';
|
|
|
|
|
};
|
2026-04-07 15:47:56 +01:00
|
|
|
"camera.nordhammer.it" = protectedProxy 1984;
|
|
|
|
|
"homepage.nordhammer.it" = protectedProxy 8082;
|
2026-04-18 21:43:11 +01:00
|
|
|
"7dtd.nordhammer.it" = protectedProxy 8090;
|
2026-04-22 14:16:52 +01:00
|
|
|
"adguard.nordhammer.it" = protectedProxy 3000;
|
2026-04-30 20:00:33 +01:00
|
|
|
"profilarr.nordhammer.it" = protectedProxy 6868;
|
2026-04-16 20:58:19 +01:00
|
|
|
|
|
|
|
|
# --- 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 *;
|
|
|
|
|
'';
|
|
|
|
|
};
|
2026-04-07 15:47:56 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.firewall.allowedTCPPorts = [ 80 443 ];
|
|
|
|
|
};
|
2026-01-20 21:17:17 +00:00
|
|
|
}
|