nixfiles/nixos/boxes/kelder/containers/spoder/nginx.nix

187 lines
5.8 KiB
Nix
Raw Normal View History

2023-05-23 21:32:38 +01:00
{ lib, pkgs, config, allAssignments, ... }:
let
2023-05-27 18:44:23 +01:00
inherit (builtins) mapAttrs;
inherit (lib) mkMerge mkIf mkDefault;
2023-12-19 23:40:54 +00:00
inherit (lib.my.c.nginx) baseHttpConfig proxyHeaders;
2023-11-02 13:41:50 +00:00
inherit (lib.my.c.kelder) domain;
2023-05-23 21:32:38 +01:00
in
{
config = {
my = {
secrets.files = {
"kelder/htpasswd" = {
owner = "nginx";
group = "nginx";
};
2023-05-27 18:44:23 +01:00
"dhparams.pem" = {
owner = "acme";
group = "acme";
mode = "440";
};
2023-05-23 21:32:38 +01:00
};
firewall = {
tcp.allowed = [ "http" "https" ];
};
};
services = {
nginx = {
package = pkgs.openresty;
2023-05-23 21:32:38 +01:00
enable = true;
enableReload = true;
logError = "stderr info";
2023-05-23 21:32:38 +01:00
recommendedTlsSettings = true;
clientMaxBodySize = "0";
serverTokens = true;
2023-05-27 18:44:23 +01:00
sslDhparam = config.age.secrets."dhparams.pem".path;
2023-05-23 21:32:38 +01:00
# Based on recommended*Settings, but probably better to be explicit about these
appendHttpConfig = ''
2023-12-19 23:40:54 +00:00
${baseHttpConfig}
2023-05-23 21:32:38 +01:00
# caching
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=CACHE:10m inactive=7d max_size=4g;
init_worker_by_lua_block {
local update_ip = function(premature)
if premature then
return
end
local hdl, err = io.popen("${pkgs.curl}/bin/curl -s https://v4.ident.me")
if not hdl then
ngx.log(ngx.ERR, "failed to run command: ", err)
return
end
local ip, err = hdl:read("*l")
hdl:close()
if not ip then
ngx.log(ngx.ERR, "failed to read ip: ", err)
return
end
pub_ip = ip
ngx.log(ngx.INFO, "ip is now: ", pub_ip)
end
local hdl, err = ngx.timer.every(5 * 60, update_ip)
if not hdl then
ngx.log(ngx.ERR, "failed to create timer: ", err)
end
update_ip()
}
2023-05-23 21:32:38 +01:00
'';
virtualHosts =
let
withAuth = c: mkMerge [
{
basicAuthFile = config.age.secrets."kelder/htpasswd".path;
}
c
];
acquisition = "http://${allAssignments.kelder-acquisition.internal.ipv4.address}";
# This is kinda borked because Virgin Media filters DNS responses with local IPs...
localRedirect = to: ''
rewrite_by_lua_block {
if ngx.var.remote_addr == pub_ip then
ngx.redirect(ngx.var.scheme .. "://${to}" .. ngx.var.request_uri, ngx.HTTP_MOVED_PERMANENTLY)
end
}
'';
2023-05-27 18:44:23 +01:00
hosts = {
"_" = {
default = true;
forceSSL = true;
onlySSL = false;
locations = {
"/".root = "${pkgs.nginx}/html";
};
};
2023-05-23 21:32:38 +01:00
2023-11-02 13:41:50 +00:00
"monitor.${domain}" = withAuth {
serverAliases = [ "monitor-local.${domain}" ];
# extraConfig = localRedirect "monitor-local.${domain}";
2023-05-28 13:58:42 +01:00
locations = {
"/" = {
proxyPass = "http://${allAssignments.kelder.ctrs.ipv4.address}:19999";
extraConfig = ''
proxy_pass_request_headers on;
2023-11-02 13:41:50 +00:00
${proxyHeaders}
2023-05-28 13:58:42 +01:00
proxy_set_header Connection "keep-alive";
proxy_store off;
gzip on;
gzip_proxied any;
gzip_types *;
'';
};
};
};
2023-11-02 13:41:50 +00:00
"kontent.${domain}" = {
serverAliases = [ "kontent-local.${domain}" ];
2023-05-27 18:44:23 +01:00
locations = {
"/".proxyPass = "${acquisition}:8096";
"= /".return = "302 $scheme://$host/web/";
"= /web/".proxyPass = "${acquisition}:8096/web/index.html";
"/socket" = {
proxyPass = "${acquisition}:8096/socket";
proxyWebsockets = true;
2023-11-02 13:41:50 +00:00
extraConfig = proxyHeaders;
2023-05-27 18:44:23 +01:00
};
};
};
2023-11-02 13:41:50 +00:00
"torrents.${domain}" = withAuth {
serverAliases = [ "torrents-local.${domain}" ];
# extraConfig = localRedirect "torrents-local.${domain}";
2023-05-27 18:44:23 +01:00
locations."/".proxyPass = "${acquisition}:9091";
};
2023-11-02 13:41:50 +00:00
"jackett.${domain}" = withAuth {
serverAliases = [ "jackett-local.${domain}" ];
# extraConfig = localRedirect "jackett-local.${domain}";
2023-05-27 18:44:23 +01:00
locations."/".proxyPass = "${acquisition}:9117";
};
2023-11-02 13:41:50 +00:00
"radarr.${domain}" = withAuth {
serverAliases = [ "radarr-local.${domain}" ];
# extraConfig = localRedirect "radarr-local.${domain}";
2023-05-27 18:44:23 +01:00
locations."/" = {
proxyPass = "${acquisition}:7878";
2023-05-23 21:32:38 +01:00
proxyWebsockets = true;
2023-11-02 13:41:50 +00:00
extraConfig = proxyHeaders;
2023-05-23 21:32:38 +01:00
};
};
2023-11-02 13:41:50 +00:00
"sonarr.${domain}" = withAuth {
serverAliases = [ "sonarr-local.${domain}" ];
# extraConfig = localRedirect "sonarr-local.${domain}";
2023-05-27 18:44:23 +01:00
locations."/" = {
proxyPass = "${acquisition}:8989";
2023-05-23 21:32:38 +01:00
proxyWebsockets = true;
2023-11-02 13:41:50 +00:00
extraConfig = proxyHeaders;
2023-05-23 21:32:38 +01:00
};
};
2023-05-28 00:14:04 +01:00
2023-11-02 13:41:50 +00:00
"cloud.${domain}" = {
serverAliases = [ "cloud-local.${domain}" ];
2023-05-28 00:14:04 +01:00
};
2023-05-23 21:32:38 +01:00
};
2023-05-27 18:44:23 +01:00
defaultsFor = mapAttrs (n: _: {
onlySSL = mkDefault true;
2023-11-02 13:41:50 +00:00
useACMEHost = mkDefault domain;
2023-05-27 18:44:23 +01:00
kTLS = mkDefault true;
http2 = mkDefault true;
});
in
mkMerge [
hosts
(defaultsFor hosts)
];
2023-05-23 21:32:38 +01:00
};
};
};
}