summaryrefslogtreecommitdiff
path: root/custom/ymir-nginx.nix
blob: bc4e0a787708d2b3f6f2b5bdd5ffdd12fa5cd03c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
{ config, lib, pkgs, ... }:

let
  uwsgi_params = builtins.toFile "uwsgi_param" ''
    uwsgi_param QUERY_STRING $query_string;
    uwsgi_param REQUEST_METHOD $request_method;
    uwsgi_param CONTENT_TYPE $content_type;
    uwsgi_param CONTENT_LENGTH $content_length;
    uwsgi_param REQUEST_URI $request_uri;
    uwsgi_param PATH_INFO $document_uri;
    uwsgi_param DOCUMENT_ROOT $document_root;
    uwsgi_param SERVER_PROTOCOL $server_protocol;
    uwsgi_param REMOTE_ADDR $remote_addr;
    uwsgi_param REMOTE_PORT $remote_port;
    uwsgi_param SERVER_ADDR $server_addr;
    uwsgi_param SERVER_PORT $server_port;
    uwsgi_param SERVER_NAME $server_name;
  '';

  favicon = builtins.toFile "favicon" ''
    location = /favicon.ico {
      root /srv/www/default;
    }
  '';

  acme = builtins.toFile "acme" ''
    location /.well-known/acme-challenge {
      root /srv/www/acme/;
    }
  '';

  mail-autoconfig = pkgs.writeTextFile {
    name = "mail-autoconfig";
    destination = "/config-v1.1.xml";
    text = ''
      <?xml version="1.0" encoding="UTF-8"?>
      <clientConfig version="1.1">
        <emailProvider id="yggdrasil.li">
          <domain>141.li</domain>
          <domain>xmpp.li</domain>
          <domain>yggdrasil.li</domain>
          <domain>praseodym.org</domain>
          <domain>kleen.li</domain>
          <domain>nights.email</domain>
          <displayName>Yggdrasil</displayName>
          <displayShortName>Yggdrasil</displayShortName>
          <incomingServer type="imap">
            <hostname>ymir.yggdrasil.li</hostname>
            <port>143</port>
            <socketType>STARTTLS</socketType>
            <authentication>password-cleartext</authentication>
            <username>%EMAILLOCALPART%</username>
          </incomingServer>
          <outgoingServer type="smtp">
            <hostname>ymir.yggdrasil.li</hostname>
            <port>25</port>
            <socketType>STARTTLS</socketType>
            <authentication>password-cleartext</authentication>
            <username>%EMAILLOCALPART%</username>
          </outgoingServer>
          <incomingServer type="imap">
            <hostname>ymir.yggdrasil.li</hostname>
            <port>993</port>
            <socketType>SSL</socketType>
            <authentication>password-cleartext</authentication>
            <username>%EMAILLOCALPART%</username>
          </incomingServer>
        </emailProvider>
      </clientConfig>
    '';
  };
in {
  services.logrotate = {
    enable = true;
    extraConfig = ''
      /var/spool/nginx/logs/* {
        compress
        daily
        missingok
        rotate 4
        size 1M
        su ${config.services.nginx.user} ${config.services.nginx.group}
      }
    '';
  };

  systemd.services.nginx.serviceConfig = {
    ReadWritePaths = "/srv/ftp";
  };

  services.nginx = {
    enable = true;

    recommendedOptimisation = true;
    sslCiphers = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
    sslDhparam = config.security.dhparams.params.nginx.path;
    recommendedTlsSettings = true;
    recommendedGzipSettings = true;
    recommendedProxySettings = true;
    

    commonHttpConfig = ''
      log_format main
              '$remote_addr "$remote_user" '
              '"$host" "$request" $status $bytes_sent '
              '"$http_referer" "$http_user_agent" '
              '$gzip_ratio';

      client_header_timeout 10m;
      client_body_timeout 10m;
      send_timeout 10m;

      connection_pool_size 256;
      client_header_buffer_size 1k;
      large_client_header_buffers 4 2k;
      request_pool_size 4k;

      output_buffers 1 32k;
      postpone_output 1460;

      ignore_invalid_headers on;

      access_log syslog:server=unix:/dev/log main;
      error_log syslog:server=unix:/dev/log info;

      ssl_certificate /var/lib/acme/yggdrasil.li/fullchain.pem;
      ssl_certificate_key /var/lib/acme/yggdrasil.li/key.pem;

      server {
        listen *:443 ssl;
        listen [::]:443 ssl;
        server_name _;

        include ${favicon};
        include ${acme};

        location /.well-known/autoconfig/mail/ {
          alias ${mail-autoconfig};
        }

        root /srv/www/default;
      }

      server {
        listen *:80;
        listen [::]:80;
        server_name _;

        include ${acme};

        location / {
          return 301 https://$host$request_uri;
        }
      }
    '';

    appendHttpConfig = ''
      server {
        listen *:443 ssl;
        listen [::]:443 ssl;
        server_name ~^(.*\.)?(f|files)\.(yggdrasil\.li|141\.li|praseodym\.org)$;

        include ${favicon};
        include ${acme};

        root /srv/www/files;
      }

      server {
        listen *:443 ssl;
        listen [::]:443 ssl;
        server_name ~^(.*\.)?(o|online)\.(yggdrasil\.li|141\.li|praseodym\.org)$;

        include ${favicon};
        include ${acme};

        location / {
          add_header X-NetworkManager-Status online;
          add_header Cache-Control "max-age=0, must-revalidate";
          return 204;
        }
      }

      server {
        listen *:443 ssl;
        listen [::]:443 ssl;
        server_name ~^(.*\.)?dirty-haskell\.org$;

        include ${favicon};
        include ${acme};

        root /srv/www/dirty-haskell.org;
      }

      server {
        listen *:443 ssl;
        listen [::]:443 ssl;
        server_name ~^(.*\.)?git\.yggdrasil\.li$;

        root ${pkgs.cgit}/cgit;

        try_files $uri @cgit;

        include ${favicon};
        include ${acme};

        location @cgit {
          include ${uwsgi_params};
          uwsgi_pass unix:/run/git.yggdrasil.li.sock;
          uwsgi_modifier1 9;
        }
      }
      
      server {
        listen *:443 ssl;
        listen [::]:443 ssl;
        server_name ~^(.*\.)?git\.rheperire\.org$;

        root ${pkgs.cgit}/cgit;

        try_files $uri @cgit;

        include ${favicon};
        include ${acme};

        location @cgit {
          include ${uwsgi_params};
          uwsgi_pass unix:/run/git.rheperire.org.sock;
          uwsgi_modifier1 9;
        }
      }

      server {
        listen *:443 ssl;
        listen [::]:443 ssl;
        server_name ~^(.*\.)?odin\.(yggdrasil\.li|141\.li)$;

        location / {
          auth_basic "Reverse proxy to odin";
          auth_basic_user_file /srv/www/odin/htpasswd;

          set $upstream http://odin.asgard.yggdrasil/;
          proxy_pass $upstream;
        }
      }

      server {
        listen *:443 ssl;
        listen [::]:443 ssl;

        server_name ~^ftp\.(yggdrasil\.li|141\.li|praseodym\.org)$;

        client_body_temp_path /run/nginx/webdav;

        location / {
          root /srv/ftp/$remote_user;
          autoindex on;
        
          auth_basic "FTP over WebDAV";
          auth_basic_user_file /srv/ftp.htpasswd;

          dav_methods PUT DELETE MKCOL COPY MOVE;
          create_full_put_path on;
          dav_access user:rw group:r all:r;
        }
      }
    '';
  };
}