1 {pkgs, lib, config, system, ...}:
2 let inherit (builtins) readFile toPath;
3 inherit (builtins.extraBuiltins) pass;
5 inherit (config) networking;
6 inherit (config.services) nginx x509;
7 domainDir = dom: lib.concatStringsSep "/" (lib.reverseList (lib.splitString "." dom));
15 x509Dir = lib.mkOption {
17 default = "/var/lib/nginx/x509";
19 webDir = lib.mkOption {
23 logDir = lib.mkOption {
25 default = "/var/log/nginx";
30 systemd.services.nginx.after = [
31 "${networking.domainBase}.key.pem-key.service"
35 "${networking.domainBase}.key.pem" = {
36 text = pass "x509/${networking.domainBase}/key.pem";
39 destDir = "/var/lib/nginx/x509/";
40 permissions = "0400"; # WARNING: not enforced when deployment.storeKeysOnMachine = true
43 systemd.services.nginx = {
44 preStart = lib.mkBefore ''
45 install -D -d -o ${nginx.user} -g ${nginx.group} -m 0700 \
53 stateDir = "/dev/shm/nginx";
57 worker_connections 1024;
59 clientMaxBodySize = "20m";
60 recommendedProxySettings = true;
61 recommendedTlsSettings = true;
63 sslCiphers = "HIGH:!ADH:!MD5:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
64 sslDhparam = ../../../sec/openssl/dh.pem;
65 #sslCiphers = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL;";
66 #sslProtocols = "TLSv1.2";
68 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
69 '$status $body_bytes_sent "$http_referer" '
70 '"$http_user_agent" "$http_x_forwarded_for"';
73 lib.concatStringsSep "\n" (lib.attrValues {
75 default_type application/octet-stream;
79 #error_page 403 = 404;
82 access_log ${nginx.logDir}/access.log main buffer=32k;
83 error_log ${nginx.logDir}/error.log warn;
84 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
87 proxy_cache_use_stale updating;
88 proxy_temp_path ${nginx.stateDir}/proxy_temp 1 2;
91 # DOC: http://wiki.nginx.org/HttpFastcgiModule
92 fastcgi_buffer_size 128k;
93 fastcgi_buffers 256 4k;
94 fastcgi_busy_buffers_size 256k;
95 fastcgi_cache_key "$request_method $scheme://$http_host$request_uri";
96 fastcgi_cache_path ${nginx.stateDir}/fastcgi_cache
98 keys_zone=microcache:2M
102 loader_threshold=2592000000
104 fastcgi_connect_timeout 60;
105 fastcgi_ignore_client_abort off;
106 fastcgi_intercept_errors on;
107 fastcgi_max_temp_file_size 2M;
108 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
109 fastcgi_param SCRIPT_FILENAME $request_filename;
110 fastcgi_temp_path ${nginx.stateDir}/fastcgi_temp 1 2;
114 # If the client stops reading data,
115 # free up the stale client connection after this much time.
117 # Causes nginx to attempt to send its HTTP response head
118 # in one packet, instead of using partial frames.
119 # This is useful for prepending headers before calling sendfile,
120 # or for throughput optimization.
122 # Don't buffer data-sends (disable Nagle algorithm).
123 # Good for sending frequent small bursts of data in real time.
125 keepalive_timeout 20;
126 reset_timedout_connection on;
127 server_names_hash_bucket_size 128;
128 types_hash_max_size 2048;
131 # User agents that are to be blocked.
132 #map $http_user_agent $bad_bot {
135 # ~(?i)(httrack|htmlparser|libwww) 1;
137 # Referrers that are to be blocked.
138 #map $http_referer $bad_referer {
140 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
151 gzip_disable "MSIE [1-6]\.";
152 gzip_http_version 1.1;
153 gzip_min_length 1024;
157 gzip_types application/atom+xml
158 application/javascript
161 application/vnd.ms-fontobject
162 application/x-font-ttf
163 application/x-javascript
176 client_body_buffer_size 4K;
179 client_body_temp_path ${nginx.stateDir}/client_body_temp 1 2;
180 client_body_timeout 60;
181 client_header_buffer_size 1k;
182 client_header_timeout 60;
183 large_client_header_buffers 4 8k;
185 open_file_cache max=200000 inactive=20s;
186 open_file_cache_errors on;
187 open_file_cache_min_uses 2;
188 open_file_cache_valid 30s;
196 # Convoluted way to load the certificate in the store and using ${networking.domainBase} to find it.
197 # NOTE: no ssl_stapling while the certificate remains self-signed.
199 pkgs.writeText "cert.self-signed.pem"
200 (readFile ((toPath ../../../sec) + "/openssl/${networking.domainBase}/cert.self-signed.pem"));
201 sslCertificateKey = "/var/lib/nginx/x509/${networking.domainBase}.key.pem";