Skip to content

Commit

Permalink
nginx: added serverName option for virtualHosts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bobvanderlinden authored and globin committed Jan 25, 2017
1 parent b9b95aa commit d9987f3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
40 changes: 24 additions & 16 deletions nixos/modules/services/web-servers/nginx/default.nix
Expand Up @@ -5,7 +5,11 @@ with lib;
let
cfg = config.services.nginx;
virtualHosts = mapAttrs (vhostName: vhostConfig:
vhostConfig // (optionalAttrs vhostConfig.enableACME {
vhostConfig // {
serverName = if vhostConfig.serverName != null
then vhostConfig.serverName
else vhostName;
} // (optionalAttrs vhostConfig.enableACME {
sslCertificate = "/var/lib/acme/${vhostName}/fullchain.pem";
sslCertificateKey = "/var/lib/acme/${vhostName}/key.pem";
})
Expand Down Expand Up @@ -112,8 +116,9 @@ let
${cfg.appendConfig}
'';

vhosts = concatStringsSep "\n" (mapAttrsToList (serverName: vhost:
vhosts = concatStringsSep "\n" (mapAttrsToList (vhostName: vhost:
let
serverName = vhost.serverName;
ssl = vhost.enableSSL || vhost.forceSSL;
port = if vhost.port != null then vhost.port else (if ssl then 443 else 80);
listenString = toString port + optionalString ssl " ssl http2"
Expand Down Expand Up @@ -161,7 +166,7 @@ let
ssl_certificate_key ${vhost.sslCertificateKey};
''}
${optionalString (vhost.basicAuth != {}) (mkBasicAuth serverName vhost.basicAuth)}
${optionalString (vhost.basicAuth != {}) (mkBasicAuth vhostName vhost.basicAuth)}
${mkLocations vhost.locations}
Expand All @@ -178,8 +183,8 @@ let
${config.extraConfig}
}
'') locations);
mkBasicAuth = serverName: authDef: let
htpasswdFile = pkgs.writeText "${serverName}.htpasswd" (
mkBasicAuth = vhostName: authDef: let
htpasswdFile = pkgs.writeText "${vhostName}.htpasswd" (
concatStringsSep "\n" (mapAttrsToList (user: password: ''
${user}:{PLAIN}${password}
'') authDef)
Expand Down Expand Up @@ -393,17 +398,20 @@ in
};

security.acme.certs = filterAttrs (n: v: v != {}) (
mapAttrs (vhostName: vhostConfig:
optionalAttrs vhostConfig.enableACME {
user = cfg.user;
group = cfg.group;
webroot = vhostConfig.acmeRoot;
extraDomains = genAttrs vhostConfig.serverAliases (alias: null);
postRun = ''
systemctl reload nginx
'';
}
) virtualHosts
let
vhostsConfigs = mapAttrsToList (vhostName: vhostConfig: vhostConfig) virtualHosts;
acmeEnabledVhosts = filter (vhostConfig: vhostConfig.enableACME) vhostsConfigs;
acmePairs = map (vhostConfig: { name = vhostConfig.serverName; value = {
user = cfg.user;
group = cfg.group;
webroot = vhostConfig.acmeRoot;
extraDomains = genAttrs vhostConfig.serverAliases (alias: null);
postRun = ''
systemctl reload nginx
'';
}; }) acmeEnabledVhosts;
in
listToAttrs acmePairs
);

users.extraUsers = optionalAttrs (cfg.user == "nginx") (singleton
Expand Down
9 changes: 9 additions & 0 deletions nixos/modules/services/web-servers/nginx/vhost-options.nix
Expand Up @@ -8,6 +8,15 @@
with lib;
{
options = {
serverName = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Name of this virtual host. Defaults to attribute name in virtualHosts.
'';
example = "example.org";
};

serverAliases = mkOption {
type = types.listOf types.str;
default = [];
Expand Down

0 comments on commit d9987f3

Please sign in to comment.