]> Git — Sourcephile - sourcephile-nix.git/blob - servers/mermet/nginx.nix
mermet: add gitolite support
[sourcephile-nix.git] / servers / mermet / nginx.nix
1 {pkgs, lib, config, system, ...}:
2 let inherit (builtins) readFile toPath;
3 inherit (builtins.extraBuiltins) pass;
4 inherit (lib) types;
5 inherit (config) networking;
6 inherit (config.services) nginx x509;
7 domainDir = dom: lib.concatStringsSep "/" (lib.reverseList (lib.splitString "." dom));
8 in
9 {
10 imports = [
11 nginx/gitweb.nix
12 ];
13 options = {
14 services.nginx = {
15 x509Dir = lib.mkOption {
16 type = types.str;
17 default = "/var/lib/nginx/x509";
18 };
19 webDir = lib.mkOption {
20 type = types.str;
21 default = "/var/www";
22 };
23 logDir = lib.mkOption {
24 type = types.str;
25 default = "/var/log/nginx";
26 };
27 };
28 };
29 config = {
30 systemd.services.nginx.after = [
31 "${networking.domainBase}.key.pem-key.service"
32 #"keys.target"
33 ];
34 deployment.keys = {
35 "${networking.domainBase}.key.pem" = {
36 text = pass "x509/${networking.domainBase}/key.pem";
37 user = nginx.user;
38 group = "nobody";
39 destDir = "/var/lib/nginx/x509/";
40 permissions = "0400"; # WARNING: not enforced when deployment.storeKeysOnMachine = true
41 };
42 };
43 systemd.services.nginx = {
44 preStart = lib.mkBefore ''
45 install -D -d -o ${nginx.user} -g ${nginx.group} -m 0700 \
46 ${nginx.x509Dir} \
47 ${nginx.webDir} \
48 ${nginx.logDir}
49 '';
50 };
51 services.nginx = {
52 enable = true;
53 stateDir = "/dev/shm/nginx";
54 eventsConfig = ''
55 multi_accept on;
56 use epoll;
57 worker_connections 1024;
58 '';
59 clientMaxBodySize = "20m";
60 recommendedProxySettings = true;
61 recommendedTlsSettings = true;
62 serverTokens = false;
63 sslCiphers = "HIGH:!ADH:!MD5:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
64 sslDhparam = ../../../sec/openssl/dh.pem;
65 #sslCiphers = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL;";
66 #sslProtocols = "TLSv1.2";
67 commonHttpConfig = ''
68 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
69 '$status $body_bytes_sent "$http_referer" '
70 '"$http_user_agent" "$http_x_forwarded_for"';
71 #charset UTF-8;
72 '' +
73 lib.concatStringsSep "\n" (lib.attrValues {
74 default = ''
75 default_type application/octet-stream;
76 root ${nginx.webDir};
77 '';
78 security = ''
79 #error_page 403 = 404;
80 '';
81 log = ''
82 access_log ${nginx.logDir}/access.log main buffer=32k;
83 error_log ${nginx.logDir}/error.log warn;
84 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
85 '';
86 proxy = ''
87 proxy_cache_use_stale updating;
88 proxy_temp_path ${nginx.stateDir}/proxy_temp 1 2;
89 '';
90 fastcgi = ''
91 # DOC: http://wiki.nginx.org/HttpFastcgiModule
92 fastcgi_buffer_size 128k;
93 fastcgi_buffers 256 4k;
94 fastcgi_busy_buffers_size 256k;
95 fastcgi_cache_key "$request_method $scheme://$http_host$request_uri";
96 fastcgi_cache_path ${nginx.stateDir}/fastcgi_cache
97 inactive=10m
98 keys_zone=microcache:2M
99 levels=1:2
100 loader_files=100000
101 loader_sleep=1
102 loader_threshold=2592000000
103 max_size=64M;
104 fastcgi_connect_timeout 60;
105 fastcgi_ignore_client_abort off;
106 fastcgi_intercept_errors on;
107 fastcgi_max_temp_file_size 2M;
108 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
109 fastcgi_param SCRIPT_FILENAME $request_filename;
110 fastcgi_temp_path ${nginx.stateDir}/fastcgi_temp 1 2;
111 '';
112 connexion = ''
113 sendfile on;
114 # If the client stops reading data,
115 # free up the stale client connection after this much time.
116 send_timeout 60;
117 # Causes nginx to attempt to send its HTTP response head
118 # in one packet, instead of using partial frames.
119 # This is useful for prepending headers before calling sendfile,
120 # or for throughput optimization.
121 tcp_nopush on;
122 # Don't buffer data-sends (disable Nagle algorithm).
123 # Good for sending frequent small bursts of data in real time.
124 tcp_nodelay on;
125 keepalive_timeout 20;
126 reset_timedout_connection on;
127 server_names_hash_bucket_size 128;
128 types_hash_max_size 2048;
129 '';
130 map = ''
131 # User agents that are to be blocked.
132 #map $http_user_agent $bad_bot {
133 # default 0;
134 # libwww-perl 1;
135 # ~(?i)(httrack|htmlparser|libwww) 1;
136 #}
137 # Referrers that are to be blocked.
138 #map $http_referer $bad_referer {
139 # default 0;
140 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
141 #}
142 #geo $not_local {
143 # default 1;
144 # 127.0.0.1 0;
145 #}
146 '';
147 gzip = ''
148 gzip on;
149 gzip_buffers 16 8k;
150 gzip_comp_level 6;
151 gzip_disable "MSIE [1-6]\.";
152 gzip_http_version 1.1;
153 gzip_min_length 1024;
154 gzip_proxied any;
155 gzip_static on;
156 gzip_vary on;
157 gzip_types application/atom+xml
158 application/javascript
159 application/json
160 application/rss+xml
161 application/vnd.ms-fontobject
162 application/x-font-ttf
163 application/x-javascript
164 application/xml
165 application/xml+rss
166 font/opentype
167 font/truetype
168 image/svg+xml
169 text/css
170 text/javascript
171 text/plain
172 text/x-component
173 text/xml;
174 '';
175 cache = ''
176 client_body_buffer_size 4K;
177 # getconf PAGESIZE
178 # 4096
179 client_body_temp_path ${nginx.stateDir}/client_body_temp 1 2;
180 client_body_timeout 60;
181 client_header_buffer_size 1k;
182 client_header_timeout 60;
183 large_client_header_buffers 4 8k;
184
185 open_file_cache max=200000 inactive=20s;
186 open_file_cache_errors on;
187 open_file_cache_min_uses 2;
188 open_file_cache_valid 30s;
189 '';
190 });
191 appendConfig = ''
192 worker_processes 2;
193 '';
194 virtualHosts."_" = {
195 forceSSL = true;
196 # Convoluted way to load the certificate in the store and using ${networking.domainBase} to find it.
197 # NOTE: no ssl_stapling while the certificate remains self-signed.
198 sslCertificate =
199 pkgs.writeText "cert.self-signed.pem"
200 (readFile ((toPath ../../../sec) + "/openssl/${networking.domainBase}/cert.self-signed.pem"));
201 sslCertificateKey = "/var/lib/nginx/x509/${networking.domainBase}.key.pem";
202 };
203 };
204 };
205 }