Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/nginx: add locations."name".uwsgiPass option and use it #346776

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nixos/modules/services/mail/mailman.nix
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ in {
enable = lib.mkDefault true;
virtualHosts = lib.genAttrs cfg.webHosts (webHost: {
locations = {
${cfg.serve.virtualRoot}.extraConfig = "uwsgi_pass unix:/run/mailman-web.socket;";
${cfg.serve.virtualRoot}.uwsgiPass = "unix:/run/mailman-web.socket";
"${lib.removeSuffix "/" cfg.serve.virtualRoot}/static/".alias = webSettings.STATIC_ROOT + "/";
};
});
Expand Down
64 changes: 63 additions & 1 deletion nixos/modules/services/web-servers/nginx/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,22 @@ let
REDIRECT_STATUS = "200";
};

recommendedProxyConfig = pkgs.writeText "nginx-recommended-proxy-headers.conf" ''
recommendedProxyConfig = pkgs.writeText "nginx-recommended-proxy_set_header-headers.conf" ''
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
'';
recommendedUwsgiConfig = pkgs.writeText "nginx-recommended-uwsgi_param-headers.conf" ''
uwsgi_param HTTP_HOST $host;
uwsgi_param HTTP_X_REAL_IP $remote_addr;
uwsgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;
uwsgi_param HTTP_X_FORWARDED_PROTO $scheme;
uwsgi_param HTTP_X_FORWARDED_HOST $host;
uwsgi_param HTTP_X_FORWARDED_SERVER $host;
Comment on lines +106 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused why these are needed and if anything even would use them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uwsgi_params are generally used by uwsgi and/or its plugins, not directly accessed by applications. Some of the very specific use-cases are here https://uwsgi-docs.readthedocs.io/en/latest/Nginx.html

I can't say the above params are recommendedUwsgiConfig

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are basically a mapping of the proxy_pass variables.

'';

proxyCachePathConfig = concatStringsSep "\n" (mapAttrsToList (name: proxyCachePath: ''
proxy_cache_path ${concatStringsSep " " [
Expand Down Expand Up @@ -238,6 +246,15 @@ let
include ${recommendedProxyConfig};
''}

${optionalString cfg.recommendedUwsgiSettings ''
uwsgi_connect_timeout ${cfg.uwsgiTimeout};
uwsgi_send_timeout ${cfg.uwsgiTimeout};
uwsgi_read_timeout ${cfg.uwsgiTimeout};
uwsgi_param HTTP_CONNECTION "";
include ${cfg.package}/conf/uwsgi_params;
include ${recommendedUwsgiConfig};
''}

${optionalString (cfg.mapHashBucketSize != null) ''
map_hash_bucket_size ${toString cfg.mapHashBucketSize};
''}
Expand Down Expand Up @@ -442,6 +459,13 @@ let
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
''}
${optionalString (config.uwsgiPass != null && !cfg.uwsgiResolveWhileRunning)
"uwsgi_pass ${config.uwsgiPass};"
SuperSandro2000 marked this conversation as resolved.
Show resolved Hide resolved
}
${optionalString (config.uwsgiPass != null && cfg.uwsgiResolveWhileRunning) ''
set $nix_proxy_target "${config.uwsgiPass}";
uwsgi_pass $nix_proxy_target;
''}
${concatStringsSep "\n"
(mapAttrsToList (n: v: ''fastcgi_param ${n} "${v}";'')
(optionalAttrs (config.fastcgiParams != {})
Expand All @@ -453,6 +477,7 @@ let
${optionalString (config.return != null) "return ${toString config.return};"}
${config.extraConfig}
${optionalString (config.proxyPass != null && config.recommendedProxySettings) "include ${recommendedProxyConfig};"}
${optionalString (config.uwsgiPass != null && config.recommendedUwsgiSettings) "include ${cfg.package}/conf/uwsgi_params; include ${recommendedUwsgiConfig};"}
${mkBasicAuth "sublocation" config}
}
'') (sortProperties (mapAttrsToList (k: v: v // { location = k; }) locations)));
Expand Down Expand Up @@ -553,6 +578,23 @@ in
'';
};

recommendedUwsgiSettings = mkOption {
default = false;
type = types.bool;
description = ''
Whether to enable recommended uwsgi settings if a vhost does not specify the option manually.
'';
};

uwsgiTimeout = mkOption {
type = types.str;
default = "60s";
example = "20s";
description = ''
Change the uwsgi related timeouts in recommendedUwsgiSettings.
'';
};

defaultListen = mkOption {
type = with types; listOf (submodule {
options = {
Expand Down Expand Up @@ -859,6 +901,16 @@ in
'';
};

uwsgiResolveWhileRunning = mkOption {
type = types.bool;
default = false;
description = ''
Resolves domains of uwsgi targets at runtime
and not only at start, you have to set
services.nginx.resolver, too.
'';
};

mapHashBucketSize = mkOption {
type = types.nullOr (types.enum [ 32 64 128 ]);
default = null;
Expand Down Expand Up @@ -1163,6 +1215,16 @@ in
'';
}

{
assertion = all (host:
all (location: !(location.proxyPass != null && location.uwsgiPass != null)) (attrValues host.locations))
(attrValues virtualHosts);
message = ''
Options services.nginx.service.virtualHosts.<name>.proxyPass and
services.nginx.virtualHosts.<name>.uwsgiPass are mutually exclusive.
'';
}
SuperSandro2000 marked this conversation as resolved.
Show resolved Hide resolved

{
assertion = cfg.package.pname != "nginxQuic" && cfg.package.pname != "angieQuic" -> !(cfg.enableQuicBPF);
message = ''
Expand Down
19 changes: 19 additions & 0 deletions nixos/modules/services/web-servers/nginx/location-options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ with lib;
'';
};

uwsgiPass = mkOption {
type = types.nullOr types.str;
default = null;
example = "unix:/run/example/example.sock";
description = ''
Adds uwsgi_pass directive and sets recommended proxy headers if
recommendedUwsgiSettings is enabled.
'';
};

index = mkOption {
type = types.nullOr types.str;
default = null;
Expand Down Expand Up @@ -137,5 +147,14 @@ with lib;
Enable recommended proxy settings.
'';
};

recommendedUwsgiSettings = mkOption {
type = types.bool;
default = config.services.nginx.recommendedUwsgiSettings;
defaultText = literalExpression "config.services.nginx.recommendedUwsgiSettings";
description = ''
Enable recommended uwsgi settings.
'';
};
};
}
Loading