1 {pkgs, lib, config, system, ...}:
3 inherit (builtins) readFile;
5 inherit (pkgs.lib) loadFile;
6 inherit (config) networking;
7 inherit (config.services) nginx;
8 domainDir = dom: lib.concatStringsSep "/" (lib.reverseList (lib.splitString "." dom));
12 nginx/sourcephile.fr.nix
16 webDir = lib.mkOption {
20 cacheDir = lib.mkOption {
22 default = "/dev/shm/nginx";
24 logDir = lib.mkOption {
26 default = "/var/log/nginx";
28 configs = lib.mkOption {
29 type = types.attrsOf types.lines;
32 Make some configs available to all virtual hosts.
33 Useful to workaround the reset of add_header:
34 https://blog.g3rt.nl/nginx-add_header-pitfall.html
36 #apply = lib.mapAttrs (name: pkgs.writeText "${name}.conf");
41 systemd.services.nginx = {
42 preStart = lib.mkBefore ''
43 install -D -d -o ${nginx.user} -g ${nginx.group} -m 0700 \
49 users.groups."acme".members = [nginx.user];
52 package = pkgs.nginx.override {
53 modules = with pkgs.nginxModules; [
60 worker_connections 1024;
62 clientMaxBodySize = "20m";
63 recommendedGzipSettings = true;
64 recommendedOptimisation = false;
65 recommendedProxySettings = true;
66 recommendedTlsSettings = true;
68 addresses = [ "127.0.0.1:53" ];
70 ipv6 = networking.defaultGateway6 != null;
73 # Only allow PFS-enabled ciphers with AES256
74 #sslCiphers = "AES256+EECDH:AES256+EDH:!aNULL";
75 #sslCiphers = "HIGH:!ADH:!MD5:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
76 #sslCiphers = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
77 sslDhparam = ../../../sec/openssl/dh.pem;
78 sslProtocols = "TLSv1.3 TLSv1.2";
81 # Add HSTS header with preloading to HTTPS requests.
82 # Adding this header to HTTP requests is discouraged
83 # DOC: https://blog.qualys.com/securitylabs/2016/03/28/the-importance-of-a-proper-http-strict-transport-security-implementation-on-your-web-server
84 add_header Strict-Transport-Security $hsts_header;
86 # Enable CSP for your services.
87 #add_header Content-Security-Policy "script-src 'self'; object-src 'none'; base-uri 'none';" always;
89 # Minimize information leaked to other domains
90 add_header 'Referrer-Policy' 'origin-when-cross-origin';
92 # Disable embedding as a frame
93 add_header X-Frame-Options DENY;
95 # Prevent injection of code in other mime types (XSS Attacks)
96 add_header X-Content-Type-Options nosniff;
98 # Enable XSS protection of the browser.
99 # May be unnecessary when CSP is configured properly (see above)
100 add_header X-XSS-Protection "1; mode=block";
102 https_add_headers = ''
106 commonHttpConfig = ''
107 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
108 '$status $body_bytes_sent "$http_referer" '
109 '"$http_user_agent" "$http_x_forwarded_for"';
111 log_format json escape=json
113 '"time_local":"$time_local",'
114 '"remote_addr":"$remote_addr",'
115 '"status": "$status",'
116 '"request":"$request",'
117 '"body_bytes_sent":"$body_bytes_sent",'
118 '"http_referrer":"$http_referer",'
119 '"http_user_agent":"$http_user_agent",'
120 '"remote_user":"$remote_user",'
121 '"request_time":"$request_time"'
129 lib.concatStringsSep "\n" (lib.attrValues {
131 default_type application/octet-stream;
132 root ${nginx.webDir};
135 #error_page 403 = 404;
137 ${nginx.configs.http_add_headers}
139 # This might create errors
140 proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
143 access_log ${nginx.logDir}/access.log main buffer=32k;
144 error_log ${nginx.logDir}/error.log warn;
145 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
148 proxy_cache_use_stale updating;
149 proxy_temp_path ${nginx.cacheDir}/proxy_temp 1 2;
152 # DOC: http://wiki.nginx.org/HttpFastcgiModule
153 fastcgi_buffer_size 128k;
154 fastcgi_buffers 256 4k;
155 fastcgi_busy_buffers_size 256k;
156 fastcgi_cache_key "$request_method $scheme://$http_host$request_uri";
157 fastcgi_connect_timeout 60;
158 fastcgi_ignore_client_abort off;
159 fastcgi_intercept_errors on;
160 fastcgi_max_temp_file_size 2M;
161 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
162 fastcgi_param SCRIPT_FILENAME $request_filename;
163 fastcgi_temp_path ${nginx.cacheDir}/fastcgi_temp 1 2;
167 # If the client stops reading data,
168 # free up the stale client connection after this much time.
170 # Causes nginx to attempt to send its HTTP response head
171 # in one packet, instead of using partial frames.
172 # This is useful for prepending headers before calling sendfile,
173 # or for throughput optimization.
175 # Don't buffer data-sends (disable Nagle algorithm).
176 # Good for sending frequent small bursts of data in real time.
178 keepalive_timeout 20;
179 reset_timedout_connection on;
180 types_hash_max_size 4096;
181 server_names_hash_bucket_size 128;
184 map $time_iso8601 $date {
185 default 'date-not-found';
186 '~^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})' $year-$month-$day;
189 map $scheme $hsts_header {
190 https "max-age=31536000; includeSubdomains; preload";
193 # User agents that are to be blocked.
194 #map $http_user_agent $bad_bot {
197 # ~(?i)(httrack|htmlparser|libwww) 1;
199 # Referrers that are to be blocked.
200 #map $http_referer $bad_referer {
202 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
210 client_body_buffer_size 4K;
213 client_body_temp_path ${nginx.cacheDir}/client_body_temp 1 2;
214 client_body_timeout 60;
215 client_header_buffer_size 1k;
216 client_header_timeout 60;
217 large_client_header_buffers 4 8k;
219 open_file_cache max=200000 inactive=20s;
220 open_file_cache_errors on;
221 open_file_cache_min_uses 2;
222 open_file_cache_valid 30s;
226 worker_processes ${toString config.nix.maxJobs};
230 useACMEHost = networking.domain;