refactor(nginx): websocket proxy settings

This commit is contained in:
arcnmx 2024-01-22 14:13:49 -08:00
parent a0bd07f898
commit ba7f32ddcb
4 changed files with 52 additions and 26 deletions

View file

@ -0,0 +1,34 @@
{
lib,
...
}: let
inherit (lib.modules) mkIf;
inherit (lib.options) mkOption mkEnableOption;
wsModule = { config, ... }: {
options = with lib.types; {
proxy.websocket.enable = mkEnableOption "websocket proxy";
};
config = mkIf config.proxy.websocket.enable {
extraConfig = ''
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
'';
};
};
hostModule = { config, ... }: {
imports = [ wsModule ];
options = with lib.types; {
locations = mkOption {
type = attrsOf (submodule wsModule);
};
};
};
in {
options = with lib.types; {
services.nginx.virtualHosts = mkOption {
type = attrsOf (submodule hostModule);
};
};
}

View file

@ -4,7 +4,7 @@
... ...
}: let }: let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.modules) mkIf; inherit (lib.modules) mkIf mkOptionDefault;
cfg = config.services.plex; cfg = config.services.plex;
access = config.services.nginx.access.plex; access = config.services.nginx.access.plex;
in { in {
@ -23,7 +23,7 @@ in {
}; };
config.services.nginx = { config.services.nginx = {
access.plex = mkIf cfg.enable { access.plex = mkIf cfg.enable {
url = "http://localhost:32400"; url = mkOptionDefault "http://localhost:32400";
}; };
virtualHosts = let virtualHosts = let
extraConfig = '' extraConfig = ''
@ -46,18 +46,18 @@ in {
proxy_redirect off; proxy_redirect off;
proxy_buffering off; proxy_buffering off;
''; '';
location = {
proxy.websocket.enable = true;
proxyPass = access.url;
};
in { in {
${access.domain} = { ${access.domain} = {
locations."/" = { locations."/" = location;
proxyPass = access.url;
};
inherit extraConfig; inherit extraConfig;
}; };
${access.localDomain} = { ${access.localDomain} = {
local.enable = true; local.enable = true;
locations."/" = { locations."/" = location;
proxyPass = access.url;
};
inherit extraConfig; inherit extraConfig;
}; };
}; };

View file

@ -59,12 +59,10 @@ in {
''; '';
}; };
locations."/prox/api2/" = { locations."/prox/api2/" = {
proxy.websocket.enable = true;
proxyPass = "${proxyPass}api2/"; proxyPass = "${proxyPass}api2/";
extraConfig = '' extraConfig = ''
internal; internal;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
''; '';
}; };
}; };
@ -73,6 +71,7 @@ in {
forceSSL = mkDefault true; forceSSL = mkDefault true;
inherit sslCertificate sslCertificateKey; inherit sslCertificate sslCertificateKey;
locations."/" = { locations."/" = {
proxy.websocket.enable = true;
inherit proxyPass; inherit proxyPass;
}; };
}; };
@ -80,6 +79,7 @@ in {
local.enable = mkDefault true; local.enable = mkDefault true;
inherit sslCertificate sslCertificateKey; inherit sslCertificate sslCertificateKey;
locations."/" = { locations."/" = {
proxy.websocket.enable = true;
inherit proxyPass; inherit proxyPass;
}; };
}; };

View file

@ -8,12 +8,10 @@ let
inherit (lib.modules) mkIf mkDefault mkOptionDefault; inherit (lib.modules) mkIf mkDefault mkOptionDefault;
cfg = config.services.zigbee2mqtt; cfg = config.services.zigbee2mqtt;
access = config.services.nginx.access.zigbee2mqtt; access = config.services.nginx.access.zigbee2mqtt;
proxyPass = mkDefault "http://${access.host}:${toString access.port}"; location = {
extraConfig = '' proxy.websocket.enable = true;
proxy_set_header Upgrade $http_upgrade; proxyPass = mkDefault "http://${access.host}:${toString access.port}";
proxy_set_header Connection "upgrade"; };
proxy_http_version 1.1;
'';
in { in {
options.services.nginx.access.zigbee2mqtt = with lib.types; { options.services.nginx.access.zigbee2mqtt = with lib.types; {
host = mkOption { host = mkOption {
@ -41,21 +39,15 @@ in {
virtualHosts = { virtualHosts = {
${access.domain} = { ${access.domain} = {
vouch.enable = true; vouch.enable = true;
locations."/" = { locations."/" = location;
inherit proxyPass extraConfig;
};
}; };
${access.localDomain} = { ${access.localDomain} = {
local.enable = true; local.enable = true;
locations."/" = { locations."/" = location;
inherit proxyPass extraConfig;
};
}; };
"z2m.tail.${config.networking.domain}" = mkIf config.services.tailscale.enable { "z2m.tail.${config.networking.domain}" = mkIf config.services.tailscale.enable {
local.enable = true; local.enable = true;
locations."/" = { locations."/" = location;
inherit proxyPass extraConfig;
};
}; };
}; };
}; };