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