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