about summary refs log tree commit diff
path: root/modules/nginx.nix
blob: 6cb47b424c786ff63eaafd5a0f4028a55c309cc1 (plain) (blame)
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
{
  config,
  lib,
  pkgs,
  this,
  ...
}:
with lib;
let
  cfg = config.nixfiles.modules.nginx;
in
{
  options.nixfiles.modules.nginx = {
    enable = mkEnableOption "Nginx";

    upstreams = mkOption {
      description = "Defines a group of servers to use as proxy target.";
      type = with types; anything;
      default = null;
    };

    virtualHosts = mkOption {
      description = "Attrset of virtual hosts.";
      type = with types; anything;
      default = null;
    };
  };

  config = mkIf cfg.enable {
    _module.args.libNginx.config = {
      internalOnly = ''
        add_header X-Robots-Tag "noindex, nofollow, noarchive, nosnippet";
        access_log off;
        if ($internal != 1) {
          return 403;
        }
      '';
      appendHead = text: ''
        brotli off;
        gzip off;
        zstd off;
        sub_filter '</head>' '${lib.concatStrings text}</head>';
        sub_filter_once on;
      '';
      noProxyBuffering = ''
        proxy_buffering off;
        proxy_cache off;
      '';
    };

    services = {
      nginx = {
        enable = true;

        enableReload = true;

        package = pkgs.nginxMainline;

        statusPage = mkDefault true;

        recommendedOptimisation = mkDefault true;
        recommendedProxySettings = mkDefault true;
        recommendedTlsSettings = mkDefault true;

        recommendedBrotliSettings = mkDefault true;
        recommendedGzipSettings = mkDefault true;
        recommendedZstdSettings = mkDefault true;

        resolver.addresses =
          let
            isIPv6 = addr: builtins.match ".*:.*:.*" addr != null;
            escapeIPv6 = addr: if isIPv6 addr then "[${addr}]" else addr;
            resolvers =
              if config.networking.nameservers != [ ] then
                config.networking.nameservers
              else
                dns.const.quad9.default;
          in
          map escapeIPv6 resolvers;

        commonHttpConfig = concatStrings [
          ''
            access_log syslog:server=unix:/dev/log;
          ''
          (optionalString (hasAttr "wireguard" this) (
            with config.nixfiles.modules.wireguard;
            ''
              geo $internal {
                default 0;
                127.0.0.1/32 1;
                ::1/128 1;
                ${ipv4.subnet} 1;
                ${ipv6.subnet} 1;
              }
            ''
          ))
        ];

        inherit (cfg) upstreams;

        virtualHosts =
          {
            default = {
              default = true;
              rejectSSL = true;
              locations."/".return = "444";
            };
          }
          // (mkIf (cfg.virtualHosts != null) (
            mapAttrs (
              _: attr:
              mkMerge [
                attr
                (mkIf config.nixfiles.modules.acme.enable {
                  enableACME = mkDefault true;
                  forceSSL = mkDefault true;
                })
              ]
            ) cfg.virtualHosts
          ));

        sslDhparam = config.security.dhparams.params.nginx.path;
      };

      fail2ban.jails = {
        nginx-http-auth.enabled = true;
        nginx-botsearch.enabled = true;
      };

      prometheus.exporters.nginx = {
        enable = true;
        listenAddress = mkDefault this.wireguard.ipv4.address;
        port = mkDefault 9113;
      };
    };

    security.dhparams = {
      enable = true;
      params.nginx = { };
    };

    networking.firewall.allowedTCPPorts = [
      80
      443
    ];
  };
}

Consider giving Nix/NixOS a try! <3