Skip to content

Commit d1a28bb

Browse files
nixos/nginx: add locations."name".uwsgiPass and related options and use it
1 parent 73bed75 commit d1a28bb

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

nixos/modules/services/mail/mailman.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ in {
441441
enable = lib.mkDefault true;
442442
virtualHosts = lib.genAttrs cfg.webHosts (webHost: {
443443
locations = {
444-
${cfg.serve.virtualRoot}.extraConfig = "uwsgi_pass unix:/run/mailman-web.socket;";
444+
${cfg.serve.virtualRoot}.uwsgiPass = "unix:/run/mailman-web.socket";
445445
"${lib.removeSuffix "/" cfg.serve.virtualRoot}/static/".alias = webSettings.STATIC_ROOT + "/";
446446
};
447447
});

nixos/modules/services/web-servers/nginx/default.nix

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,22 @@ let
9494
REDIRECT_STATUS = "200";
9595
};
9696

97-
recommendedProxyConfig = pkgs.writeText "nginx-recommended-proxy-headers.conf" ''
97+
recommendedProxyConfig = pkgs.writeText "nginx-recommended-proxy_set_header-headers.conf" ''
9898
proxy_set_header Host $host;
9999
proxy_set_header X-Real-IP $remote_addr;
100100
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
101101
proxy_set_header X-Forwarded-Proto $scheme;
102102
proxy_set_header X-Forwarded-Host $host;
103103
proxy_set_header X-Forwarded-Server $host;
104104
'';
105+
recommendedUwsgiConfig = pkgs.writeText "nginx-recommended-uwsgi_param-headers.conf" ''
106+
uwsgi_param HTTP_HOST $host;
107+
uwsgi_param HTTP_X_REAL_IP $remote_addr;
108+
uwsgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;
109+
uwsgi_param HTTP_X_FORWARDED_PROTO $scheme;
110+
uwsgi_param HTTP_X_FORWARDED_HOST $host;
111+
uwsgi_param HTTP_X_FORWARDED_SERVER $host;
112+
'';
105113

106114
proxyCachePathConfig = concatStringsSep "\n" (mapAttrsToList (name: proxyCachePath: ''
107115
proxy_cache_path ${concatStringsSep " " [
@@ -238,6 +246,15 @@ let
238246
include ${recommendedProxyConfig};
239247
''}
240248
249+
${optionalString cfg.recommendedUwsgiSettings ''
250+
uwsgi_connect_timeout ${cfg.uwsgiTimeout};
251+
uwsgi_send_timeout ${cfg.uwsgiTimeout};
252+
uwsgi_read_timeout ${cfg.uwsgiTimeout};
253+
uwsgi_param HTTP_CONNECTION "";
254+
include ${cfg.package}/conf/uwsgi_params;
255+
include ${recommendedUwsgiConfig};
256+
''}
257+
241258
${optionalString (cfg.mapHashBucketSize != null) ''
242259
map_hash_bucket_size ${toString cfg.mapHashBucketSize};
243260
''}
@@ -442,6 +459,13 @@ let
442459
proxy_set_header Upgrade $http_upgrade;
443460
proxy_set_header Connection $connection_upgrade;
444461
''}
462+
${optionalString (config.uwsgiPass != null && !cfg.uwsgiResolveWhileRunning)
463+
"uwsgi_pass ${config.uwsgiPass};"
464+
}
465+
${optionalString (config.uwsgiPass != null && cfg.uwsgiResolveWhileRunning) ''
466+
set $nix_proxy_target "${config.uwsgiPass}";
467+
uwsgi_pass $nix_proxy_target;
468+
''}
445469
${concatStringsSep "\n"
446470
(mapAttrsToList (n: v: ''fastcgi_param ${n} "${v}";'')
447471
(optionalAttrs (config.fastcgiParams != {})
@@ -453,6 +477,7 @@ let
453477
${optionalString (config.return != null) "return ${toString config.return};"}
454478
${config.extraConfig}
455479
${optionalString (config.proxyPass != null && config.recommendedProxySettings) "include ${recommendedProxyConfig};"}
480+
${optionalString (config.uwsgiPass != null && config.recommendedUwsgiSettings) "include ${cfg.package}/conf/uwsgi_params; include ${recommendedUwsgiConfig};"}
456481
${mkBasicAuth "sublocation" config}
457482
}
458483
'') (sortProperties (mapAttrsToList (k: v: v // { location = k; }) locations)));
@@ -553,6 +578,23 @@ in
553578
'';
554579
};
555580

581+
recommendedUwsgiSettings = mkOption {
582+
default = false;
583+
type = types.bool;
584+
description = ''
585+
Whether to enable recommended uwsgi settings if a vhost does not specify the option manually.
586+
'';
587+
};
588+
589+
uwsgiTimeout = mkOption {
590+
type = types.str;
591+
default = "60s";
592+
example = "20s";
593+
description = ''
594+
Change the uwsgi related timeouts in recommendedUwsgiSettings.
595+
'';
596+
};
597+
556598
defaultListen = mkOption {
557599
type = with types; listOf (submodule {
558600
options = {
@@ -859,6 +901,16 @@ in
859901
'';
860902
};
861903

904+
uwsgiResolveWhileRunning = mkOption {
905+
type = types.bool;
906+
default = false;
907+
description = ''
908+
Resolves domains of uwsgi targets at runtime
909+
and not only at start, you have to set
910+
services.nginx.resolver, too.
911+
'';
912+
};
913+
862914
mapHashBucketSize = mkOption {
863915
type = types.nullOr (types.enum [ 32 64 128 ]);
864916
default = null;
@@ -1163,6 +1215,16 @@ in
11631215
'';
11641216
}
11651217

1218+
{
1219+
assertion = all (host:
1220+
all (location: !(location.proxyPass != null && location.uwsgiPass != null)) (attrValues host.locations))
1221+
(attrValues virtualHosts);
1222+
message = ''
1223+
Options services.nginx.service.virtualHosts.<name>.proxyPass and
1224+
services.nginx.virtualHosts.<name>.uwsgiPass are mutually exclusive.
1225+
'';
1226+
}
1227+
11661228
{
11671229
assertion = cfg.package.pname != "nginxQuic" && cfg.package.pname != "angieQuic" -> !(cfg.enableQuicBPF);
11681230
message = ''

nixos/modules/services/web-servers/nginx/location-options.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ with lib;
5656
'';
5757
};
5858

59+
uwsgiPass = mkOption {
60+
type = types.nullOr types.str;
61+
default = null;
62+
example = "unix:/run/example/example.sock";
63+
description = ''
64+
Adds uwsgi_pass directive and sets recommended proxy headers if
65+
recommendedUwsgiSettings is enabled.
66+
'';
67+
};
68+
5969
index = mkOption {
6070
type = types.nullOr types.str;
6171
default = null;
@@ -137,5 +147,14 @@ with lib;
137147
Enable recommended proxy settings.
138148
'';
139149
};
150+
151+
recommendedUwsgiSettings = mkOption {
152+
type = types.bool;
153+
default = config.services.nginx.recommendedUwsgiSettings;
154+
defaultText = literalExpression "config.services.nginx.recommendedUwsgiSettings";
155+
description = ''
156+
Enable recommended uwsgi settings.
157+
'';
158+
};
140159
};
141160
}

0 commit comments

Comments
 (0)