From ff2e2e82ccc2eebc36220613b332efa475dcca51 Mon Sep 17 00:00:00 2001 From: Evan Danaher Date: Thu, 9 Mar 2017 13:02:29 -0500 Subject: [PATCH 1/3] nginx: Add alias configuration option for hosts and locations. It's like root, but doesn't keep the prefix. --- nixos/modules/services/web-servers/nginx/default.nix | 2 ++ .../services/web-servers/nginx/location-options.nix | 9 +++++++++ .../modules/services/web-servers/nginx/vhost-options.nix | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/nixos/modules/services/web-servers/nginx/default.nix b/nixos/modules/services/web-servers/nginx/default.nix index 9e93e56b9c2c..4ad272deb376 100644 --- a/nixos/modules/services/web-servers/nginx/default.nix +++ b/nixos/modules/services/web-servers/nginx/default.nix @@ -158,6 +158,7 @@ let server_name ${serverName} ${concatStringsSep " " vhost.serverAliases}; ${acmeLocation} ${optionalString (vhost.root != null) "root ${vhost.root};"} + ${optionalString (vhost.alias != null) "alias ${vhost.alias};"} ${optionalString (vhost.globalRedirect != null) '' return 301 http${optionalString ssl "s"}://${vhost.globalRedirect}$request_uri; ''} @@ -180,6 +181,7 @@ let ${optionalString (config.index != null) "index ${config.index};"} ${optionalString (config.tryFiles != null) "try_files ${config.tryFiles};"} ${optionalString (config.root != null) "root ${config.root};"} + ${optionalString (config.alias != null) "alias ${config.alias};"} ${config.extraConfig} } '') locations); diff --git a/nixos/modules/services/web-servers/nginx/location-options.nix b/nixos/modules/services/web-servers/nginx/location-options.nix index e1885b160664..83ce0f717341 100644 --- a/nixos/modules/services/web-servers/nginx/location-options.nix +++ b/nixos/modules/services/web-servers/nginx/location-options.nix @@ -45,6 +45,15 @@ with lib; ''; }; + alias = mkOption { + type = types.nullOr types.path; + default = null; + example = "/your/alias/directory"; + description = '' + Alias directory for requests. + ''; + }; + extraConfig = mkOption { type = types.lines; default = ""; diff --git a/nixos/modules/services/web-servers/nginx/vhost-options.nix b/nixos/modules/services/web-servers/nginx/vhost-options.nix index c0ea645b3dfe..a76a971aff06 100644 --- a/nixos/modules/services/web-servers/nginx/vhost-options.nix +++ b/nixos/modules/services/web-servers/nginx/vhost-options.nix @@ -89,6 +89,15 @@ with lib; ''; }; + alias = mkOption { + type = types.nullOr types.path; + default = null; + example = "/data/webserver/docs"; + description = '' + The path of the web alias directory. + ''; + }; + default = mkOption { type = types.bool; default = false; From e7358b192a3cdfc7d64fa2a21321df03b2256345 Mon Sep 17 00:00:00 2001 From: Evan Danaher Date: Thu, 9 Mar 2017 13:02:49 -0500 Subject: [PATCH 2/3] nginx: Assert that either root or alias is null. If both are set, nginx won't start. More error checking is certainly in order, but this seems like a reasonable start. --- nixos/modules/services/web-servers/nginx/default.nix | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nixos/modules/services/web-servers/nginx/default.nix b/nixos/modules/services/web-servers/nginx/default.nix index 4ad272deb376..72cc10caef11 100644 --- a/nixos/modules/services/web-servers/nginx/default.nix +++ b/nixos/modules/services/web-servers/nginx/default.nix @@ -380,6 +380,15 @@ in config = mkIf cfg.enable { # TODO: test user supplied config file pases syntax test + assertions = let hostOrAliasIsNull = l: l.root == null || l.alias == null; in [ { + assertion = all hostOrAliasIsNull (attrValues virtualHosts); + message = "Only one of nginx root or alias can be specified on a virtualHost."; + } { + assertion = all (host: all hostOrAliasIsNull (attrValues host.locations)) (attrValues virtualHosts); + message = "Only one of nginx root or alias can be specified on a location."; + } + ]; + systemd.services.nginx = { description = "Nginx Web Server"; after = [ "network.target" ]; From a09246948cafb7672ea1e5590688e7eb6a16cef3 Mon Sep 17 00:00:00 2001 From: Evan Danaher Date: Thu, 9 Mar 2017 16:54:44 -0500 Subject: [PATCH 3/3] nginx: disallow alias directive on server level; it doesn't work. --- nixos/modules/services/web-servers/nginx/default.nix | 7 ++----- .../modules/services/web-servers/nginx/vhost-options.nix | 9 --------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/nixos/modules/services/web-servers/nginx/default.nix b/nixos/modules/services/web-servers/nginx/default.nix index 72cc10caef11..709fce522d52 100644 --- a/nixos/modules/services/web-servers/nginx/default.nix +++ b/nixos/modules/services/web-servers/nginx/default.nix @@ -158,7 +158,6 @@ let server_name ${serverName} ${concatStringsSep " " vhost.serverAliases}; ${acmeLocation} ${optionalString (vhost.root != null) "root ${vhost.root};"} - ${optionalString (vhost.alias != null) "alias ${vhost.alias};"} ${optionalString (vhost.globalRedirect != null) '' return 301 http${optionalString ssl "s"}://${vhost.globalRedirect}$request_uri; ''} @@ -380,10 +379,8 @@ in config = mkIf cfg.enable { # TODO: test user supplied config file pases syntax test - assertions = let hostOrAliasIsNull = l: l.root == null || l.alias == null; in [ { - assertion = all hostOrAliasIsNull (attrValues virtualHosts); - message = "Only one of nginx root or alias can be specified on a virtualHost."; - } { + assertions = let hostOrAliasIsNull = l: l.root == null || l.alias == null; in [ + { assertion = all (host: all hostOrAliasIsNull (attrValues host.locations)) (attrValues virtualHosts); message = "Only one of nginx root or alias can be specified on a location."; } diff --git a/nixos/modules/services/web-servers/nginx/vhost-options.nix b/nixos/modules/services/web-servers/nginx/vhost-options.nix index a76a971aff06..c0ea645b3dfe 100644 --- a/nixos/modules/services/web-servers/nginx/vhost-options.nix +++ b/nixos/modules/services/web-servers/nginx/vhost-options.nix @@ -89,15 +89,6 @@ with lib; ''; }; - alias = mkOption { - type = types.nullOr types.path; - default = null; - example = "/data/webserver/docs"; - description = '' - The path of the web alias directory. - ''; - }; - default = mkOption { type = types.bool; default = false;