-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx.nix
77 lines (67 loc) · 1.97 KB
/
nginx.nix
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
{config, ...}:
let
allowHttps = true;
serverTemplate = {
domain,
proxyTarget,
redirectWww ? false,
enableHttps ? false
}: let
wwwAlias = if redirectWww then "www.${domain}" else "";
proxyConfig = ''
location / {
proxy_pass ${proxyTarget};
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
'';
httpChallengeConfig = ''
location /.well-known/acme-challenge {
root /var/www/challenges;
}
location / {
return 301 https://${domain}$request_uri;
}
'';
secureServer = ''
server {
server_name ${domain};
listen 443 ssl;
ssl_certificate ${config.security.acme.directory}/${domain}/fullchain.pem;
ssl_certificate_key ${config.security.acme.directory}/${domain}/key.pem;
${proxyConfig}
}
'';
in ''
server {
server_name ${domain} ${wwwAlias};
listen 80;
listen [::]:80;
${if enableHttps then httpChallengeConfig else proxyConfig}
}
${if enableHttps then secureServer else ""}
'';
in {
networking.hostName = "backbonecabal";
networking.firewall.allowedTCPPorts = [80 443];
services.httpd.enable = true;
services.httpd.adminAddr = "[email protected]";
services.httpd.documentRoot = ./static;
services.httpd.port = 8080;
services.nginx.enable = true;
services.nginx.httpConfig =
serverTemplate {
domain = "backbonecabal.com";
redirectWww = true;
proxyTarget = "http://127.0.0.1:8080";
enableHttps = allowHttps;
};
} // (if allowHttps then {
security.acme.certs."backbonecabal.com" = {
webroot = "/var/www/challenges";
email = "[email protected]";
postRun = "systemctl reload nginx.service";
};
} else {})