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));
13 nginx/sourcephile.fr.nix
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 = {
33 preStart = lib.mkBefore ''
34 install -D -d -o ${nginx.user} -g ${nginx.group} -m 0700 \
40 users.groups."acme".members = [nginx.user];
43 stateDir = "/dev/shm/nginx";
47 worker_connections 1024;
49 clientMaxBodySize = "20m";
50 recommendedGzipSettings = true;
51 recommendedOptimisation = false;
52 recommendedProxySettings = true;
53 recommendedTlsSettings = true;
55 addresses = [ "127.0.0.1:53" ];
57 ipv6 = networking.defaultGateway6 != null;
60 # Only allow PFS-enabled ciphers with AES256
61 #sslCiphers = "AES256+EECDH:AES256+EDH:!aNULL";
62 #sslCiphers = "HIGH:!ADH:!MD5:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
63 #sslCiphers = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
64 sslDhparam = ../../../sec/openssl/dh.pem;
65 sslProtocols = "TLSv1.3 TLSv1.2";
67 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
68 '$status $body_bytes_sent "$http_referer" '
69 '"$http_user_agent" "$http_x_forwarded_for"';
72 lib.concatStringsSep "\n" (lib.attrValues {
74 default_type application/octet-stream;
78 #error_page 403 = 404;
80 # Add HSTS header with preloading to HTTPS requests.
81 # Adding this header to HTTP requests is discouraged
82 # DOC: https://blog.qualys.com/securitylabs/2016/03/28/the-importance-of-a-proper-http-strict-transport-security-implementation-on-your-web-server
83 map $scheme $hsts_header {
84 https "max-age=31536000; includeSubdomains; preload";
86 add_header Strict-Transport-Security $hsts_header;
88 # Enable CSP for your services.
89 #add_header Content-Security-Policy "script-src 'self'; object-src 'none'; base-uri 'none';" always;
91 # Minimize information leaked to other domains
92 add_header 'Referrer-Policy' 'origin-when-cross-origin';
94 # Disable embedding as a frame
95 add_header X-Frame-Options DENY;
97 # Prevent injection of code in other mime types (XSS Attacks)
98 add_header X-Content-Type-Options nosniff;
100 # Enable XSS protection of the browser.
101 # May be unnecessary when CSP is configured properly (see above)
102 add_header X-XSS-Protection "1; mode=block";
104 # This might create errors
105 proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
107 # If a client has a session ticket, it can present it to the server and re-negotiation is not necessary.
108 ssl_session_tickets on;
111 access_log ${nginx.logDir}/access.log main buffer=32k;
112 error_log ${nginx.logDir}/error.log warn;
113 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
116 proxy_cache_use_stale updating;
117 proxy_temp_path ${nginx.stateDir}/proxy_temp 1 2;
120 # DOC: http://wiki.nginx.org/HttpFastcgiModule
121 fastcgi_buffer_size 128k;
122 fastcgi_buffers 256 4k;
123 fastcgi_busy_buffers_size 256k;
124 fastcgi_cache_key "$request_method $scheme://$http_host$request_uri";
125 fastcgi_cache_path ${nginx.stateDir}/fastcgi_cache
127 keys_zone=microcache:2M
131 loader_threshold=2592000000
133 fastcgi_connect_timeout 60;
134 fastcgi_ignore_client_abort off;
135 fastcgi_intercept_errors on;
136 fastcgi_max_temp_file_size 2M;
137 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
138 fastcgi_param SCRIPT_FILENAME $request_filename;
139 fastcgi_temp_path ${nginx.stateDir}/fastcgi_temp 1 2;
143 # If the client stops reading data,
144 # free up the stale client connection after this much time.
146 # Causes nginx to attempt to send its HTTP response head
147 # in one packet, instead of using partial frames.
148 # This is useful for prepending headers before calling sendfile,
149 # or for throughput optimization.
151 # Don't buffer data-sends (disable Nagle algorithm).
152 # Good for sending frequent small bursts of data in real time.
154 keepalive_timeout 20;
155 reset_timedout_connection on;
156 types_hash_max_size 4096;
157 server_names_hash_bucket_size 128;
160 # User agents that are to be blocked.
161 #map $http_user_agent $bad_bot {
164 # ~(?i)(httrack|htmlparser|libwww) 1;
166 # Referrers that are to be blocked.
167 #map $http_referer $bad_referer {
169 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
177 client_body_buffer_size 4K;
180 client_body_temp_path ${nginx.stateDir}/client_body_temp 1 2;
181 client_body_timeout 60;
182 client_header_buffer_size 1k;
183 client_header_timeout 60;
184 large_client_header_buffers 4 8k;
186 open_file_cache max=200000 inactive=20s;
187 open_file_cache_errors on;
188 open_file_cache_min_uses 2;
189 open_file_cache_valid 30s;
193 worker_processes ${toString config.nix.maxJobs};
197 useACMEHost = networking.domain;