nginx module: Add ACME support for ssl sites
This commit is contained in:
parent
f298be9ef4
commit
4676983990
@ -4,7 +4,13 @@ with lib;
|
|||||||
|
|
||||||
let
|
let
|
||||||
cfg = config.services.nginx;
|
cfg = config.services.nginx;
|
||||||
nginx = cfg.package;
|
virtualHosts = mapAttrs (vhostName: vhostConfig:
|
||||||
|
vhostConfig // (optionalAttrs vhostConfig.enableACME {
|
||||||
|
sslCertificate = "/var/lib/acme/${vhostName}/fullchain.pem";
|
||||||
|
sslCertificateKey = "/var/lib/acme/${vhostName}/key.pem";
|
||||||
|
})
|
||||||
|
) cfg.virtualHosts;
|
||||||
|
|
||||||
configFile = pkgs.writeText "nginx.conf" ''
|
configFile = pkgs.writeText "nginx.conf" ''
|
||||||
user ${cfg.user} ${cfg.group};
|
user ${cfg.user} ${cfg.group};
|
||||||
error_log stderr;
|
error_log stderr;
|
||||||
@ -72,21 +78,23 @@ let
|
|||||||
port = if vhost.port != null then vhost.port else (if ssl then 443 else 80);
|
port = if vhost.port != null then vhost.port else (if ssl then 443 else 80);
|
||||||
listenString = toString port + optionalString ssl " ssl spdy";
|
listenString = toString port + optionalString ssl " ssl spdy";
|
||||||
in ''
|
in ''
|
||||||
${if vhost.forceSSL then ''
|
${optionalString vhost.forceSSL ''
|
||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
listen [::]:80;
|
listen [::]:80;
|
||||||
|
|
||||||
server_name ${serverName} ${concatStringsSep " " vhost.serverAliases};
|
server_name ${serverName} ${concatStringsSep " " vhost.serverAliases};
|
||||||
|
${optionalString vhost.enableACME "location /.well-known/acme-challenge { root ${vhost.acmeRoot}; }"}
|
||||||
return 301 https://$host${optionalString (port != 443) ":${port}"}$request_uri;
|
return 301 https://$host${optionalString (port != 443) ":${port}"}$request_uri;
|
||||||
}
|
}
|
||||||
'' else ""}
|
''}
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen ${listenString};
|
listen ${listenString};
|
||||||
listen [::]:${listenString};
|
listen [::]:${listenString};
|
||||||
|
|
||||||
server_name ${serverName} ${concatStringsSep " " vhost.serverAliases};
|
server_name ${serverName} ${concatStringsSep " " vhost.serverAliases};
|
||||||
|
${optionalString vhost.enableACME "location /.well-known/acme-challenge { root ${vhost.acmeRoot}; }"}
|
||||||
${optionalString (vhost.root != null) "root ${vhost.root};"}
|
${optionalString (vhost.root != null) "root ${vhost.root};"}
|
||||||
${optionalString (vhost.globalRedirect != null) ''
|
${optionalString (vhost.globalRedirect != null) ''
|
||||||
return 301 https://${vhost.globalRedirect}$request_uri;
|
return 301 https://${vhost.globalRedirect}$request_uri;
|
||||||
@ -101,7 +109,7 @@ let
|
|||||||
${vhost.extraConfig}
|
${vhost.extraConfig}
|
||||||
}
|
}
|
||||||
''
|
''
|
||||||
) cfg.virtualHosts);
|
) virtualHosts);
|
||||||
genLocations = locations: concatStringsSep "\n" (mapAttrsToList (location: config: ''
|
genLocations = locations: concatStringsSep "\n" (mapAttrsToList (location: config: ''
|
||||||
location ${location} {
|
location ${location} {
|
||||||
${optionalString (config.proxyPass != null) "proxy_pass ${config.proxyPass};"}
|
${optionalString (config.proxyPass != null) "proxy_pass ${config.proxyPass};"}
|
||||||
@ -202,7 +210,6 @@ in
|
|||||||
description = "Nginx Web Server";
|
description = "Nginx Web Server";
|
||||||
after = [ "network.target" ];
|
after = [ "network.target" ];
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
path = [ nginx ];
|
|
||||||
preStart =
|
preStart =
|
||||||
''
|
''
|
||||||
mkdir -p ${cfg.stateDir}/logs
|
mkdir -p ${cfg.stateDir}/logs
|
||||||
@ -210,7 +217,7 @@ in
|
|||||||
chown -R ${cfg.user}:${cfg.group} ${cfg.stateDir}
|
chown -R ${cfg.user}:${cfg.group} ${cfg.stateDir}
|
||||||
'';
|
'';
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
ExecStart = "${nginx}/bin/nginx -c ${configFile} -p ${cfg.stateDir}";
|
ExecStart = "${cfg.package}/bin/nginx -c ${configFile} -p ${cfg.stateDir}";
|
||||||
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
||||||
Restart = "always";
|
Restart = "always";
|
||||||
RestartSec = "10s";
|
RestartSec = "10s";
|
||||||
@ -218,6 +225,14 @@ in
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
security.acme.certs = mapAttrs (vhostName: vhostConfig: {
|
||||||
|
webroot = vhostConfig.acmeRoot;
|
||||||
|
extraDomains = genAttrs vhostConfig.serverAliases (alias: {
|
||||||
|
"${alias}" = null;
|
||||||
|
});
|
||||||
|
}) virtualHosts;
|
||||||
|
|
||||||
|
|
||||||
users.extraUsers = optionalAttrs (cfg.user == "nginx") (singleton
|
users.extraUsers = optionalAttrs (cfg.user == "nginx") (singleton
|
||||||
{ name = "nginx";
|
{ name = "nginx";
|
||||||
group = cfg.group;
|
group = cfg.group;
|
||||||
|
@ -26,6 +26,18 @@ with lib;
|
|||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enableACME = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
description = "Whether to ask Let's Encrypt to sign a certificate for this vhost.";
|
||||||
|
};
|
||||||
|
|
||||||
|
acmeRoot = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "/var/lib/acme/acme-challenge";
|
||||||
|
description = "Directory to store certificates and keys managed by the ACME service.";
|
||||||
|
};
|
||||||
|
|
||||||
enableSSL = mkOption {
|
enableSSL = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = false;
|
default = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user