1 {pkgs, lib, config, system, ...}:
2 let inherit (lib) types;
3 inherit (config.services) nginx x509;
4 logDir = "/var/log/nginx";
5 domainDir = dom: lib.concatStringsSep "/" (lib.reverseList (lib.splitString "." dom));
11 options.services.nginx.webDir = lib.mkOption {
13 default = "/var/www"; # TODO: /var/lib/nginx ?
24 stateDir = "/dev/shm/nginx";
28 worker_connections 1024;
30 clientMaxBodySize = "20m";
31 recommendedProxySettings = true;
32 recommendedTlsSettings = true;
34 sslCiphers = "HIGH:!ADH:!MD5:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
35 #sslCiphers = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL;";
36 #sslProtocols = "TLSv1.2";
38 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
39 '$status $body_bytes_sent "$http_referer" '
40 '"$http_user_agent" "$http_x_forwarded_for"';
43 lib.concatStringsSep "\n" (lib.attrValues {
45 default_type application/octet-stream;
50 #ssl_certificate ${x509.cert};
51 #ssl_certificate_key ${x509.key};
54 access_log ${logDir}/access.log main buffer=32k;
55 error_log ${logDir}/error.log warn;
56 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
59 proxy_cache_use_stale updating;
60 proxy_temp_path ${nginx.stateDir}/proxy_temp 1 2;
63 # DOC: http://wiki.nginx.org/HttpFastcgiModule
64 fastcgi_buffer_size 128k;
65 fastcgi_buffers 256 4k;
66 fastcgi_busy_buffers_size 256k;
67 fastcgi_cache_key "$request_method $scheme://$http_host$request_uri";
68 fastcgi_cache_path ${nginx.stateDir}/fastcgi_cache
70 keys_zone=microcache:2M
74 loader_threshold=2592000000
76 fastcgi_connect_timeout 60;
77 fastcgi_ignore_client_abort off;
78 fastcgi_intercept_errors on;
79 fastcgi_max_temp_file_size 2M;
80 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
81 fastcgi_param SCRIPT_FILENAME $request_filename;
82 fastcgi_temp_path ${nginx.stateDir}/fastcgi_temp 1 2;
87 # ^ If the client stops reading data,
88 # free up the stale client connection after this much time.
90 # ^ Causes nginx to attempt to send its HTTP response head
91 # in one packet, instead of using partial frames.
92 # This is useful for prepending headers before calling sendfile,
93 # or for throughput optimization.
95 # ^ Don't buffer data-sends (disable Nagle algorithm).
96 # Good for sending frequent small bursts of data in real time.
98 reset_timedout_connection on;
99 server_names_hash_bucket_size 128;
100 types_hash_max_size 2048;
103 # User agents that are to be blocked.
104 #map $http_user_agent $bad_bot {
107 # ~(?i)(httrack|htmlparser|libwww) 1;
109 # Referrers that are to be blocked.
110 #map $http_referer $bad_referer {
112 # ~(?i)(babes|casino|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|replica|sex|teen|webcam|zippo) 1;
123 gzip_disable "MSIE [1-6]\.";
124 gzip_http_version 1.1;
125 gzip_min_length 1024;
129 gzip_types application/atom+xml
130 application/javascript
133 application/vnd.ms-fontobject
134 application/x-font-ttf
135 application/x-javascript
148 client_body_buffer_size 4K;
151 client_body_temp_path ${nginx.stateDir}/client_body_temp 1 2;
152 client_body_timeout 60;
153 client_header_buffer_size 1k;
154 client_header_timeout 60;
155 large_client_header_buffers 4 8k;
157 open_file_cache max=200000 inactive=20s;
158 open_file_cache_errors on;
159 open_file_cache_min_uses 2;
160 open_file_cache_valid 30s;