nixpkgs/nixos/modules/services/web-servers/nginx/default.nix

283 lines
8 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
2013-04-14 09:14:27 +00:00
with lib;
2013-04-14 09:14:27 +00:00
2013-03-02 22:40:56 +00:00
let
cfg = config.services.nginx;
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" ''
user ${cfg.user} ${cfg.group};
2016-01-24 15:50:54 +00:00
error_log stderr;
daemon off;
2013-03-02 22:40:56 +00:00
${cfg.config}
http {
include ${cfg.package}/conf/mime.types;
2016-01-24 15:50:54 +00:00
include ${cfg.package}/conf/fastcgi.conf;
# optimisation
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# use secure TLS defaults
2016-02-01 13:08:45 +00:00
ssl_protocols ${cfg.sslProtocols};
2016-01-24 15:50:54 +00:00
ssl_session_cache shared:SSL:42m;
ssl_session_timeout 23m;
2016-02-01 13:09:13 +00:00
ssl_ciphers ${cfg.sslCiphers};
2016-01-24 15:50:54 +00:00
ssl_ecdh_curve secp521r1;
ssl_prefer_server_ciphers on;
2016-02-01 16:30:43 +00:00
${optionalString (cfg.sslDhparam != null) "ssl_dhparam ${cfg.sslDhparam};"}
2016-01-24 15:50:54 +00:00
ssl_stapling on;
ssl_stapling_verify on;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_comp_level 9;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# sane proxy settings/headers
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 Accept-Encoding "";
proxy_redirect off;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
proxy_buffer_size 8k;
proxy_http_version 1.0;
server_tokens ${if cfg.serverTokens then "on" else "off"};
${vhosts}
${cfg.httpConfig}
}
${cfg.appendConfig}
2013-03-02 22:40:56 +00:00
'';
2016-01-24 15:50:54 +00:00
vhosts = concatStringsSep "\n" (mapAttrsToList (serverName: vhost:
let
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 spdy";
acmeLocation = optionalString vhost.enableACME ''
location /.well-known/acme-challenge {
try_files $uri @acme-fallback;
root ${vhost.acmeRoot};
}
location @acme-fallback {
proxy_pass http://${vhost.acmeFallbackHost};
}
'';
2016-01-24 15:50:54 +00:00
in ''
${optionalString vhost.forceSSL ''
2016-01-24 15:50:54 +00:00
server {
listen 80;
listen [::]:80;
server_name ${serverName} ${concatStringsSep " " vhost.serverAliases};
${acmeLocation}
location / {
return 301 https://$host${optionalString (port != 443) ":${port}"}$request_uri;
}
2016-01-24 15:50:54 +00:00
}
''}
2016-01-24 15:50:54 +00:00
server {
listen ${listenString};
listen [::]:${listenString};
server_name ${serverName} ${concatStringsSep " " vhost.serverAliases};
${acmeLocation}
2016-01-24 15:50:54 +00:00
${optionalString (vhost.root != null) "root ${vhost.root};"}
${optionalString (vhost.globalRedirect != null) ''
return 301 https://${vhost.globalRedirect}$request_uri;
''}
${optionalString ssl ''
ssl_certificate ${vhost.sslCertificate};
ssl_certificate_key ${vhost.sslCertificateKey};
''}
${genLocations vhost.locations}
${vhost.extraConfig}
}
''
) virtualHosts);
2016-01-24 15:50:54 +00:00
genLocations = locations: concatStringsSep "\n" (mapAttrsToList (location: config: ''
location ${location} {
${optionalString (config.proxyPass != null) "proxy_pass ${config.proxyPass};"}
${optionalString (config.root != null) "root ${config.root};"}
${config.extraConfig}
2016-01-24 15:50:54 +00:00
}
'') locations);
2013-03-02 22:40:56 +00:00
in
2013-04-14 09:14:27 +00:00
2013-03-02 22:40:56 +00:00
{
options = {
services.nginx = {
2013-03-02 22:40:56 +00:00
enable = mkOption {
default = false;
2014-12-10 01:40:00 +00:00
type = types.bool;
2013-03-02 22:40:56 +00:00
description = "
Enable the nginx Web Server.
";
};
2013-04-14 09:14:27 +00:00
package = mkOption {
default = pkgs.nginx;
defaultText = "pkgs.nginx";
type = types.package;
description = "
Nginx package to use.
";
};
2013-03-02 22:40:56 +00:00
config = mkOption {
default = "events {}";
2013-03-02 22:40:56 +00:00
description = "
Verbatim nginx.conf configuration.
";
};
appendConfig = mkOption {
type = types.lines;
default = "";
description = ''
Configuration lines appended to the generated Nginx
configuration file. Commonly used by different modules
providing http snippets. <option>appendConfig</option>
can be specified more than once and it's value will be
concatenated (contrary to <option>config</option> which
can be set only once).
'';
};
httpConfig = mkOption {
type = types.lines;
default = "";
description = "Configuration lines to be appended inside of the http {} block.";
};
2013-03-02 22:40:56 +00:00
stateDir = mkOption {
default = "/var/spool/nginx";
description = "
Directory holding all state for nginx to run.
";
};
user = mkOption {
2014-12-10 01:40:00 +00:00
type = types.str;
default = "nginx";
description = "User account under which nginx runs.";
};
group = mkOption {
2014-12-10 01:40:00 +00:00
type = types.str;
default = "nginx";
description = "Group account under which nginx runs.";
};
2016-01-24 15:50:54 +00:00
serverTokens = mkOption {
type = types.bool;
default = false;
description = "Show nginx version in headers and error pages";
};
2016-02-01 13:09:13 +00:00
sslCiphers = mkOption {
type = types.str;
default = "EDH+CHACHA20:EDH+AES:EECDHE+CHACHA20:ECDHE+AES:+AES128:-DSS";
description = "Ciphers to choose from when negotiating tls handshakes.";
};
2016-02-01 13:08:45 +00:00
sslProtocols = mkOption {
type = types.str;
default = "TLSv1.2";
example = "TLSv1 TLSv1.1 TLSv1.2";
description = "Allowed TLS protocol versions.";
};
2016-02-01 16:30:43 +00:00
sslDhparam = mkOption {
type = types.nullOr types.path;
default = null;
example = literalExample "/path/to/dhparams.pem";
description = "Path to DH parameters file.";
};
2016-01-24 15:50:54 +00:00
virtualHosts = mkOption {
type = types.attrsOf (types.submodule (import ./vhost-options.nix {
inherit lib;
}));
default = {
localhost = {};
};
example = [];
description = ''
'';
};
};
2013-03-02 22:40:56 +00:00
};
2013-04-14 09:14:27 +00:00
2013-03-02 22:40:56 +00:00
config = mkIf cfg.enable {
# TODO: test user supplied config file pases syntax test
systemd.services.nginx = {
description = "Nginx Web Server";
2013-03-02 22:40:56 +00:00
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
preStart =
''
mkdir -p ${cfg.stateDir}/logs
chmod 700 ${cfg.stateDir}
chown -R ${cfg.user}:${cfg.group} ${cfg.stateDir}
2013-03-02 22:40:56 +00:00
'';
serviceConfig = {
ExecStart = "${cfg.package}/bin/nginx -c ${configFile} -p ${cfg.stateDir}";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
2016-01-24 15:50:54 +00:00
Restart = "always";
RestartSec = "10s";
StartLimitInterval = "1min";
2013-03-02 22:40:56 +00:00
};
};
2013-04-14 09:14:27 +00:00
security.acme.certs = filterAttrs (n: v: v != {}) (
mapAttrs (vhostName: vhostConfig:
optionalAttrs vhostConfig.enableACME {
webroot = vhostConfig.acmeRoot;
extraDomains = genAttrs vhostConfig.serverAliases (alias: null);
}
) virtualHosts
);
users.extraUsers = optionalAttrs (cfg.user == "nginx") (singleton
{ name = "nginx";
group = cfg.group;
uid = config.ids.uids.nginx;
});
2013-04-14 09:14:27 +00:00
users.extraGroups = optionalAttrs (cfg.group == "nginx") (singleton
{ name = "nginx";
gid = config.ids.gids.nginx;
});
};
}