feat(nginx): reuseport

This commit is contained in:
arcnmx 2024-04-30 14:52:23 -07:00
parent 6df95aa9ce
commit b17af83d2a
10 changed files with 41 additions and 23 deletions

View file

@ -118,7 +118,7 @@ let
local.denyGlobal = mkIf listenProxied (mkDefault true); local.denyGlobal = mkIf listenProxied (mkDefault true);
listen' = mkIf listenProxied { listen' = mkIf listenProxied {
proxied = { proxied = {
addr = "[::]"; addr = mkAlmostOptionDefault nginx.proxied.listenAddr;
port = mkAlmostOptionDefault nginx.proxied.listenPort; port = mkAlmostOptionDefault nginx.proxied.listenPort;
}; };
}; };
@ -130,10 +130,12 @@ let
in { in {
config, config,
system, system,
gensokyo-zone,
lib, lib,
... ...
}: let }: let
inherit (lib.options) mkOption; inherit (gensokyo-zone.lib) mkAlmostOptionDefault;
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.modules) mkIf mkOptionDefault; inherit (lib.modules) mkIf mkOptionDefault;
inherit (lib.attrsets) attrValues; inherit (lib.attrsets) attrValues;
inherit (lib.lists) any; inherit (lib.lists) any;
@ -142,8 +144,10 @@ in {
in { in {
options.services.nginx = with lib.types; { options.services.nginx = with lib.types; {
proxied = { proxied = {
enabled = mkOption { enable = mkEnableOption "proxy";
type = bool; listenAddr = mkOption {
type = str;
default = "[::]";
}; };
listenPort = mkOption { listenPort = mkOption {
type = port; type = port;
@ -156,13 +160,11 @@ in {
}; };
config = { config = {
services.nginx = let services.nginx = let
warnEnable = lib.warnIf (cfg.enable != hasProxiedHosts) "services.nginx.proxied.enable expected to be set";
hasProxiedHosts = any (virtualHost: virtualHost.enable && virtualHost.proxied.enabled) (attrValues nginx.virtualHosts); hasProxiedHosts = any (virtualHost: virtualHost.enable && virtualHost.proxied.enabled) (attrValues nginx.virtualHosts);
in { in {
proxied = {
enabled = mkOptionDefault hasProxiedHosts;
};
upstreams' = { upstreams' = {
nginx'proxied = mkIf cfg.enabled { nginx'proxied = mkIf (warnEnable cfg.enable) {
servers.local = { servers.local = {
accessService = { accessService = {
system = system.name; system = system.name;
@ -172,10 +174,23 @@ in {
}; };
}; };
}; };
# TODO: virtualHosts.fallback'proxied.reuseport = true; virtualHosts = {
fallback'proxied = mkIf cfg.enable {
serverName = null;
reuseport = mkAlmostOptionDefault true;
default = mkAlmostOptionDefault true;
listen'.proxied = {
addr = mkAlmostOptionDefault cfg.listenAddr;
port = mkAlmostOptionDefault cfg.listenPort;
};
locations."/".extraConfig = mkAlmostOptionDefault ''
return 502;
'';
};
};
}; };
networking.firewall.interfaces.lan = mkIf nginx.enable { networking.firewall.interfaces.lan = mkIf nginx.enable {
allowedTCPPorts = mkIf cfg.enabled [ cfg.listenPort ]; allowedTCPPorts = mkIf cfg.enable [ cfg.listenPort ];
}; };
}; };
} }

View file

@ -12,7 +12,7 @@ in {
message = "ports mismatch"; message = "ports mismatch";
}; };
assertProxied = nixosConfig: cfg: { assertProxied = nixosConfig: cfg: {
assertion = config.ports.proxied.enable == cfg.proxied.enabled; assertion = config.ports.proxied.enable == cfg.proxied.enable;
message = "proxied mismatch"; message = "proxied mismatch";
}; };
assertProxiedPort = nixosConfig: cfg: { assertProxiedPort = nixosConfig: cfg: {

View file

@ -8,7 +8,10 @@
name.shortServer = mkDefault "bbuddy"; name.shortServer = mkDefault "bbuddy";
serverName = "@bbuddy_internal"; serverName = "@bbuddy_internal";
in { in {
config.services.nginx.vouch.enable = true; config.services.nginx = {
vouch.enable = true;
proxied.enable = true;
};
config.services.nginx.virtualHosts = { config.services.nginx.virtualHosts = {
barcodebuddy'php = mkIf barcodebuddy.enable { barcodebuddy'php = mkIf barcodebuddy.enable {
inherit serverName; inherit serverName;

View file

@ -49,6 +49,7 @@ in {
config.services.nginx = { config.services.nginx = {
lua.http.enable = true; lua.http.enable = true;
vouch.enable = true; vouch.enable = true;
proxied.enable = true;
virtualHosts = { virtualHosts = {
grocy'php = mkIf grocy.enable { grocy'php = mkIf grocy.enable {
inherit serverName; inherit serverName;

View file

@ -9,9 +9,9 @@
inherit (config.services) nginx; inherit (config.services) nginx;
cfg = config.services.invidious; cfg = config.services.invidious;
upstreamName = "invidious'access"; upstreamName = "invidious'access";
upstreamNginx = "invidious'access'nginx";
in { in {
config.services.nginx = { config.services.nginx = {
proxied.enable = true;
upstreams' = { upstreams' = {
${upstreamName}.servers = { ${upstreamName}.servers = {
local = { local = {
@ -26,15 +26,6 @@ in {
}; };
}; };
}; };
${upstreamNginx} = {
enable = mkDefault nginx.virtualHosts.invidious'int.enable;
host = mkDefault nginx.virtualHosts.invidious'int.serverName;
servers.local = {
accessService = {
inherit (nginx.upstreams'.nginx'proxied.servers.local.accessService) system name id port;
};
};
};
}; };
virtualHosts = let virtualHosts = let
invidiousDomains = invidiousDomains =
@ -66,7 +57,10 @@ in {
invidious = { invidious = {
# lua can't handle HTTP 2.0 requests, so layer it behind another proxy... # lua can't handle HTTP 2.0 requests, so layer it behind another proxy...
inherit name extraConfig; inherit name extraConfig;
proxy.upstream = upstreamNginx; proxy = mkIf nginx.virtualHosts.invidious'int.enable {
upstream = "nginx'proxied";
host = mkDefault nginx.virtualHosts.invidious'int.serverName;
};
locations."/" = { xvars, virtualHost, ... }: { locations."/" = { xvars, virtualHost, ... }: {
proxy.enable = true; proxy.enable = true;
extraConfig = '' extraConfig = ''

View file

@ -8,6 +8,7 @@
cfg = config.services.vouch-proxy; cfg = config.services.vouch-proxy;
in { in {
config.services.nginx = { config.services.nginx = {
proxied.enable = true;
upstreams'.vouch'access.servers.access = { upstreams'.vouch'access.servers.access = {
accessService = { accessService = {
inherit (nginx.upstreams'.vouch'auth.servers.service.accessService) system name id port; inherit (nginx.upstreams'.vouch'auth.servers.service.accessService) system name id port;

View file

@ -37,6 +37,7 @@ in {
virtualHosts.fallback = { virtualHosts.fallback = {
serverName = null; serverName = null;
default = mkDefault true; default = mkDefault true;
reuseport = mkDefault true;
locations."/".extraConfig = mkDefault '' locations."/".extraConfig = mkDefault ''
return 404; return 404;
''; '';

View file

@ -213,6 +213,7 @@ in {
}; };
services.nginx = { services.nginx = {
proxied.enable = true;
vouch.enable = true; vouch.enable = true;
upstreams' = { upstreams' = {
vouch'auth.servers.local.enable = false; vouch'auth.servers.local.enable = false;

View file

@ -24,6 +24,7 @@
]; ];
services.nginx = { services.nginx = {
proxied.enable = true;
virtualHosts = { virtualHosts = {
zigbee2mqtt.proxied.enable = "cloudflared"; zigbee2mqtt.proxied.enable = "cloudflared";
grocy.proxied.enable = "cloudflared"; grocy.proxied.enable = "cloudflared";

View file

@ -29,6 +29,7 @@ in {
}; };
services.nginx = { services.nginx = {
proxied.enable = true;
virtualHosts = { virtualHosts = {
unifi.proxied.enable = "cloudflared"; unifi.proxied.enable = "cloudflared";
}; };