modules.services.nginx.http: cleanup

This commit is contained in:
2025-07-06 17:00:13 +08:00
parent c3491c8804
commit a18d464a58
2 changed files with 67 additions and 84 deletions

View File

@@ -17,7 +17,7 @@ inputs:
httpsPort = 3065;
httpsPortShift = { http2 = 1; proxyProtocol = 2; };
httpsLocationTypes = [ "proxy" "static" "php" "return" "cgi" "alias" ];
httpTypes = [ "rewriteHttps" "php" ];
httpTypes = [ "rewriteHttps" "php" "proxy" ];
streamPort = 5575;
streamPortShift.proxyProtocol = 1;
};

View File

@@ -1,96 +1,79 @@
inputs:
{
options.nixos.services.nginx = let inherit (inputs.lib) mkOption types; in
options.nixos.services.nginx.http = let inherit (inputs.lib) mkOption types; in mkOption
{
http = mkOption
type = types.attrsOf (types.submodule (submoduleInputs: { options =
{
type = types.attrsOf (types.submodule (submoduleInputs: { options =
rewriteHttps = mkOption
{
rewriteHttps = mkOption
type = types.nullOr (types.submodule { options =
{
type = types.nullOr (types.submodule { options =
{
hostname = mkOption { type = types.nonEmptyStr; default = submoduleInputs.config._module.args.name; };
};});
default = null;
};
php = mkOption
hostname = mkOption { type = types.nonEmptyStr; default = submoduleInputs.config._module.args.name; };
};});
default = null;
};
php = mkOption
{
type = types.nullOr (types.submodule { options =
{ root = mkOption { type = types.nonEmptyStr; }; fastcgiPass = mkOption { type = types.nonEmptyStr; };};});
default = null;
};
proxy = mkOption
{
type = types.nullOr (types.submodule { options =
{
type = types.nullOr (types.submodule { options =
{ root = mkOption { type = types.nonEmptyStr; }; fastcgiPass = mkOption { type = types.nonEmptyStr; };};});
default = null;
};
proxy = mkOption
{
type = types.nullOr (types.submodule { options =
{
upstream = mkOption { type = types.nonEmptyStr; };
websocket = mkOption { type = types.bool; default = false; };
setHeaders = mkOption
{
type = types.attrsOf types.str;
default.Host = submoduleInputs.config._module.args.name;
};
};});
default = null;
};
};}));
default = {};
};
upstream = mkOption { type = types.nonEmptyStr; };
websocket = mkOption { type = types.bool; default = false; };
setHeaders = mkOption
{ type = types.attrsOf types.str; default.Host = submoduleInputs.config._module.args.name; };
};});
default = null;
};
};}));
default = {};
};
config =
let
inherit (inputs.localLib) attrsToList;
inherit (inputs.config.nixos.services) nginx;
inherit (builtins) map listToAttrs concatStringsSep toString filter attrValues concatLists;
concatAttrs = list: listToAttrs (concatLists (map (attrs: attrsToList attrs) list));
in inputs.lib.mkIf nginx.enable (inputs.lib.mkMerge
[
config = let inherit (inputs.config.nixos.services) nginx; in inputs.lib.mkIf nginx.enable
{
assertions = inputs.lib.mapAttrsToList
(n: v:
{
assertions = map
(site:
assertion = (inputs.lib.count (x: x != null) (builtins.map (type: v.${type}) nginx.global.httpTypes)) <= 1;
message = "Only one type shuold be specified in ${n}";
})
nginx.http;
services.nginx.virtualHosts = inputs.lib.mapAttrs'
(n: v:
{
name = "http.${n}";
value = { serverName = n; listen = [ { addr = "0.0.0.0"; port = 80; } ]; }
// (inputs.lib.optionalAttrs (v.rewriteHttps != null)
{ locations."/".return = "301 https://${v.rewriteHttps.hostname}$request_uri"; })
// (inputs.lib.optionalAttrs (v.php != null)
{
assertion = (inputs.lib.count (x: x != null) (map (type: site.value.${type}) nginx.global.httpTypes)) <= 1;
message = "Only one type shuold be specified in ${site.name}";
extraConfig = "index index.php;";
root = v.php.root;
locations."~ ^.+?.php(/.*)?$".extraConfig =
''
fastcgi_pass ${v.php.fastcgiPass};
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include ${inputs.config.services.nginx.package}/conf/fastcgi.conf;
'';
})
(attrsToList nginx.http);
services.nginx.virtualHosts = listToAttrs (map
(site:
// (inputs.lib.optionalAttrs (v.proxy != null)
{
name = "http.${site.name}";
value = { serverName = site.name; listen = [ { addr = "0.0.0.0"; port = 80; } ]; }
// (if site.value.rewriteHttps != null then
{ locations."/".return = "301 https://${site.value.rewriteHttps.hostname}$request_uri"; }
else {})
// (if site.value.php != null then
{
extraConfig = "index index.php;";
root = site.value.php.root;
locations."~ ^.+?.php(/.*)?$".extraConfig =
''
fastcgi_pass ${site.value.php.fastcgiPass};
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include ${inputs.config.services.nginx.package}/conf/fastcgi.conf;
'';
}
else {})
// (if site.value.proxy != null then
{
locations."/" =
{
proxyPass = site.value.proxy.upstream;
proxyWebsockets = site.value.proxy.websocket;
recommendedProxySettings = false;
recommendedProxySettingsNoHost = true;
extraConfig = builtins.concatStringsSep "\n" (builtins.map
(header: ''proxy_set_header ${header.name} "${header.value}";'')
(inputs.localLib.attrsToList site.value.proxy.setHeaders));
};
}
else {});
})
(attrsToList nginx.http));
}
]);
locations."/" =
{
proxyPass = v.proxy.upstream;
proxyWebsockets = v.proxy.websocket;
recommendedProxySettings = false;
recommendedProxySettingsNoHost = true;
extraConfig = builtins.concatStringsSep "\n" (inputs.lib.mapAttrsToList
(n: v: ''proxy_set_header ${n} "${v}";'')
v.proxy.setHeaders);
};
});
})
nginx.http;
};
}