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