git.iko.soy/typeable/nixpkgs
on master help sign in
nginx: added serverName option for virtualHosts

This allows overriding the `server_name` attribute of virtual hosts. By doing so it is possible to have multiple virtualHost definitions that share the same `server_name`. This is useful in particular when you need a HTTP as well as a HTTPS virtualhost: same server_name, different port.

commitd9987f360a72b97b346f13f7d5ec1bf58f284e52 authorBob van der Linden <bobvanderlinden@gmail.com> dateMon 16 Jan 2017, 16:46 (10 years ago) parentb9b95aa
2 files changed+33−16
nixos/modules/services/web-servers/nginx/default.nix+24−16 nixos/modules/services/web-servers/nginx/vhost-options.nix+9
nixos/modules/services/web-servers/nginx/default.nix+24−16
@@ -5,7 +5,11 @@ with lib;
55let
66 cfg = config.services.nginx;
77 virtualHosts = mapAttrs (vhostName: vhostConfig:
8− vhostConfig // (optionalAttrs vhostConfig.enableACME {
8+ vhostConfig // {
9+ serverName = if vhostConfig.serverName != null
10+ then vhostConfig.serverName
11+ else vhostName;
12+ } // (optionalAttrs vhostConfig.enableACME {
913 sslCertificate = "/var/lib/acme/${vhostName}/fullchain.pem";
1014 sslCertificateKey = "/var/lib/acme/${vhostName}/key.pem";
1115 })
@@ -112,8 +116,9 @@ let
112116 ${cfg.appendConfig}
113117 '';
114118
115− vhosts = concatStringsSep "\n" (mapAttrsToList (serverName: vhost:
119+ vhosts = concatStringsSep "\n" (mapAttrsToList (vhostName: vhost:
116120 let
121+ serverName = vhost.serverName;
117122 ssl = vhost.enableSSL || vhost.forceSSL;
118123 port = if vhost.port != null then vhost.port else (if ssl then 443 else 80);
119124 listenString = toString port + optionalString ssl " ssl http2"
@@ -161,7 +166,7 @@ let
161166 ssl_certificate_key ${vhost.sslCertificateKey};
162167 ''}
163168
164− ${optionalString (vhost.basicAuth != {}) (mkBasicAuth serverName vhost.basicAuth)}
169+ ${optionalString (vhost.basicAuth != {}) (mkBasicAuth vhostName vhost.basicAuth)}
165170
166171 ${mkLocations vhost.locations}
167172
@@ -178,8 +183,8 @@ let
178183 ${config.extraConfig}
179184 }
180185 '') locations);
181− mkBasicAuth = serverName: authDef: let
182− htpasswdFile = pkgs.writeText "${serverName}.htpasswd" (
186+ mkBasicAuth = vhostName: authDef: let
187+ htpasswdFile = pkgs.writeText "${vhostName}.htpasswd" (
183188 concatStringsSep "\n" (mapAttrsToList (user: password: ''
184189 ${user}:{PLAIN}${password}
185190 '') authDef)
@@ -393,17 +398,20 @@ in
393398 };
394399
395400 security.acme.certs = filterAttrs (n: v: v != {}) (
396− mapAttrs (vhostName: vhostConfig:
397− optionalAttrs vhostConfig.enableACME {
398− user = cfg.user;
399− group = cfg.group;
400− webroot = vhostConfig.acmeRoot;
401− extraDomains = genAttrs vhostConfig.serverAliases (alias: null);
402− postRun = ''
403− systemctl reload nginx
404− '';
405− }
406− ) virtualHosts
401+ let
402+ vhostsConfigs = mapAttrsToList (vhostName: vhostConfig: vhostConfig) virtualHosts;
403+ acmeEnabledVhosts = filter (vhostConfig: vhostConfig.enableACME) vhostsConfigs;
404+ acmePairs = map (vhostConfig: { name = vhostConfig.serverName; value = {
405+ user = cfg.user;
406+ group = cfg.group;
407+ webroot = vhostConfig.acmeRoot;
408+ extraDomains = genAttrs vhostConfig.serverAliases (alias: null);
409+ postRun = ''
410+ systemctl reload nginx
411+ '';
412+ }; }) acmeEnabledVhosts;
413+ in
414+ listToAttrs acmePairs
407415 );
408416
409417 users.extraUsers = optionalAttrs (cfg.user == "nginx") (singleton
nixos/modules/services/web-servers/nginx/vhost-options.nix+9
@@ -8,6 +8,15 @@
88with lib;
99{
1010 options = {
11+ serverName = mkOption {
12+ type = types.nullOr types.str;
13+ default = null;
14+ description = ''
15+ Name of this virtual host. Defaults to attribute name in virtualHosts.
16+ '';
17+ example = "example.org";
18+ };
19+
1120 serverAliases = mkOption {
1221 type = types.listOf types.str;
1322 default = [];