]> Git — Sourcephile - sourcephile-nix.git/blob - servers/mermet/nginx.nix
nix: add module security.pass
[sourcephile-nix.git] / servers / mermet / nginx.nix
1 {pkgs, lib, config, system, ...}:
2 let
3 inherit (builtins) readFile;
4 inherit (lib) types;
5 inherit (pkgs.lib) loadFile;
6 inherit (config) networking;
7 inherit (config.services) nginx;
8 domainDir = dom: lib.concatStringsSep "/" (lib.reverseList (lib.splitString "." dom));
9 in
10 {
11 imports = [
12 nginx/sourcephile.fr.nix
13 ];
14 options = {
15 services.nginx = {
16 webDir = lib.mkOption {
17 type = types.str;
18 default = "/var/www";
19 };
20 cacheDir = lib.mkOption {
21 type = types.str;
22 default = "/dev/shm/nginx";
23 };
24 logDir = lib.mkOption {
25 type = types.str;
26 default = "/var/log/nginx";
27 };
28 configs = lib.mkOption {
29 type = types.attrsOf types.lines;
30 default = {};
31 description = ''
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
35 '';
36 #apply = lib.mapAttrs (name: pkgs.writeText "${name}.conf");
37 };
38 };
39 };
40 config = {
41 systemd.services.nginx = {
42 preStart = lib.mkBefore ''
43 install -D -d -o ${nginx.user} -g ${nginx.group} -m 0700 \
44 ${nginx.cacheDir} \
45 ${nginx.logDir} \
46 ${nginx.webDir}
47 '';
48 };
49 users.groups."acme".members = [nginx.user];
50 services.nginx = {
51 enable = true;
52 package = pkgs.nginx.override {
53 modules = with pkgs.nginxModules; [
54 fancyindex
55 ];
56 };
57 eventsConfig = ''
58 multi_accept on;
59 use epoll;
60 worker_connections 1024;
61 '';
62 clientMaxBodySize = "20m";
63 recommendedGzipSettings = true;
64 recommendedOptimisation = false;
65 recommendedProxySettings = true;
66 recommendedTlsSettings = true;
67 resolver = {
68 addresses = [ "127.0.0.1:53" ];
69 valid = "";
70 ipv6 = networking.defaultGateway6 != null;
71 };
72 serverTokens = false;
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";
79 configs = rec {
80 http_add_headers = ''
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;
85
86 # Enable CSP for your services.
87 #add_header Content-Security-Policy "script-src 'self'; object-src 'none'; base-uri 'none';" always;
88
89 # Minimize information leaked to other domains
90 add_header 'Referrer-Policy' 'origin-when-cross-origin';
91
92 # Disable embedding as a frame
93 add_header X-Frame-Options DENY;
94
95 # Prevent injection of code in other mime types (XSS Attacks)
96 add_header X-Content-Type-Options nosniff;
97
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";
101 '';
102 https_add_headers = ''
103 ${http_add_headers}
104 '';
105 };
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"';
110
111 log_format json escape=json
112 '{'
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"'
122 '}';
123 charset UTF-8;
124 types {
125 text/html html5;
126 text/plain md;
127 }
128 '' +
129 lib.concatStringsSep "\n" (lib.attrValues {
130 default = ''
131 default_type application/octet-stream;
132 root ${nginx.webDir};
133 '';
134 security = ''
135 #error_page 403 = 404;
136
137 ${nginx.configs.http_add_headers}
138
139 # This might create errors
140 proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
141 '';
142 log = ''
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;
146 '';
147 proxy = ''
148 proxy_cache_use_stale updating;
149 proxy_temp_path ${nginx.cacheDir}/proxy_temp 1 2;
150 '';
151 fastcgi = ''
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;
164 '';
165 connection = ''
166 sendfile on;
167 # If the client stops reading data,
168 # free up the stale client connection after this much time.
169 send_timeout 60;
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.
174 tcp_nopush on;
175 # Don't buffer data-sends (disable Nagle algorithm).
176 # Good for sending frequent small bursts of data in real time.
177 tcp_nodelay on;
178 keepalive_timeout 20;
179 reset_timedout_connection on;
180 types_hash_max_size 4096;
181 server_names_hash_bucket_size 128;
182 '';
183 map = ''
184 map $time_iso8601 $date {
185 default 'date-not-found';
186 '~^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})' $year-$month-$day;
187 }
188
189 map $scheme $hsts_header {
190 https "max-age=31536000; includeSubdomains; preload";
191 }
192
193 # User agents that are to be blocked.
194 #map $http_user_agent $bad_bot {
195 # default 0;
196 # libwww-perl 1;
197 # ~(?i)(httrack|htmlparser|libwww) 1;
198 #}
199 # Referrers that are to be blocked.
200 #map $http_referer $bad_referer {
201 # default 0;
202 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
203 #}
204 #geo $not_local {
205 # default 1;
206 # 127.0.0.1 0;
207 #}
208 '';
209 cache = ''
210 client_body_buffer_size 4K;
211 # getconf PAGESIZE
212 # 4096
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;
218
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;
223 '';
224 });
225 appendConfig = ''
226 worker_processes ${toString config.nix.maxJobs};
227 '';
228 virtualHosts."_" = {
229 forceSSL = true;
230 useACMEHost = networking.domain;
231 };
232 };
233 };
234 }