nixos/modules/services/reverseproxy.nix

216 lines
6.9 KiB
Nix
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
lib,
config,
...
}:
let
cfg = config.crans.reverseProxy;
mainTld = "org";
otherTld = [
"fr"
"eu"
];
inherit (lib)
mkEnableOption
mkIf
mkOption
types
;
mkHostName = als: tld: if als == "_crans-org" then "crans.${tld}" else "${als}.crans.${tld}";
in
{
options.crans.reverseProxy = {
enable = mkEnableOption "Configuration du reverseproxy.";
virtualHosts = mkOption {
type = types.attrsOf (types.submodule ../info/reverseproxyVhost.nix);
default = { };
example = {
"framadate" = {
host = "176.16.10.128:8000";
aliases = [
"everything"
"voyager"
];
};
};
description = "Déclaration des machines.";
};
};
config = mkIf cfg.enable {
systemd.services = lib.mapAttrs (vhostName: vhostConfig: {
wantedBy = [ "multi-user.target" ];
}) cfg.virtualHosts;
# On fait un certificat commun pour tous les hosts
security.acme.certs."all.crans.all" = {
domain = "*.crans.${mainTld}";
dnsProvider = "rfc2136";
# Contient le serveur à contacter avec le protocole
# et le mot de passe
environmentFile = config.age.secrets.acme-env.path;
extraDomainNames =
lib.concatMap (tld: [
"*.crans.${tld}"
"crans.${tld}"
]) otherTld
++ [
"crans.${mainTld}"
];
group = config.services.nginx.group;
};
# Création des dossiers de logs
system.activationScripts = {
nginx-logs = {
text =
lib.concatMapStrings (f: "mkdir -p /var/log/nginx/${f}/\n") (
[ "anubis" ] ++ (lib.map (hn: "host/${hn}") (builtins.attrNames cfg.virtualHosts))
)
+ "chown -R ${config.services.nginx.user}:${config.services.nginx.group} /var/log/nginx/\n";
};
};
# On active Anubis
crans.anubis = {
enable = true;
useRedis = true;
instances = lib.mapAttrs (vhostName: vhostConfig: {
hostname = mkHostName vhostName mainTld;
bind = "/run/anubis/anubis-${vhostName}/socket.sock";
bots = vhostConfig.anubisConfig;
}) (lib.filterAttrs (_: c: c.anubisConfig != null) cfg.virtualHosts);
};
services = {
logrotate.settings =
let
# https://github.com/NixOS/nixpkgs/blob/nixos-26.05/nixos/modules/services/web-servers/nginx/default.nix
base = {
su = "${config.services.nginx.user} ${config.services.nginx.group}";
postrotate = "[ ! -f /var/run/nginx/nginx.pid ] || kill -USR1 `cat /var/run/nginx/nginx.pid`";
frequency = "daily";
ifempty = true;
compress = true;
delaycompress = true;
dateyesterday = true;
};
in
{
nginx-anubis = base // {
files = [ "/var/log/nginx/anubis/*.log" ];
rotate = 7; # une semaine de log pour débugguer
};
nginx-hosts = base // {
files = [ "/var/log/nginx/host/*/*.log" ];
rotate = 173; # 6 mois de logs
dateext = true;
dateformat = "%Y-%m-%d";
};
};
nginx =
let
configVhost =
vhostName: vhostConfig:
let
hostname = mkHostName vhostName mainTld;
sslConf = {
forceSSL = !vhostConfig.httpOnly;
rejectSSL = vhostConfig.httpOnly;
useACMEHost = mkIf (!vhostConfig.httpOnly) "all.crans.all";
acmeRoot = null;
};
logHostConf = ''
access_log /var/log/nginx/host/${vhostName}/access.log;
error_log /var/log/nginx/host/${vhostName}/error.log;
'';
# Les alias : vhostName × otherTld U aliases × allTld
aliases = lib.foldr (
tld: acc:
acc
++ (lib.foldr (alias: acc: acc ++ [ (mkHostName alias tld) ]) [
(mkHostName vhostName tld)
] vhostConfig.aliases)
) (lib.foldr (alias: acc: acc ++ [ (mkHostName alias mainTld) ]) [ ] vhostConfig.aliases) otherTld;
in
{
# Configuration du service à proxy.
"${vhostName}" = sslConf // {
serverName = hostname;
# Site statiques et alias
root = vhostConfig.serveLocalFiles;
globalRedirect = vhostConfig.globalRedirect;
locations = {
# Entrée site, via proxy
"/" = mkIf (vhostConfig.proxyPass != null) {
proxyPass = "http://${vhostConfig.proxyPass}";
proxyWebsockets = vhostConfig.proxyWebsockets;
extraConfig = lib.optionalString (vhostConfig.anubisConfig != null) ''
auth_request /.within.website/x/cmd/anubis/api/check;
error_page 401 = @redirectToAnubis;
'';
};
# Anubis
"/.within.website/" = mkIf (vhostConfig.anubisConfig != null) {
# https://anubis.techaro.lol/docs/admin/configuration/subrequest-auth/
proxyPass = "http://unix:/run/anubis/anubis-${vhostName}/socket.sock";
extraConfig = ''
proxy_pass_request_body off;
proxy_set_header content-length "";
add_header 'X-Robots-Tag' 'noindex,nofollow' always;
auth_request off;
access_log /var/log/nginx/anubis/access.log;
error_log /var/log/nginx/anubis/error.log;
'';
};
"@redirectToAnubis" = mkIf (vhostConfig.anubisConfig != null) {
extraConfig = ''
return 307 /.within.website/?redir=$scheme://$host$request_uri;
auth_request off;
access_log /var/log/nginx/anubis/access.log;
error_log /var/log/nginx/anubis/error.log;
'';
};
};
extraConfig = ''
set_real_ip_from 172.16.0.0/16;
set_real_ip_from fd00::/56;
real_ip_header X-Real-Ip;
''
+ logHostConf;
};
}
# Les alias
// lib.mergeAttrsList (
lib.map (alias: {
"${vhostName}-alias-${alias}" = sslConf // {
serverName = alias;
globalRedirect = lib.defaultTo hostname vhostConfig.globalRedirect;
extraConfig = logHostConf;
};
}) aliases
);
in
{
enable = true;
virtualHosts = lib.concatMapAttrs configVhost cfg.virtualHosts;
};
};
};
}