1 {pkgs, lib, config, system, ...}:
3 inherit (builtins) readFile;
4 inherit (builtins.extraBuiltins) pass;
6 inherit (pkgs.lib) loadFile;
7 inherit (config) networking;
8 inherit (config.services) nginx;
9 domainDir = dom: lib.concatStringsSep "/" (lib.reverseList (lib.splitString "." dom));
18 x509Dir = lib.mkOption {
20 default = "/var/lib/nginx/x509";
22 webDir = lib.mkOption {
26 logDir = lib.mkOption {
28 default = "/var/log/nginx";
33 systemd.services.nginx = {
34 preStart = lib.mkBefore ''
35 install -D -d -o ${nginx.user} -g ${nginx.group} -m 0700 \
41 "${networking.domain}.key.pem-key.service"
46 stateDir = "/dev/shm/nginx";
50 worker_connections 1024;
52 clientMaxBodySize = "20m";
53 recommendedProxySettings = true;
54 recommendedTlsSettings = true;
56 # Only allow PFS-enabled ciphers with AES256
57 sslCiphers = "AES256+EECDH:AES256+EDH:!aNULL";
58 #sslCiphers = "HIGH:!ADH:!MD5:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
59 sslDhparam = ../../../sec/openssl/dh.pem;
60 #sslCiphers = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL;";
61 #sslProtocols = "TLSv1.2";
63 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
64 '$status $body_bytes_sent "$http_referer" '
65 '"$http_user_agent" "$http_x_forwarded_for"';
68 lib.concatStringsSep "\n" (lib.attrValues {
70 default_type application/octet-stream;
74 #error_page 403 = 404;
76 # Add HSTS header with preloading to HTTPS requests.
77 # Adding this header to HTTP requests is discouraged
78 #map $scheme $hsts_header {
79 # https "max-age=31536000; includeSubdomains; preload";
81 #add_header Strict-Transport-Security $hsts_header;
83 # Enable CSP for your services.
84 #add_header Content-Security-Policy "script-src 'self'; object-src 'none'; base-uri 'none';" always;
86 # Minimize information leaked to other domains
87 add_header 'Referrer-Policy' 'origin-when-cross-origin';
89 # Disable embedding as a frame
90 add_header X-Frame-Options DENY;
92 # Prevent injection of code in other mime types (XSS Attacks)
93 add_header X-Content-Type-Options nosniff;
95 # Enable XSS protection of the browser.
96 # May be unnecessary when CSP is configured properly (see above)
97 add_header X-XSS-Protection "1; mode=block";
99 # This might create errors
100 proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
103 access_log ${nginx.logDir}/access.log main buffer=32k;
104 error_log ${nginx.logDir}/error.log warn;
105 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
108 proxy_cache_use_stale updating;
109 proxy_temp_path ${nginx.stateDir}/proxy_temp 1 2;
112 # DOC: http://wiki.nginx.org/HttpFastcgiModule
113 fastcgi_buffer_size 128k;
114 fastcgi_buffers 256 4k;
115 fastcgi_busy_buffers_size 256k;
116 fastcgi_cache_key "$request_method $scheme://$http_host$request_uri";
117 fastcgi_cache_path ${nginx.stateDir}/fastcgi_cache
119 keys_zone=microcache:2M
123 loader_threshold=2592000000
125 fastcgi_connect_timeout 60;
126 fastcgi_ignore_client_abort off;
127 fastcgi_intercept_errors on;
128 fastcgi_max_temp_file_size 2M;
129 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
130 fastcgi_param SCRIPT_FILENAME $request_filename;
131 fastcgi_temp_path ${nginx.stateDir}/fastcgi_temp 1 2;
135 # If the client stops reading data,
136 # free up the stale client connection after this much time.
138 # Causes nginx to attempt to send its HTTP response head
139 # in one packet, instead of using partial frames.
140 # This is useful for prepending headers before calling sendfile,
141 # or for throughput optimization.
143 # Don't buffer data-sends (disable Nagle algorithm).
144 # Good for sending frequent small bursts of data in real time.
146 keepalive_timeout 20;
147 reset_timedout_connection on;
148 server_names_hash_bucket_size 128;
149 types_hash_max_size 2048;
152 # User agents that are to be blocked.
153 #map $http_user_agent $bad_bot {
156 # ~(?i)(httrack|htmlparser|libwww) 1;
158 # Referrers that are to be blocked.
159 #map $http_referer $bad_referer {
161 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
172 gzip_disable "MSIE [1-6]\.";
173 gzip_http_version 1.1;
174 gzip_min_length 1024;
178 gzip_types application/atom+xml
179 application/javascript
182 application/vnd.ms-fontobject
183 application/x-font-ttf
184 application/x-javascript
197 client_body_buffer_size 4K;
200 client_body_temp_path ${nginx.stateDir}/client_body_temp 1 2;
201 client_body_timeout 60;
202 client_header_buffer_size 1k;
203 client_header_timeout 60;
204 large_client_header_buffers 4 8k;
206 open_file_cache max=200000 inactive=20s;
207 open_file_cache_errors on;
208 open_file_cache_min_uses 2;
209 open_file_cache_valid 30s;
217 # Convoluted way to load the certificate in the store and using ${networking.domainBase} to find it.
218 # NOTE: no ssl_stapling while the certificate remains self-signed.
219 sslCertificate = loadFile (../../../sec + "/openssl/${networking.domain}/cert.self-signed.pem");
220 sslCertificateKey = "/run/keys/${networking.domain}.key.pem";