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