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