]> Git — Sourcephile - sourcephile-nix.git/blob - nixos/profiles/services/nginx.nix
mermet: dovecot: try to fix list.sieve
[sourcephile-nix.git] / nixos / profiles / services / nginx.nix
1 { inputs, pkgs, lib, config, host, ... }:
2 let
3 inherit (lib) types;
4 inherit (config) networking;
5 inherit (config.services) nginx;
6 in
7 {
8 options = {
9 services.nginx = {
10 configs = lib.mkOption {
11 type = types.attrsOf types.lines;
12 default = {};
13 description = ''
14 Make some configs available to all virtual hosts.
15 Useful to workaround the reset of add_header:
16 https://blog.g3rt.nl/nginx-add_header-pitfall.html
17 '';
18 #apply = lib.mapAttrs (name: pkgs.writeText "${name}.conf");
19 };
20 };
21 };
22 config = {
23 systemd.tmpfiles.rules = [
24 "d '/dev/shm/nginx' '750' '${nginx.user}' '${nginx.group}' - -"
25 ];
26 systemd.services.nginx = {
27 requires = [ "systemd-tmpfiles-setup-dev.service" ];
28 serviceConfig = {
29 # FIXME: remove all the mkForce in LogsDirectory
30 # whenever upstream uses a list instead of a string.
31 LogsDirectory = lib.mkForce ["nginx"];
32 StateDirectory = ["nginx"];
33 StateDirectoryMode = "2770";
34 #BindPaths = ["/dev/shm/nginx:/var/cache/nginx"];
35 };
36 };
37 services.logrotate = {
38 enable = true;
39 paths.nginx = {
40 path = [
41 "/var/log/nginx/*.log"
42 "/var/log/nginx/*/*.log"
43 "/var/log/nginx/*/*/*.log"
44 "/var/log/nginx/*/*/*/*.log"
45 ];
46 frequency = "weekly";
47 keep = 26;
48 extraConfig = ''
49 compress
50 delaycompress
51 postrotate
52 [ ! -f /var/run/nginx/nginx.pid ] || kill -USR1 `cat /var/run/nginx/nginx.pid`
53 endscript
54 '';
55 };
56 };
57 services.nginx = {
58 eventsConfig = ''
59 multi_accept on;
60 use epoll;
61 worker_connections 1024;
62 '';
63 clientMaxBodySize = "20m";
64 recommendedGzipSettings = true;
65 recommendedOptimisation = false;
66 recommendedProxySettings = true;
67 recommendedTlsSettings = true;
68 resolver = {
69 ipv6 = lib.mkDefault (networking.defaultGateway6 != null);
70 };
71 serverTokens = false;
72 # Only allow PFS-enabled ciphers with AES256
73 #sslCiphers = "AES256+EECDH:AES256+EDH:!aNULL";
74 #sslCiphers = "HIGH:!ADH:!MD5:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
75 #sslCiphers = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
76 sslDhparam = inputs.secrets + "/openssl/dh.pem";
77 sslProtocols = "TLSv1.3 TLSv1.2";
78 configs = rec {
79 http_add_headers = ''
80 # Enable CSP
81 #add_header Content-Security-Policy "script-src 'self'; object-src 'none'; base-uri 'none';" always;
82
83 # Enable XSS protection of the browser.
84 # May be unnecessary when CSP is configured properly (see above)
85 add_header X-XSS-Protection "1; mode=block";
86
87 # Minimize information leaked to other domains
88 add_header 'Referrer-Policy' 'origin-when-cross-origin';
89
90 # Restrict embedding as a frame
91 #add_header X-Frame-Options SAMEORIGIN;
92
93 # Prevent injection of code in other mime types (XSS Attacks)
94 add_header X-Content-Type-Options nosniff;
95 '';
96 https_add_headers = ''
97 ${http_add_headers}
98 # Add HSTS header with preloading to HTTPS requests.
99 # Adding this header to HTTP requests is discouraged,
100 # as doing so makes the connection vulnerable to SSL stripping attacks
101 # DOC: https://blog.qualys.com/securitylabs/2016/03/28/the-importance-of-a-proper-http-strict-transport-security-implementation-on-your-web-server
102 add_header Strict-Transport-Security $hsts_header;
103 '';
104 };
105 commonHttpConfig = ''
106 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
107 '$status $body_bytes_sent "$http_referer" '
108 '"$http_user_agent" "$http_x_forwarded_for"';
109 log_format json escape=json '{'
110 '"time_local":"$time_local",'
111 '"host":"$host",'
112 '"request":"$request",'
113 '"status":"$status",'
114 '"http_referrer":"$http_referer",'
115 '"remote_addr":"$remote_addr",'
116 '"remote_user":"$remote_user",'
117 '"msec":"$msec",'
118 '"body_bytes_sent":"$body_bytes_sent",'
119 '"bytes_sent":"$bytes_sent",'
120 '"http_user_agent":"$http_user_agent",'
121 '"request_length":"$request_length",'
122 '"request_method":"$request_method",'
123 '"request_time":"$request_time",'
124 '"request_uri":"$request_uri",'
125 '"server_protocol":"$server_protocol",'
126 '"ssl_protocol":"$ssl_protocol",'
127 '"upstream_addr":"$upstream_addr",'
128 '"upstream_connect_time":"$upstream_connect_time",'
129 '"upstream_response_time":"$upstream_response_time"'
130 '}';
131 charset UTF-8;
132 types {
133 text/html html5;
134 text/plain dump;
135 }
136 '' +
137 lib.concatStringsSep "\n" (lib.attrValues {
138 default = ''
139 #default_type application/octet-stream;
140 root /var/lib/nginx;
141 '';
142 security = ''
143 #error_page 403 = 404;
144
145 ${nginx.configs.http_add_headers}
146
147 # This might create errors
148 #proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
149 '';
150 log = ''
151 access_log /var/log/nginx/access.json json;
152 error_log /var/log/nginx/error.log warn;
153 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
154 '';
155 proxy = ''
156 proxy_cache_use_stale updating;
157 proxy_temp_path /var/cache/nginx/proxy_temp 1 2;
158 '';
159 fastcgi = ''
160 # DOC: http://wiki.nginx.org/HttpFastcgiModule
161 fastcgi_buffer_size 128k;
162 fastcgi_buffers 256 4k;
163 fastcgi_busy_buffers_size 256k;
164 fastcgi_cache_key "$request_method $scheme://$http_host$request_uri";
165 fastcgi_connect_timeout 60;
166 fastcgi_ignore_client_abort off;
167 fastcgi_intercept_errors on;
168 fastcgi_max_temp_file_size 2M;
169 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
170 fastcgi_param SCRIPT_FILENAME $request_filename;
171 fastcgi_temp_path /var/cache/nginx/fastcgi_temp 1 2;
172 '';
173 connection = ''
174 sendfile on;
175 # If the client stops reading data,
176 # free up the stale client connection after this much time.
177 send_timeout 60;
178 # Causes nginx to attempt to send its HTTP response head
179 # in one packet, instead of using partial frames.
180 # This is useful for prepending headers before calling sendfile,
181 # or for throughput optimization.
182 tcp_nopush on;
183 # Don't buffer data-sends (disable Nagle algorithm).
184 # Good for sending frequent small bursts of data in real time.
185 tcp_nodelay on;
186 keepalive_timeout 20;
187 reset_timedout_connection on;
188 server_names_hash_bucket_size 128;
189 '';
190 map = ''
191 map $time_iso8601 $date {
192 default 'date-not-found';
193 '~^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})' $year-$month-$day;
194 }
195
196 map $scheme $hsts_header {
197 https "max-age=31536000; includeSubdomains; preload";
198 }
199
200 # User agents that are to be blocked.
201 #map $http_user_agent $bad_bot {
202 # default 0;
203 # libwww-perl 1;
204 # ~(?i)(httrack|htmlparser|libwww) 1;
205 #}
206 # Referrers that are to be blocked.
207 #map $http_referer $bad_referer {
208 # default 0;
209 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
210 #}
211 #geo $not_local {
212 # default 1;
213 # 127.0.0.1 0;
214 #}
215 '';
216 cache = ''
217 client_body_buffer_size 4K;
218 # getconf PAGESIZE
219 # 4096
220 client_body_temp_path /var/cache/nginx/client_body_temp 1 2;
221 client_body_timeout 60;
222 client_header_buffer_size 1k;
223 client_header_timeout 60;
224 large_client_header_buffers 4 8k;
225
226 open_file_cache max=200000 inactive=20s;
227 open_file_cache_errors on;
228 open_file_cache_min_uses 2;
229 open_file_cache_valid 30s;
230 '';
231 });
232 appendConfig = ''
233 worker_processes ${toString host.CPUs};
234 '';
235 };
236 };
237 }