1 {pkgs, lib, config, system, ...}:
2 let inherit (builtins) readFile;
3 inherit (builtins.extraBuiltins) pass;
5 inherit (pkgs.lib) loadFile;
6 inherit (config) networking;
7 inherit (config.services) nginx;
8 domainDir = dom: lib.concatStringsSep "/" (lib.reverseList (lib.splitString "." dom));
17 x509Dir = lib.mkOption {
19 default = "/var/lib/nginx/x509";
21 webDir = lib.mkOption {
25 logDir = lib.mkOption {
27 default = "/var/log/nginx";
32 systemd.services.nginx.after = [
33 "${networking.domainBase}.key.pem-key.service"
37 "nginx.${networking.domainBase}.key.pem" = {
38 text = pass "x509/${networking.domainBase}/key.pem";
41 destDir = "/run/keys/";
42 permissions = "0400"; # WARNING: not enforced when deployment.storeKeysOnMachine = true
45 systemd.services.nginx = {
46 preStart = lib.mkBefore ''
47 install -D -d -o ${nginx.user} -g ${nginx.group} -m 0700 \
55 stateDir = "/dev/shm/nginx";
59 worker_connections 1024;
61 clientMaxBodySize = "20m";
62 recommendedProxySettings = true;
63 recommendedTlsSettings = true;
65 sslCiphers = "HIGH:!ADH:!MD5:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
66 sslDhparam = ../../../sec/openssl/dh.pem;
67 #sslCiphers = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL;";
68 #sslProtocols = "TLSv1.2";
70 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
71 '$status $body_bytes_sent "$http_referer" '
72 '"$http_user_agent" "$http_x_forwarded_for"';
75 lib.concatStringsSep "\n" (lib.attrValues {
77 default_type application/octet-stream;
81 #error_page 403 = 404;
84 access_log ${nginx.logDir}/access.log main buffer=32k;
85 error_log ${nginx.logDir}/error.log warn;
86 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
89 proxy_cache_use_stale updating;
90 proxy_temp_path ${nginx.stateDir}/proxy_temp 1 2;
93 # DOC: http://wiki.nginx.org/HttpFastcgiModule
94 fastcgi_buffer_size 128k;
95 fastcgi_buffers 256 4k;
96 fastcgi_busy_buffers_size 256k;
97 fastcgi_cache_key "$request_method $scheme://$http_host$request_uri";
98 fastcgi_cache_path ${nginx.stateDir}/fastcgi_cache
100 keys_zone=microcache:2M
104 loader_threshold=2592000000
106 fastcgi_connect_timeout 60;
107 fastcgi_ignore_client_abort off;
108 fastcgi_intercept_errors on;
109 fastcgi_max_temp_file_size 2M;
110 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
111 fastcgi_param SCRIPT_FILENAME $request_filename;
112 fastcgi_temp_path ${nginx.stateDir}/fastcgi_temp 1 2;
116 # If the client stops reading data,
117 # free up the stale client connection after this much time.
119 # Causes nginx to attempt to send its HTTP response head
120 # in one packet, instead of using partial frames.
121 # This is useful for prepending headers before calling sendfile,
122 # or for throughput optimization.
124 # Don't buffer data-sends (disable Nagle algorithm).
125 # Good for sending frequent small bursts of data in real time.
127 keepalive_timeout 20;
128 reset_timedout_connection on;
129 server_names_hash_bucket_size 128;
130 types_hash_max_size 2048;
133 # User agents that are to be blocked.
134 #map $http_user_agent $bad_bot {
137 # ~(?i)(httrack|htmlparser|libwww) 1;
139 # Referrers that are to be blocked.
140 #map $http_referer $bad_referer {
142 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
153 gzip_disable "MSIE [1-6]\.";
154 gzip_http_version 1.1;
155 gzip_min_length 1024;
159 gzip_types application/atom+xml
160 application/javascript
163 application/vnd.ms-fontobject
164 application/x-font-ttf
165 application/x-javascript
178 client_body_buffer_size 4K;
181 client_body_temp_path ${nginx.stateDir}/client_body_temp 1 2;
182 client_body_timeout 60;
183 client_header_buffer_size 1k;
184 client_header_timeout 60;
185 large_client_header_buffers 4 8k;
187 open_file_cache max=200000 inactive=20s;
188 open_file_cache_errors on;
189 open_file_cache_min_uses 2;
190 open_file_cache_valid 30s;
198 # Convoluted way to load the certificate in the store and using ${networking.domainBase} to find it.
199 # NOTE: no ssl_stapling while the certificate remains self-signed.
200 sslCertificate = loadFile (../../../sec + "/openssl/${networking.domainBase}/cert.self-signed.pem");
201 sslCertificateKey = "/run/keys/nginx.${networking.domainBase}.key.pem";