zion/modules/webstack.nix

233 lines
7.6 KiB
Nix
Raw Normal View History

2020-08-24 15:07:55 +02:00
# Web services configuration
{ config, pkgs, lib, ... }: {
2019-11-10 23:40:31 +01:00
2021-01-19 00:14:57 +01:00
# Reverse proxy configuration
2019-11-10 23:40:31 +01:00
services.nginx = {
enable = true;
recommendedTlsSettings = true;
recommendedGzipSettings = true;
recommendedProxySettings = true;
recommendedOptimisation = true;
2023-06-08 18:30:16 +02:00
recommendedBrotliSettings = true;
clientMaxBodySize = "0";
2020-08-24 15:07:55 +02:00
sslCiphers =
"ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK:!AES128";
2019-11-10 23:40:31 +01:00
sslProtocols = "TLSv1.2 TLSv1.3";
2019-11-14 00:31:39 +01:00
sslDhparam = "/var/lib/dhparams/nginx.pem";
2019-11-10 23:40:31 +01:00
commonHttpConfig = ''
# Add HSTS header with preloading to HTTPS requests.
2022-10-22 17:18:53 +02:00
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
2019-11-10 23:40:31 +01:00
# Minimize information leaked to other domains
2022-10-23 10:53:09 +02:00
add_header 'Referrer-Policy' 'strict-origin-when-cross-origin';
2019-11-10 23:40:31 +01:00
# Prevent injection of code in other mime types (XSS Attacks)
add_header X-Content-Type-Options nosniff;
2022-10-23 10:53:09 +02:00
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options SAMEORIGIN;
2019-11-10 23:40:31 +01:00
# This might create errors
proxy_cookie_path / "/; secure; HttpOnly; SameSite=strict";
'';
virtualHosts = {
"coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
2021-02-03 03:56:59 +01:00
# Redirect from legacy subdirectory URL to subdomain
locations = {
"/radicale/".return = "301 https://radicale.coolneng.duckdns.org";
"/syncthing/".return = "301 https://sync.coolneng.duckdns.org";
"/gitea/".extraConfig =
2020-08-24 15:07:55 +02:00
"rewrite ^/gitea/(.*)$ https://git.coolneng.duckdns.org/$1 last;";
2021-02-03 03:56:59 +01:00
"/miniflux/".extraConfig =
2020-08-24 15:07:55 +02:00
"rewrite ^/miniflux/(.*)$ https://rss.coolneng.duckdns.org/$1 last;";
# Delegation for Matrix
2022-10-22 17:18:53 +02:00
"/.well-known/" = {
alias = "${../well-known}" + "/";
extraConfig = ''
${config.services.nginx.commonHttpConfig}
default_type application/json;
add_header Access-Control-Allow-Origin * always;
2022-10-22 17:18:53 +02:00
'';
};
2019-11-17 23:25:54 +01:00
};
2020-08-24 15:07:55 +02:00
};
"radicale.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
locations."/" = {
proxyPass = "http://localhost:5232/";
extraConfig = ''
proxy_set_header X-Script-Name /;
proxy_pass_header Authorization;
'';
2019-11-17 18:58:31 +01:00
};
2019-11-10 23:40:31 +01:00
};
2020-08-24 15:07:55 +02:00
"sync.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
2021-02-03 03:56:59 +01:00
locations."/".proxyPass = "http://localhost:8384/";
2020-08-24 15:07:55 +02:00
};
"git.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
2022-10-23 10:53:40 +02:00
locations."/" = {
proxyPass = "http://localhost:3000/";
extraConfig = ''
${config.services.nginx.commonHttpConfig}
# Disable embedding as a frame, except from the same origin
add_header Content-Security-Policy "frame-src git.coolneng.duckdns.org; frame-ancestors git.coolneng.duckdns.org";
'';
};
2020-08-24 15:07:55 +02:00
};
"rss.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
2021-02-03 03:56:59 +01:00
locations."/".proxyPass = "http://localhost:8080/";
2020-08-24 15:07:55 +02:00
};
2020-12-28 18:42:26 +01:00
"matrix.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
listen = [
{
addr = "0.0.0.0";
port = 8448;
ssl = true;
}
{
addr = "0.0.0.0";
port = 443;
ssl = true;
}
];
2022-07-20 16:34:14 +02:00
locations."~ ^(/_matrix|/_synapse/client)" = {
proxyPass = "http://localhost:8008";
extraConfig = ''
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
'';
};
2020-12-28 18:42:26 +01:00
};
"element.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
2021-02-03 03:56:59 +01:00
locations."/".root = pkgs.element-web.override {
conf.default_server_config = {
"m.homeserver"."base_url" = "https://matrix.coolneng.duckdns.org";
"m.identity_server"."base_url" = "https://vector.im";
2020-12-28 18:42:26 +01:00
};
};
};
2021-01-19 00:14:57 +01:00
"wallabag.coolneng.duckdns.org" = {
2021-02-03 05:28:10 +01:00
enableACME = true;
forceSSL = true;
2021-01-19 00:14:57 +01:00
root = "${pkgs.wallabag}/web";
2021-02-03 05:28:10 +01:00
locations = {
"/".tryFiles = "$uri /app.php$is_args$args";
"/assets".root = "${config.environment.variables.WALLABAG_DATA}/web";
"~ ^/app.php(/|$)" = {
fastcgiParams = {
SCRIPT_FILENAME = "${pkgs.wallabag}/web/$fastcgi_script_name";
DOCUMENT_ROOT = "${pkgs.wallabag}/web";
};
extraConfig = ''
fastcgi_pass unix:${config.services.phpfpm.pools.wallabag.socket};
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include ${pkgs.nginx}/conf/fastcgi_params;
internal;
'';
};
2021-01-19 00:14:57 +01:00
};
};
2022-05-02 01:49:40 +02:00
"books.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
locations."/" = {
proxyPass = "http://localhost:9000/";
2023-01-14 20:00:15 +01:00
proxyWebsockets = true;
2022-05-02 01:49:40 +02:00
extraConfig = ''
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
'';
};
};
"grafana.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
locations."/" = {
proxyPass = "http://localhost:9009/";
proxyWebsockets = true;
};
};
2023-04-18 21:18:38 +02:00
"/nginx_status/" = {
listen = [{
addr = "127.0.0.1";
port = 8282;
}];
extraConfig = ''
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
'';
};
2023-05-24 14:15:15 +02:00
"nightscout.coolneng.duckdns.org" = {
enableACME = true;
forceSSL = true;
locations."/" = {
proxyPass = "http://localhost:1337";
extraConfig = ''
proxy_set_header X-Forwarded-For $remote_addr;
'';
};
};
2019-11-10 23:40:31 +01:00
};
};
# ACME certs configuration
security.acme = {
acceptTerms = true;
defaults = {
email = "akasroua@disroot.org";
dnsResolver = "127.0.0.1:53";
};
2019-11-10 23:40:31 +01:00
};
2019-11-14 02:30:12 +01:00
# Generate dhparams
2019-11-14 00:31:39 +01:00
security.dhparams = {
enable = true;
params.nginx.bits = 2048;
2019-11-14 00:31:39 +01:00
};
2019-11-10 23:40:31 +01:00
2019-11-17 18:58:31 +01:00
# PostgreSQL databases configuration
2019-11-16 10:55:10 +01:00
services.postgresql = {
enable = true;
2022-10-23 17:19:26 +02:00
package = pkgs.postgresql_15;
2019-11-16 10:55:10 +01:00
authentication = lib.mkForce ''
2020-08-24 15:07:55 +02:00
# Generated file; do not edit!
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
2019-11-16 10:55:10 +01:00
'';
2022-10-23 17:34:18 +02:00
settings = {
max_connections = "300";
2023-06-20 17:57:41 +02:00
shared_buffers = "1024MB";
2022-10-23 17:34:18 +02:00
};
2019-11-16 10:55:10 +01:00
};
2019-11-14 02:30:12 +01:00
2020-04-17 00:47:17 +02:00
# Restart reverse proxy after services startup
2020-08-24 15:07:55 +02:00
systemd.services.nginx.after = [
"gitea.service"
"syncthing.service"
"miniflux.service"
"radicale.service"
2022-07-20 16:34:14 +02:00
"dendrite.service"
2021-01-19 00:14:57 +01:00
"phpfpm-wallabag.service"
2022-12-12 00:15:47 +01:00
"systemd-tmpfiles-setup.service"
2023-06-08 18:30:16 +02:00
"podman-openbooks.service"
"podman-mqtt2prometheus.service"
"podman-nightscout.service"
2020-08-24 15:07:55 +02:00
];
2019-11-10 23:40:31 +01:00
}