2020-04-24 23:36:52 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.services.nginx;
|
|
|
|
|
certs = config.security.acme.certs;
|
|
|
|
|
vhostsConfigs = mapAttrsToList (vhostName: vhostConfig: vhostConfig) virtualHosts;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
acmeEnabledVhosts = filter (vhostConfig: vhostConfig.enableACME || vhostConfig.useACMEHost != null) vhostsConfigs;
|
|
|
|
|
dependentCertNames = unique (map (hostOpts: hostOpts.certName) acmeEnabledVhosts);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
virtualHosts = mapAttrs (vhostName: vhostConfig:
|
|
|
|
|
let
|
|
|
|
|
serverName = if vhostConfig.serverName != null
|
|
|
|
|
then vhostConfig.serverName
|
|
|
|
|
else vhostName;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
certName = if vhostConfig.useACMEHost != null
|
|
|
|
|
then vhostConfig.useACMEHost
|
|
|
|
|
else serverName;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
in
|
|
|
|
|
vhostConfig // {
|
2020-09-25 04:45:31 +00:00
|
|
|
|
inherit serverName certName;
|
|
|
|
|
} // (optionalAttrs (vhostConfig.enableACME || vhostConfig.useACMEHost != null) {
|
|
|
|
|
sslCertificate = "${certs.${certName}.directory}/fullchain.pem";
|
|
|
|
|
sslCertificateKey = "${certs.${certName}.directory}/key.pem";
|
2021-08-08 23:34:03 +00:00
|
|
|
|
sslTrustedCertificate = if vhostConfig.sslTrustedCertificate != null
|
|
|
|
|
then vhostConfig.sslTrustedCertificate
|
|
|
|
|
else "${certs.${certName}.directory}/chain.pem";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
})
|
|
|
|
|
) cfg.virtualHosts;
|
|
|
|
|
enableIPv6 = config.networking.enableIPv6;
|
|
|
|
|
|
2021-01-05 17:05:55 +00:00
|
|
|
|
defaultFastcgiParams = {
|
|
|
|
|
SCRIPT_FILENAME = "$document_root$fastcgi_script_name";
|
|
|
|
|
QUERY_STRING = "$query_string";
|
|
|
|
|
REQUEST_METHOD = "$request_method";
|
|
|
|
|
CONTENT_TYPE = "$content_type";
|
|
|
|
|
CONTENT_LENGTH = "$content_length";
|
|
|
|
|
|
|
|
|
|
SCRIPT_NAME = "$fastcgi_script_name";
|
|
|
|
|
REQUEST_URI = "$request_uri";
|
|
|
|
|
DOCUMENT_URI = "$document_uri";
|
|
|
|
|
DOCUMENT_ROOT = "$document_root";
|
|
|
|
|
SERVER_PROTOCOL = "$server_protocol";
|
|
|
|
|
REQUEST_SCHEME = "$scheme";
|
|
|
|
|
HTTPS = "$https if_not_empty";
|
|
|
|
|
|
|
|
|
|
GATEWAY_INTERFACE = "CGI/1.1";
|
|
|
|
|
SERVER_SOFTWARE = "nginx/$nginx_version";
|
|
|
|
|
|
|
|
|
|
REMOTE_ADDR = "$remote_addr";
|
|
|
|
|
REMOTE_PORT = "$remote_port";
|
|
|
|
|
SERVER_ADDR = "$server_addr";
|
|
|
|
|
SERVER_PORT = "$server_port";
|
|
|
|
|
SERVER_NAME = "$server_name";
|
|
|
|
|
|
|
|
|
|
REDIRECT_STATUS = "200";
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
recommendedProxyConfig = pkgs.writeText "nginx-recommended-proxy-headers.conf" ''
|
|
|
|
|
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 X-Forwarded-Host $host;
|
|
|
|
|
proxy_set_header X-Forwarded-Server $host;
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
upstreamConfig = toString (flip mapAttrsToList cfg.upstreams (name: upstream: ''
|
|
|
|
|
upstream ${name} {
|
|
|
|
|
${toString (flip mapAttrsToList upstream.servers (name: server: ''
|
|
|
|
|
server ${name} ${optionalString server.backup "backup"};
|
|
|
|
|
''))}
|
|
|
|
|
${upstream.extraConfig}
|
|
|
|
|
}
|
|
|
|
|
''));
|
|
|
|
|
|
|
|
|
|
commonHttpConfig = ''
|
|
|
|
|
# The mime type definitions included with nginx are very incomplete, so
|
|
|
|
|
# we use a list of mime types from the mailcap package, which is also
|
|
|
|
|
# used by most other Linux distributions by default.
|
|
|
|
|
include ${pkgs.mailcap}/etc/nginx/mime.types;
|
2021-12-06 16:07:01 +00:00
|
|
|
|
# When recommendedOptimisation is disabled nginx fails to start because the mailmap mime.types database
|
|
|
|
|
# contains 1026 enries and the default is only 1024. Setting to a higher number to remove the need to
|
|
|
|
|
# overwrite it because nginx does not allow duplicated settings.
|
|
|
|
|
types_hash_max_size 4096;
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
include ${cfg.package}/conf/fastcgi.conf;
|
|
|
|
|
include ${cfg.package}/conf/uwsgi_params;
|
2021-02-13 14:23:35 +00:00
|
|
|
|
|
|
|
|
|
default_type application/octet-stream;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
configFile = pkgs.writers.writeNginxConfig "nginx.conf" ''
|
|
|
|
|
pid /run/nginx/nginx.pid;
|
|
|
|
|
error_log ${cfg.logError};
|
|
|
|
|
daemon off;
|
|
|
|
|
|
|
|
|
|
${cfg.config}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.eventsConfig != "" || cfg.config == "") ''
|
|
|
|
|
events {
|
|
|
|
|
${cfg.eventsConfig}
|
|
|
|
|
}
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.httpConfig == "" && cfg.config == "") ''
|
|
|
|
|
http {
|
|
|
|
|
${commonHttpConfig}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.resolver.addresses != []) ''
|
|
|
|
|
resolver ${toString cfg.resolver.addresses} ${optionalString (cfg.resolver.valid != "") "valid=${cfg.resolver.valid}"} ${optionalString (!cfg.resolver.ipv6) "ipv6=off"};
|
|
|
|
|
''}
|
|
|
|
|
${upstreamConfig}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.recommendedOptimisation) ''
|
|
|
|
|
# optimisation
|
|
|
|
|
sendfile on;
|
|
|
|
|
tcp_nopush on;
|
|
|
|
|
tcp_nodelay on;
|
|
|
|
|
keepalive_timeout 65;
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
ssl_protocols ${cfg.sslProtocols};
|
2020-10-27 00:29:36 +00:00
|
|
|
|
${optionalString (cfg.sslCiphers != null) "ssl_ciphers ${cfg.sslCiphers};"}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
${optionalString (cfg.sslDhparam != null) "ssl_dhparam ${cfg.sslDhparam};"}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.recommendedTlsSettings) ''
|
|
|
|
|
# Keep in sync with https://ssl-config.mozilla.org/#server=nginx&config=intermediate
|
|
|
|
|
|
|
|
|
|
ssl_session_timeout 1d;
|
|
|
|
|
ssl_session_cache shared:SSL:10m;
|
|
|
|
|
# Breaks forward secrecy: https://github.com/mozilla/server-side-tls/issues/135
|
|
|
|
|
ssl_session_tickets off;
|
|
|
|
|
# We don't enable insecure ciphers by default, so this allows
|
|
|
|
|
# clients to pick the most performant, per https://github.com/mozilla/server-side-tls/issues/260
|
|
|
|
|
ssl_prefer_server_ciphers off;
|
|
|
|
|
|
|
|
|
|
# OCSP stapling
|
|
|
|
|
ssl_stapling on;
|
|
|
|
|
ssl_stapling_verify on;
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.recommendedGzipSettings) ''
|
|
|
|
|
gzip on;
|
|
|
|
|
gzip_proxied any;
|
|
|
|
|
gzip_comp_level 5;
|
|
|
|
|
gzip_types
|
|
|
|
|
application/atom+xml
|
|
|
|
|
application/javascript
|
|
|
|
|
application/json
|
|
|
|
|
application/xml
|
|
|
|
|
application/xml+rss
|
|
|
|
|
image/svg+xml
|
|
|
|
|
text/css
|
|
|
|
|
text/javascript
|
|
|
|
|
text/plain
|
|
|
|
|
text/xml;
|
|
|
|
|
gzip_vary on;
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.recommendedProxySettings) ''
|
|
|
|
|
proxy_redirect off;
|
2021-05-20 23:08:51 +00:00
|
|
|
|
proxy_connect_timeout ${cfg.proxyTimeout};
|
|
|
|
|
proxy_send_timeout ${cfg.proxyTimeout};
|
|
|
|
|
proxy_read_timeout ${cfg.proxyTimeout};
|
2021-04-12 18:23:04 +00:00
|
|
|
|
proxy_http_version 1.1;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
include ${recommendedProxyConfig};
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.mapHashBucketSize != null) ''
|
|
|
|
|
map_hash_bucket_size ${toString cfg.mapHashBucketSize};
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.mapHashMaxSize != null) ''
|
|
|
|
|
map_hash_max_size ${toString cfg.mapHashMaxSize};
|
|
|
|
|
''}
|
|
|
|
|
|
2021-08-18 13:19:15 +00:00
|
|
|
|
${optionalString (cfg.serverNamesHashBucketSize != null) ''
|
|
|
|
|
server_names_hash_bucket_size ${toString cfg.serverNamesHashBucketSize};
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.serverNamesHashMaxSize != null) ''
|
|
|
|
|
server_names_hash_max_size ${toString cfg.serverNamesHashMaxSize};
|
|
|
|
|
''}
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
# $connection_upgrade is used for websocket proxying
|
|
|
|
|
map $http_upgrade $connection_upgrade {
|
|
|
|
|
default upgrade;
|
|
|
|
|
''' close;
|
|
|
|
|
}
|
|
|
|
|
client_max_body_size ${cfg.clientMaxBodySize};
|
|
|
|
|
|
|
|
|
|
server_tokens ${if cfg.serverTokens then "on" else "off"};
|
|
|
|
|
|
2022-12-02 08:20:57 +00:00
|
|
|
|
${optionalString (cfg.proxyCache.enable) ''
|
|
|
|
|
proxy_cache_path /var/cache/nginx keys_zone=${cfg.proxyCache.keysZoneName}:${cfg.proxyCache.keysZoneSize}
|
|
|
|
|
levels=${cfg.proxyCache.levels}
|
|
|
|
|
use_temp_path=${if cfg.proxyCache.useTempPath then "on" else "off"}
|
|
|
|
|
inactive=${cfg.proxyCache.inactive}
|
|
|
|
|
max_size=${cfg.proxyCache.maxSize};
|
|
|
|
|
''}
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
${cfg.commonHttpConfig}
|
|
|
|
|
|
|
|
|
|
${vhosts}
|
|
|
|
|
|
|
|
|
|
${optionalString cfg.statusPage ''
|
|
|
|
|
server {
|
2022-11-21 17:40:18 +00:00
|
|
|
|
listen ${toString cfg.defaultHTTPListenPort};
|
|
|
|
|
${optionalString enableIPv6 "listen [::]:${toString cfg.defaultHTTPListenPort};" }
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
server_name localhost;
|
|
|
|
|
|
|
|
|
|
location /nginx_status {
|
|
|
|
|
stub_status on;
|
|
|
|
|
access_log off;
|
|
|
|
|
allow 127.0.0.1;
|
|
|
|
|
${optionalString enableIPv6 "allow ::1;"}
|
|
|
|
|
deny all;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
${cfg.appendHttpConfig}
|
|
|
|
|
}''}
|
|
|
|
|
|
|
|
|
|
${optionalString (cfg.httpConfig != "") ''
|
|
|
|
|
http {
|
|
|
|
|
${commonHttpConfig}
|
|
|
|
|
${cfg.httpConfig}
|
|
|
|
|
}''}
|
|
|
|
|
|
2021-01-09 10:05:03 +00:00
|
|
|
|
${optionalString (cfg.streamConfig != "") ''
|
|
|
|
|
stream {
|
|
|
|
|
${cfg.streamConfig}
|
|
|
|
|
}
|
|
|
|
|
''}
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
${cfg.appendConfig}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
configPath = if cfg.enableReload
|
|
|
|
|
then "/etc/nginx/nginx.conf"
|
|
|
|
|
else configFile;
|
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
execCommand = "${cfg.package}/bin/nginx -c '${configPath}'";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
vhosts = concatStringsSep "\n" (mapAttrsToList (vhostName: vhost:
|
|
|
|
|
let
|
|
|
|
|
onlySSL = vhost.onlySSL || vhost.enableSSL;
|
|
|
|
|
hasSSL = onlySSL || vhost.addSSL || vhost.forceSSL;
|
|
|
|
|
|
|
|
|
|
defaultListen =
|
|
|
|
|
if vhost.listen != [] then vhost.listen
|
2021-08-12 14:41:47 +00:00
|
|
|
|
else
|
2022-01-25 03:21:06 +00:00
|
|
|
|
let addrs = if vhost.listenAddresses != [] then vhost.listenAddresses else cfg.defaultListenAddresses;
|
2022-11-21 17:40:18 +00:00
|
|
|
|
in optionals (hasSSL || vhost.rejectSSL) (map (addr: { inherit addr; port = cfg.defaultSSLListenPort; ssl = true; }) addrs)
|
|
|
|
|
++ optionals (!onlySSL) (map (addr: { inherit addr; port = cfg.defaultHTTPListenPort; ssl = false; }) addrs);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
hostListen =
|
|
|
|
|
if vhost.forceSSL
|
|
|
|
|
then filter (x: x.ssl) defaultListen
|
|
|
|
|
else defaultListen;
|
|
|
|
|
|
|
|
|
|
listenString = { addr, port, ssl, extraParameters ? [], ... }:
|
2022-04-15 01:41:22 +00:00
|
|
|
|
(if ssl && vhost.http3 then "
|
|
|
|
|
# UDP listener for **QUIC+HTTP/3
|
|
|
|
|
listen ${addr}:${toString port} http3 "
|
|
|
|
|
+ optionalString vhost.default "default_server "
|
|
|
|
|
+ optionalString vhost.reuseport "reuseport "
|
|
|
|
|
+ optionalString (extraParameters != []) (concatStringsSep " " extraParameters)
|
|
|
|
|
+ ";" else "")
|
|
|
|
|
+ "
|
|
|
|
|
|
|
|
|
|
listen ${addr}:${toString port} "
|
2020-04-24 23:36:52 +00:00
|
|
|
|
+ optionalString (ssl && vhost.http2) "http2 "
|
2022-04-15 01:41:22 +00:00
|
|
|
|
+ optionalString ssl "ssl "
|
2020-04-24 23:36:52 +00:00
|
|
|
|
+ optionalString vhost.default "default_server "
|
2022-04-15 01:41:22 +00:00
|
|
|
|
+ optionalString vhost.reuseport "reuseport "
|
2020-04-24 23:36:52 +00:00
|
|
|
|
+ optionalString (extraParameters != []) (concatStringsSep " " extraParameters)
|
2022-04-15 01:41:22 +00:00
|
|
|
|
+ ";";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
redirectListen = filter (x: !x.ssl) defaultListen;
|
|
|
|
|
|
|
|
|
|
acmeLocation = optionalString (vhost.enableACME || vhost.useACMEHost != null) ''
|
2022-10-30 15:09:59 +00:00
|
|
|
|
# Rule for legitimate ACME Challenge requests (like /.well-known/acme-challenge/xxxxxxxxx)
|
|
|
|
|
# We use ^~ here, so that we don't check any regexes (which could
|
|
|
|
|
# otherwise easily override this intended match accidentally).
|
|
|
|
|
location ^~ /.well-known/acme-challenge/ {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
${optionalString (vhost.acmeFallbackHost != null) "try_files $uri @acme-fallback;"}
|
2021-12-30 13:39:12 +00:00
|
|
|
|
${optionalString (vhost.acmeRoot != null) "root ${vhost.acmeRoot};"}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
auth_basic off;
|
|
|
|
|
}
|
|
|
|
|
${optionalString (vhost.acmeFallbackHost != null) ''
|
|
|
|
|
location @acme-fallback {
|
|
|
|
|
auth_basic off;
|
|
|
|
|
proxy_pass http://${vhost.acmeFallbackHost};
|
|
|
|
|
}
|
|
|
|
|
''}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
in ''
|
|
|
|
|
${optionalString vhost.forceSSL ''
|
|
|
|
|
server {
|
|
|
|
|
${concatMapStringsSep "\n" listenString redirectListen}
|
|
|
|
|
|
|
|
|
|
server_name ${vhost.serverName} ${concatStringsSep " " vhost.serverAliases};
|
|
|
|
|
${acmeLocation}
|
|
|
|
|
location / {
|
|
|
|
|
return 301 https://$host$request_uri;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
''}
|
|
|
|
|
|
|
|
|
|
server {
|
|
|
|
|
${concatMapStringsSep "\n" listenString hostListen}
|
|
|
|
|
server_name ${vhost.serverName} ${concatStringsSep " " vhost.serverAliases};
|
|
|
|
|
${acmeLocation}
|
|
|
|
|
${optionalString (vhost.root != null) "root ${vhost.root};"}
|
|
|
|
|
${optionalString (vhost.globalRedirect != null) ''
|
|
|
|
|
return 301 http${optionalString hasSSL "s"}://${vhost.globalRedirect}$request_uri;
|
|
|
|
|
''}
|
|
|
|
|
${optionalString hasSSL ''
|
|
|
|
|
ssl_certificate ${vhost.sslCertificate};
|
|
|
|
|
ssl_certificate_key ${vhost.sslCertificateKey};
|
|
|
|
|
''}
|
|
|
|
|
${optionalString (hasSSL && vhost.sslTrustedCertificate != null) ''
|
|
|
|
|
ssl_trusted_certificate ${vhost.sslTrustedCertificate};
|
|
|
|
|
''}
|
2022-01-03 16:56:52 +00:00
|
|
|
|
${optionalString vhost.rejectSSL ''
|
2021-05-28 09:39:13 +00:00
|
|
|
|
ssl_reject_handshake on;
|
|
|
|
|
''}
|
2021-12-25 05:07:40 +00:00
|
|
|
|
${optionalString (hasSSL && vhost.kTLS) ''
|
|
|
|
|
ssl_conf_command Options KTLS;
|
|
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
|
${optionalString (hasSSL && vhost.http3) ''
|
|
|
|
|
# Advertise that HTTP/3 is available
|
|
|
|
|
add_header Alt-Svc 'h3=":443"; ma=86400' always;
|
|
|
|
|
''}
|
|
|
|
|
|
2020-11-03 02:18:15 +00:00
|
|
|
|
${mkBasicAuth vhostName vhost}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
${mkLocations vhost.locations}
|
|
|
|
|
|
|
|
|
|
${vhost.extraConfig}
|
|
|
|
|
}
|
|
|
|
|
''
|
|
|
|
|
) virtualHosts);
|
|
|
|
|
mkLocations = locations: concatStringsSep "\n" (map (config: ''
|
|
|
|
|
location ${config.location} {
|
|
|
|
|
${optionalString (config.proxyPass != null && !cfg.proxyResolveWhileRunning)
|
|
|
|
|
"proxy_pass ${config.proxyPass};"
|
|
|
|
|
}
|
|
|
|
|
${optionalString (config.proxyPass != null && cfg.proxyResolveWhileRunning) ''
|
|
|
|
|
set $nix_proxy_target "${config.proxyPass}";
|
|
|
|
|
proxy_pass $nix_proxy_target;
|
|
|
|
|
''}
|
|
|
|
|
${optionalString config.proxyWebsockets ''
|
|
|
|
|
proxy_http_version 1.1;
|
|
|
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
|
|
|
proxy_set_header Connection $connection_upgrade;
|
|
|
|
|
''}
|
2021-01-05 17:05:55 +00:00
|
|
|
|
${concatStringsSep "\n"
|
|
|
|
|
(mapAttrsToList (n: v: ''fastcgi_param ${n} "${v}";'')
|
|
|
|
|
(optionalAttrs (config.fastcgiParams != {})
|
|
|
|
|
(defaultFastcgiParams // config.fastcgiParams)))}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
${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};"}
|
|
|
|
|
${optionalString (config.return != null) "return ${config.return};"}
|
|
|
|
|
${config.extraConfig}
|
2022-06-26 10:26:21 +00:00
|
|
|
|
${optionalString (config.proxyPass != null && config.recommendedProxySettings) "include ${recommendedProxyConfig};"}
|
2020-11-03 02:18:15 +00:00
|
|
|
|
${mkBasicAuth "sublocation" config}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
}
|
|
|
|
|
'') (sortProperties (mapAttrsToList (k: v: v // { location = k; }) locations)));
|
2020-11-03 02:18:15 +00:00
|
|
|
|
|
|
|
|
|
mkBasicAuth = name: zone: optionalString (zone.basicAuthFile != null || zone.basicAuth != {}) (let
|
|
|
|
|
auth_file = if zone.basicAuthFile != null
|
|
|
|
|
then zone.basicAuthFile
|
|
|
|
|
else mkHtpasswd name zone.basicAuth;
|
|
|
|
|
in ''
|
|
|
|
|
auth_basic secured;
|
|
|
|
|
auth_basic_user_file ${auth_file};
|
|
|
|
|
'');
|
|
|
|
|
mkHtpasswd = name: authDef: pkgs.writeText "${name}.htpasswd" (
|
2020-04-24 23:36:52 +00:00
|
|
|
|
concatStringsSep "\n" (mapAttrsToList (user: password: ''
|
|
|
|
|
${user}:{PLAIN}${password}
|
|
|
|
|
'') authDef)
|
|
|
|
|
);
|
2022-01-13 20:06:32 +00:00
|
|
|
|
|
|
|
|
|
mkCertOwnershipAssertion = import ../../../security/acme/mk-cert-ownership-assertion.nix;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
options = {
|
|
|
|
|
services.nginx = {
|
2022-09-09 14:08:57 +00:00
|
|
|
|
enable = mkEnableOption (lib.mdDoc "Nginx Web Server");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
statusPage = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Enable status page reachable from localhost on http://127.0.0.1/nginx_status.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
recommendedTlsSettings = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Enable recommended TLS settings.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
recommendedOptimisation = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Enable recommended optimisation settings.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
recommendedGzipSettings = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Enable recommended gzip settings.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
recommendedProxySettings = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-06-26 10:26:21 +00:00
|
|
|
|
Whether to enable recommended proxy settings if a vhost does not specify the option manually.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
|
proxyTimeout = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "60s";
|
|
|
|
|
example = "20s";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2021-05-20 23:08:51 +00:00
|
|
|
|
Change the proxy related timeouts in recommendedProxySettings.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2021-05-20 23:08:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-25 03:21:06 +00:00
|
|
|
|
defaultListenAddresses = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [ "0.0.0.0" ] ++ optional enableIPv6 "[::0]";
|
|
|
|
|
defaultText = literalExpression ''[ "0.0.0.0" ] ++ lib.optional config.networking.enableIPv6 "[::0]"'';
|
|
|
|
|
example = literalExpression ''[ "10.0.0.12" "[2002:a00:1::]" ]'';
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2022-01-25 03:21:06 +00:00
|
|
|
|
If vhosts do not specify listenAddresses, use these addresses by default.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2022-01-25 03:21:06 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-11-21 17:40:18 +00:00
|
|
|
|
defaultHTTPListenPort = mkOption {
|
|
|
|
|
type = types.port;
|
|
|
|
|
default = 80;
|
|
|
|
|
example = 8080;
|
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
If vhosts do not specify listen.port, use these ports for HTTP by default.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
defaultSSLListenPort = mkOption {
|
|
|
|
|
type = types.port;
|
|
|
|
|
default = 443;
|
|
|
|
|
example = 8443;
|
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
If vhosts do not specify listen.port, use these ports for SSL by default.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
package = mkOption {
|
|
|
|
|
default = pkgs.nginxStable;
|
2021-10-06 13:57:05 +00:00
|
|
|
|
defaultText = literalExpression "pkgs.nginxStable";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
type = types.package;
|
2021-04-08 16:26:57 +00:00
|
|
|
|
apply = p: p.override {
|
|
|
|
|
modules = p.modules ++ cfg.additionalModules;
|
|
|
|
|
};
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Nginx package to use. This defaults to the stable version. Note
|
|
|
|
|
that the nginx team recommends to use the mainline version which
|
2022-08-21 13:32:41 +00:00
|
|
|
|
available in nixpkgs as `nginxMainline`.
|
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-04-08 16:26:57 +00:00
|
|
|
|
additionalModules = mkOption {
|
|
|
|
|
default = [];
|
|
|
|
|
type = types.listOf (types.attrsOf types.anything);
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression "[ pkgs.nginxModules.brotli ]";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Additional [third-party nginx modules](https://www.nginx.com/resources/wiki/modules/)
|
2021-04-08 16:26:57 +00:00
|
|
|
|
to install. Packaged modules are available in
|
2022-08-21 13:32:41 +00:00
|
|
|
|
`pkgs.nginxModules`.
|
2021-04-08 16:26:57 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
logError = mkOption {
|
|
|
|
|
default = "stderr";
|
2021-02-05 17:12:51 +00:00
|
|
|
|
type = types.str;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Configures logging.
|
|
|
|
|
The first parameter defines a file that will store the log. The
|
|
|
|
|
special value stderr selects the standard error file. Logging to
|
|
|
|
|
syslog can be configured by specifying the “syslog:” prefix.
|
|
|
|
|
The second parameter determines the level of logging, and can be
|
|
|
|
|
one of the following: debug, info, notice, warn, error, crit,
|
|
|
|
|
alert, or emerg. Log levels above are listed in the order of
|
|
|
|
|
increasing severity. Setting a certain log level will cause all
|
|
|
|
|
messages of the specified and more severe log levels to be logged.
|
|
|
|
|
If this parameter is omitted then error is used.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
preStart = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Shell commands executed before the service's nginx is started.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config = mkOption {
|
2020-12-25 13:55:36 +00:00
|
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
default = "";
|
2022-09-09 14:08:57 +00:00
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Verbatim {file}`nginx.conf` configuration.
|
2020-12-25 13:55:36 +00:00
|
|
|
|
This is mutually exclusive to any other config option for
|
2022-09-09 14:08:57 +00:00
|
|
|
|
{file}`nginx.conf` except for
|
|
|
|
|
- [](#opt-services.nginx.appendConfig)
|
|
|
|
|
- [](#opt-services.nginx.httpConfig)
|
|
|
|
|
- [](#opt-services.nginx.logError)
|
2020-12-25 13:55:36 +00:00
|
|
|
|
|
|
|
|
|
If additional verbatim config in addition to other options is needed,
|
2022-09-09 14:08:57 +00:00
|
|
|
|
[](#opt-services.nginx.appendConfig) should be used instead.
|
2020-12-25 13:55:36 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
appendConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Configuration lines appended to the generated Nginx
|
|
|
|
|
configuration file. Commonly used by different modules
|
2022-08-21 13:32:41 +00:00
|
|
|
|
providing http snippets. {option}`appendConfig`
|
2020-04-24 23:36:52 +00:00
|
|
|
|
can be specified more than once and it's value will be
|
2022-08-21 13:32:41 +00:00
|
|
|
|
concatenated (contrary to {option}`config` which
|
2020-04-24 23:36:52 +00:00
|
|
|
|
can be set only once).
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
commonHttpConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
|
|
|
|
example = ''
|
|
|
|
|
resolver 127.0.0.1 valid=5s;
|
|
|
|
|
|
|
|
|
|
log_format myformat '$remote_addr - $remote_user [$time_local] '
|
|
|
|
|
'"$request" $status $body_bytes_sent '
|
|
|
|
|
'"$http_referer" "$http_user_agent"';
|
|
|
|
|
'';
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
With nginx you must provide common http context definitions before
|
|
|
|
|
they are used, e.g. log_format, resolver, etc. inside of server
|
|
|
|
|
or location contexts. Use this attribute to set these definitions
|
|
|
|
|
at the appropriate location.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
httpConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Configuration lines to be set inside the http block.
|
|
|
|
|
This is mutually exclusive with the structured configuration
|
|
|
|
|
via virtualHosts and the recommendedXyzSettings configuration
|
|
|
|
|
options. See appendHttpConfig for appending to the generated http block.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-01-09 10:05:03 +00:00
|
|
|
|
streamConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
|
|
|
|
example = ''
|
|
|
|
|
server {
|
|
|
|
|
listen 127.0.0.1:53 udp reuseport;
|
|
|
|
|
proxy_timeout 20s;
|
|
|
|
|
proxy_pass 192.168.0.1:53535;
|
|
|
|
|
}
|
|
|
|
|
'';
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2021-01-09 10:05:03 +00:00
|
|
|
|
Configuration lines to be set inside the stream block.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2021-01-09 10:05:03 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
eventsConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Configuration lines to be set inside the events block.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
appendHttpConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Configuration lines to be appended to the generated http block.
|
|
|
|
|
This is mutually exclusive with using config and httpConfig for
|
|
|
|
|
specifying the whole http block verbatim.
|
2022-08-21 13:32:41 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enableReload = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Reload nginx when configuration file changes (instead of restart).
|
2022-08-21 13:32:41 +00:00
|
|
|
|
The configuration file is exposed at {file}`/etc/nginx/nginx.conf`.
|
|
|
|
|
See also `systemd.services.*.restartIfChanged`.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "nginx";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "User account under which nginx runs.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "nginx";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "Group account under which nginx runs.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
serverTokens = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "Show nginx version in headers and error pages.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clientMaxBodySize = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "10m";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "Set nginx global client_max_body_size.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sslCiphers = mkOption {
|
2020-10-27 00:29:36 +00:00
|
|
|
|
type = types.nullOr types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
# Keep in sync with https://ssl-config.mozilla.org/#server=nginx&config=intermediate
|
|
|
|
|
default = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "Ciphers to choose from when negotiating TLS handshakes.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sslProtocols = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "TLSv1.2 TLSv1.3";
|
|
|
|
|
example = "TLSv1 TLSv1.1 TLSv1.2 TLSv1.3";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "Allowed TLS protocol versions.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sslDhparam = mkOption {
|
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
|
default = null;
|
|
|
|
|
example = "/path/to/dhparams.pem";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "Path to DH parameters file.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proxyResolveWhileRunning = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Resolves domains of proxyPass targets at runtime
|
|
|
|
|
and not only at start, you have to set
|
|
|
|
|
services.nginx.resolver, too.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mapHashBucketSize = mkOption {
|
|
|
|
|
type = types.nullOr (types.enum [ 32 64 128 ]);
|
|
|
|
|
default = null;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Sets the bucket size for the map variables hash tables. Default
|
|
|
|
|
value depends on the processor’s cache line size.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mapHashMaxSize = mkOption {
|
|
|
|
|
type = types.nullOr types.ints.positive;
|
|
|
|
|
default = null;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Sets the maximum size of the map variables hash tables.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-18 13:19:15 +00:00
|
|
|
|
serverNamesHashBucketSize = mkOption {
|
|
|
|
|
type = types.nullOr types.ints.positive;
|
|
|
|
|
default = null;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2021-08-18 13:19:15 +00:00
|
|
|
|
Sets the bucket size for the server names hash tables. Default
|
|
|
|
|
value depends on the processor’s cache line size.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
serverNamesHashMaxSize = mkOption {
|
|
|
|
|
type = types.nullOr types.ints.positive;
|
|
|
|
|
default = null;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2021-08-18 13:19:15 +00:00
|
|
|
|
Sets the maximum size of the server names hash tables.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-02 08:20:57 +00:00
|
|
|
|
proxyCache = mkOption {
|
|
|
|
|
type = types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
enable = mkEnableOption (lib.mdDoc "Enable proxy cache");
|
|
|
|
|
|
|
|
|
|
keysZoneName = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "cache";
|
|
|
|
|
example = "my_cache";
|
|
|
|
|
description = lib.mdDoc "Set name to shared memory zone.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
keysZoneSize = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "10m";
|
|
|
|
|
example = "32m";
|
|
|
|
|
description = lib.mdDoc "Set size to shared memory zone.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
levels = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "1:2";
|
|
|
|
|
example = "1:2:2";
|
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
The levels parameter defines structure of subdirectories in cache: from
|
|
|
|
|
1 to 3, each level accepts values 1 or 2. Сan be used any combination of
|
|
|
|
|
1 and 2 in these formats: x, x:x and x:x:x.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useTempPath = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
|
|
|
|
example = true;
|
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Nginx first writes files that are destined for the cache to a temporary
|
|
|
|
|
storage area, and the use_temp_path=off directive instructs Nginx to
|
|
|
|
|
write them to the same directories where they will be cached. Recommended
|
|
|
|
|
that you set this parameter to off to avoid unnecessary copying of data
|
|
|
|
|
between file systems.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inactive = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "10m";
|
|
|
|
|
example = "1d";
|
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
|
Cached data that has not been accessed for the time specified by
|
|
|
|
|
the inactive parameter is removed from the cache, regardless of
|
|
|
|
|
its freshness.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
maxSize = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "1g";
|
|
|
|
|
example = "2048m";
|
|
|
|
|
description = lib.mdDoc "Set maximum cache size";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
default = {};
|
|
|
|
|
description = lib.mdDoc "Configure proxy cache";
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
resolver = mkOption {
|
|
|
|
|
type = types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
addresses = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression ''[ "[::1]" "127.0.0.1:5353" ]'';
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "List of resolvers to use";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
valid = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = "";
|
|
|
|
|
example = "30s";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
By default, nginx caches answers using the TTL value of a response.
|
|
|
|
|
An optional valid parameter allows overriding it
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
ipv6 = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = true;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
By default, nginx will look up both IPv4 and IPv6 addresses while resolving.
|
|
|
|
|
If looking up of IPv6 addresses is not desired, the ipv6=off parameter can be
|
|
|
|
|
specified.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Configures name servers used to resolve names of upstream servers into addresses
|
|
|
|
|
'';
|
|
|
|
|
default = {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
upstreams = mkOption {
|
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
servers = mkOption {
|
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
backup = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Marks the server as a backup server. It will be passed
|
|
|
|
|
requests when the primary servers are unavailable.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Defines the address and other parameters of the upstream servers.
|
|
|
|
|
'';
|
|
|
|
|
default = {};
|
2021-04-17 00:35:05 +00:00
|
|
|
|
example = { "127.0.0.1:8000" = {}; };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
These lines go to the end of the upstream verbatim.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Defines a group of servers to use as proxy target.
|
|
|
|
|
'';
|
|
|
|
|
default = {};
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression ''
|
2021-04-17 00:35:05 +00:00
|
|
|
|
"backend_server" = {
|
|
|
|
|
servers = { "127.0.0.1:8000" = {}; };
|
|
|
|
|
extraConfig = ''''
|
|
|
|
|
keepalive 16;
|
|
|
|
|
'''';
|
|
|
|
|
};
|
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtualHosts = mkOption {
|
|
|
|
|
type = types.attrsOf (types.submodule (import ./vhost-options.nix {
|
|
|
|
|
inherit config lib;
|
|
|
|
|
}));
|
|
|
|
|
default = {
|
|
|
|
|
localhost = {};
|
|
|
|
|
};
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
{
|
|
|
|
|
"hydra.example.com" = {
|
|
|
|
|
forceSSL = true;
|
|
|
|
|
enableACME = true;
|
|
|
|
|
locations."/" = {
|
|
|
|
|
proxyPass = "http://localhost:3000";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
'';
|
2022-08-21 13:32:41 +00:00
|
|
|
|
description = lib.mdDoc "Declarative vhost config";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
|
imports = [
|
|
|
|
|
(mkRemovedOptionModule [ "services" "nginx" "stateDir" ] ''
|
|
|
|
|
The Nginx log directory has been moved to /var/log/nginx, the cache directory
|
|
|
|
|
to /var/cache/nginx. The option services.nginx.stateDir has been removed.
|
|
|
|
|
'')
|
|
|
|
|
];
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
# TODO: test user supplied config file pases syntax test
|
|
|
|
|
|
|
|
|
|
warnings =
|
|
|
|
|
let
|
|
|
|
|
deprecatedSSL = name: config: optional config.enableSSL
|
|
|
|
|
''
|
|
|
|
|
config.services.nginx.virtualHosts.<name>.enableSSL is deprecated,
|
|
|
|
|
use config.services.nginx.virtualHosts.<name>.onlySSL instead.
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
in flatten (mapAttrsToList deprecatedSSL virtualHosts);
|
|
|
|
|
|
|
|
|
|
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.";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2021-05-28 09:39:13 +00:00
|
|
|
|
assertion = all (host: with host;
|
|
|
|
|
count id [ addSSL (onlySSL || enableSSL) forceSSL rejectSSL ] <= 1
|
2020-04-24 23:36:52 +00:00
|
|
|
|
) (attrValues virtualHosts);
|
|
|
|
|
message = ''
|
|
|
|
|
Options services.nginx.service.virtualHosts.<name>.addSSL,
|
2021-05-28 09:39:13 +00:00
|
|
|
|
services.nginx.virtualHosts.<name>.onlySSL,
|
|
|
|
|
services.nginx.virtualHosts.<name>.forceSSL and
|
|
|
|
|
services.nginx.virtualHosts.<name>.rejectSSL are mutually exclusive.
|
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
assertion = any (host: host.rejectSSL) (attrValues virtualHosts) -> versionAtLeast cfg.package.version "1.19.4";
|
|
|
|
|
message = ''
|
|
|
|
|
services.nginx.virtualHosts.<name>.rejectSSL requires nginx version
|
|
|
|
|
1.19.4 or above; see the documentation for services.nginx.package.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 05:07:40 +00:00
|
|
|
|
{
|
|
|
|
|
assertion = any (host: host.kTLS) (attrValues virtualHosts) -> versionAtLeast cfg.package.version "1.21.4";
|
|
|
|
|
message = ''
|
|
|
|
|
services.nginx.virtualHosts.<name>.kTLS requires nginx version
|
|
|
|
|
1.21.4 or above; see the documentation for services.nginx.package.
|
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
{
|
2021-05-28 09:39:13 +00:00
|
|
|
|
assertion = all (host: !(host.enableACME && host.useACMEHost != null)) (attrValues virtualHosts);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
message = ''
|
|
|
|
|
Options services.nginx.service.virtualHosts.<name>.enableACME and
|
|
|
|
|
services.nginx.virtualHosts.<name>.useACMEHost are mutually exclusive.
|
|
|
|
|
'';
|
|
|
|
|
}
|
2022-01-13 20:06:32 +00:00
|
|
|
|
] ++ map (name: mkCertOwnershipAssertion {
|
|
|
|
|
inherit (cfg) group user;
|
|
|
|
|
cert = config.security.acme.certs.${name};
|
|
|
|
|
groups = config.users.groups;
|
|
|
|
|
}) dependentCertNames;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
systemd.services.nginx = {
|
|
|
|
|
description = "Nginx Web Server";
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2020-09-25 04:45:31 +00:00
|
|
|
|
wants = concatLists (map (certName: [ "acme-finished-${certName}.target" ]) dependentCertNames);
|
|
|
|
|
after = [ "network.target" ] ++ map (certName: "acme-selfsigned-${certName}.service") dependentCertNames;
|
2020-07-18 16:06:22 +00:00
|
|
|
|
# Nginx needs to be started in order to be able to request certificates
|
|
|
|
|
# (it's hosting the acme challenge after all)
|
|
|
|
|
# This fixes https://github.com/NixOS/nixpkgs/issues/81842
|
2020-09-25 04:45:31 +00:00
|
|
|
|
before = map (certName: "acme-${certName}.service") dependentCertNames;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
stopIfChanged = false;
|
|
|
|
|
preStart = ''
|
|
|
|
|
${cfg.preStart}
|
|
|
|
|
${execCommand} -t
|
|
|
|
|
'';
|
2020-11-03 02:18:15 +00:00
|
|
|
|
|
|
|
|
|
startLimitIntervalSec = 60;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
serviceConfig = {
|
|
|
|
|
ExecStart = execCommand;
|
2020-08-20 17:08:02 +00:00
|
|
|
|
ExecReload = [
|
|
|
|
|
"${execCommand} -t"
|
|
|
|
|
"${pkgs.coreutils}/bin/kill -HUP $MAINPID"
|
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Restart = "always";
|
|
|
|
|
RestartSec = "10s";
|
|
|
|
|
# User and group
|
|
|
|
|
User = cfg.user;
|
|
|
|
|
Group = cfg.group;
|
|
|
|
|
# Runtime directory and mode
|
|
|
|
|
RuntimeDirectory = "nginx";
|
|
|
|
|
RuntimeDirectoryMode = "0750";
|
2020-05-15 21:57:56 +00:00
|
|
|
|
# Cache directory and mode
|
|
|
|
|
CacheDirectory = "nginx";
|
|
|
|
|
CacheDirectoryMode = "0750";
|
|
|
|
|
# Logs directory and mode
|
|
|
|
|
LogsDirectory = "nginx";
|
|
|
|
|
LogsDirectoryMode = "0750";
|
2021-05-03 20:48:10 +00:00
|
|
|
|
# Proc filesystem
|
|
|
|
|
ProcSubset = "pid";
|
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
|
# New file permissions
|
|
|
|
|
UMask = "0027"; # 0640 / 0750
|
2020-04-24 23:36:52 +00:00
|
|
|
|
# Capabilities
|
|
|
|
|
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" "CAP_SYS_RESOURCE" ];
|
2020-05-15 21:57:56 +00:00
|
|
|
|
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" "CAP_SYS_RESOURCE" ];
|
|
|
|
|
# Security
|
|
|
|
|
NoNewPrivileges = true;
|
2021-05-03 20:48:10 +00:00
|
|
|
|
# Sandboxing (sorted by occurrence in https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
|
2020-05-15 21:57:56 +00:00
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
|
ProtectHome = mkDefault true;
|
|
|
|
|
PrivateTmp = true;
|
|
|
|
|
PrivateDevices = true;
|
|
|
|
|
ProtectHostname = true;
|
2021-05-03 20:48:10 +00:00
|
|
|
|
ProtectClock = true;
|
2020-05-15 21:57:56 +00:00
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
|
ProtectKernelModules = true;
|
2021-05-03 20:48:10 +00:00
|
|
|
|
ProtectKernelLogs = true;
|
2020-05-15 21:57:56 +00:00
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
|
2021-05-03 20:48:10 +00:00
|
|
|
|
RestrictNamespaces = true;
|
2020-05-15 21:57:56 +00:00
|
|
|
|
LockPersonality = true;
|
2021-10-28 06:52:43 +00:00
|
|
|
|
MemoryDenyWriteExecute = !((builtins.any (mod: (mod.allowMemoryWriteExecute or false)) cfg.package.modules) || (cfg.package == pkgs.openresty));
|
2020-05-15 21:57:56 +00:00
|
|
|
|
RestrictRealtime = true;
|
|
|
|
|
RestrictSUIDSGID = true;
|
2021-05-03 20:48:10 +00:00
|
|
|
|
RemoveIPC = true;
|
2020-05-15 21:57:56 +00:00
|
|
|
|
PrivateMounts = true;
|
|
|
|
|
# System Call Filtering
|
|
|
|
|
SystemCallArchitectures = "native";
|
2022-03-10 19:12:11 +00:00
|
|
|
|
SystemCallFilter = [ "~@cpu-emulation @debug @keyring @mount @obsolete @privileged @setuid" ]
|
2022-06-16 17:23:12 +00:00
|
|
|
|
++ optionals ((cfg.package != pkgs.tengine) && (cfg.package != pkgs.openresty) && (!lib.any (mod: (mod.disableIPC or false)) cfg.package.modules)) [ "~@ipc" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
environment.etc."nginx/nginx.conf" = mkIf cfg.enableReload {
|
|
|
|
|
source = configFile;
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
|
# This service waits for all certificates to be available
|
|
|
|
|
# before reloading nginx configuration.
|
|
|
|
|
# sslTargets are added to wantedBy + before
|
2020-09-25 04:45:31 +00:00
|
|
|
|
# which allows the acme-finished-$cert.target to signify the successful updating
|
|
|
|
|
# of certs end-to-end.
|
|
|
|
|
systemd.services.nginx-config-reload = let
|
|
|
|
|
sslServices = map (certName: "acme-${certName}.service") dependentCertNames;
|
|
|
|
|
sslTargets = map (certName: "acme-finished-${certName}.target") dependentCertNames;
|
|
|
|
|
in mkIf (cfg.enableReload || sslServices != []) {
|
|
|
|
|
wants = optionals (cfg.enableReload) [ "nginx.service" ];
|
|
|
|
|
wantedBy = sslServices ++ [ "multi-user.target" ];
|
|
|
|
|
# Before the finished targets, after the renew services.
|
|
|
|
|
# This service might be needed for HTTP-01 challenges, but we only want to confirm
|
|
|
|
|
# certs are updated _after_ config has been reloaded.
|
|
|
|
|
before = sslTargets;
|
|
|
|
|
after = sslServices;
|
|
|
|
|
restartTriggers = optionals (cfg.enableReload) [ configFile ];
|
|
|
|
|
# Block reloading if not all certs exist yet.
|
|
|
|
|
# Happens when config changes add new vhosts/certs.
|
|
|
|
|
unitConfig.ConditionPathExists = optionals (sslServices != []) (map (certName: certs.${certName}.directory + "/fullchain.pem") dependentCertNames);
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
TimeoutSec = 60;
|
|
|
|
|
ExecCondition = "/run/current-system/systemd/bin/systemctl -q is-active nginx.service";
|
|
|
|
|
ExecStart = "/run/current-system/systemd/bin/systemctl reload nginx.service";
|
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
|
security.acme.certs = let
|
2021-12-30 13:39:12 +00:00
|
|
|
|
acmePairs = map (vhostConfig: let
|
|
|
|
|
hasRoot = vhostConfig.acmeRoot != null;
|
|
|
|
|
in nameValuePair vhostConfig.serverName {
|
2020-09-25 04:45:31 +00:00
|
|
|
|
group = mkDefault cfg.group;
|
2021-12-30 13:39:12 +00:00
|
|
|
|
# if acmeRoot is null inherit config.security.acme
|
|
|
|
|
# Since config.security.acme.certs.<cert>.webroot's own default value
|
|
|
|
|
# should take precedence set priority higher than mkOptionDefault
|
|
|
|
|
webroot = mkOverride (if hasRoot then 1000 else 2000) vhostConfig.acmeRoot;
|
|
|
|
|
# Also nudge dnsProvider to null in case it is inherited
|
|
|
|
|
dnsProvider = mkOverride (if hasRoot then 1000 else 2000) null;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
extraDomainNames = vhostConfig.serverAliases;
|
|
|
|
|
# Filter for enableACME-only vhosts. Don't want to create dud certs
|
|
|
|
|
}) (filter (vhostConfig: vhostConfig.useACMEHost == null) acmeEnabledVhosts);
|
|
|
|
|
in listToAttrs acmePairs;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
users.users = optionalAttrs (cfg.user == "nginx") {
|
|
|
|
|
nginx = {
|
|
|
|
|
group = cfg.group;
|
2021-04-25 03:57:28 +00:00
|
|
|
|
isSystemUser = true;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
uid = config.ids.uids.nginx;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.groups = optionalAttrs (cfg.group == "nginx") {
|
|
|
|
|
nginx.gid = config.ids.gids.nginx;
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-03 18:54:34 +00:00
|
|
|
|
services.logrotate.settings.nginx = mapAttrs (_: mkDefault) {
|
|
|
|
|
files = "/var/log/nginx/*.log";
|
2022-03-05 16:20:37 +00:00
|
|
|
|
frequency = "weekly";
|
2022-04-03 18:54:34 +00:00
|
|
|
|
su = "${cfg.user} ${cfg.group}";
|
|
|
|
|
rotate = 26;
|
|
|
|
|
compress = true;
|
|
|
|
|
delaycompress = true;
|
|
|
|
|
postrotate = "[ ! -f /var/run/nginx/nginx.pid ] || kill -USR1 `cat /var/run/nginx/nginx.pid`";
|
2022-03-05 16:20:37 +00:00
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
}
|