git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #119186 from ncfavier/nginx-reject-ssl

nixos/nginx: add option rejectSSL exposing ssl_reject_handshake

commit709785f944e57a3040dcf9326c6d6373f4a29417 authorLuke Granger-Brown <git@lukegb.com> dateMon 24 May 2021, 15:35 (5 years ago) parent05d9b4d821ca7d
2 files changed+36−14
nixos/modules/services/web-servers/nginx/default.nix+24−14 nixos/modules/services/web-servers/nginx/vhost-options.nix+12
nixos/modules/services/web-servers/nginx/default.nix+24−14
@@ -230,13 +230,13 @@ let
230230
231231 defaultListen =
232232 if vhost.listen != [] then vhost.listen
233− else ((optionals hasSSL (
234− singleton { addr = "0.0.0.0"; port = 443; ssl = true; }
235− ++ optional enableIPv6 { addr = "[::]"; port = 443; ssl = true; }
236− )) ++ optionals (!onlySSL) (
237− singleton { addr = "0.0.0.0"; port = 80; ssl = false; }
238− ++ optional enableIPv6 { addr = "[::]"; port = 80; ssl = false; }
239− ));
233+ else optionals (hasSSL || vhost.rejectSSL) (
234+ singleton { addr = "0.0.0.0"; port = 443; ssl = true; }
235+ ++ optional enableIPv6 { addr = "[::]"; port = 443; ssl = true; }
236+ ) ++ optionals (!onlySSL) (
237+ singleton { addr = "0.0.0.0"; port = 80; ssl = false; }
238+ ++ optional enableIPv6 { addr = "[::]"; port = 80; ssl = false; }
239+ );
240240
241241 hostListen =
242242 if vhost.forceSSL
@@ -303,6 +303,9 @@ let
303303 ${optionalString (hasSSL && vhost.sslTrustedCertificate != null) ''
304304 ssl_trusted_certificate ${vhost.sslTrustedCertificate};
305305 ''}
306+ ${optionalString vhost.rejectSSL ''
307+ ssl_reject_handshake on;
308+ ''}
306309
307310 ${mkBasicAuth vhostName vhost}
308311
@@ -771,20 +774,27 @@ in
771774 }
772775
773776 {
774− assertion = all (conf: with conf;
775− !(addSSL && (onlySSL || enableSSL)) &&
776− !(forceSSL && (onlySSL || enableSSL)) &&
777− !(addSSL && forceSSL)
777+ assertion = all (host: with host;
778+ count id [ addSSL (onlySSL || enableSSL) forceSSL rejectSSL ] <= 1
778779 ) (attrValues virtualHosts);
779780 message = ''
780781 Options services.nginx.service.virtualHosts.<name>.addSSL,
781− services.nginx.virtualHosts.<name>.onlySSL and services.nginx.virtualHosts.<name>.forceSSL
782− are mutually exclusive.
782+ services.nginx.virtualHosts.<name>.onlySSL,
783+ services.nginx.virtualHosts.<name>.forceSSL and
784+ services.nginx.virtualHosts.<name>.rejectSSL are mutually exclusive.
785+ '';
786+ }
787+
788+ {
789+ assertion = any (host: host.rejectSSL) (attrValues virtualHosts) -> versionAtLeast cfg.package.version "1.19.4";
790+ message = ''
791+ services.nginx.virtualHosts.<name>.rejectSSL requires nginx version
792+ 1.19.4 or above; see the documentation for services.nginx.package.
783793 '';
784794 }
785795
786796 {
787− assertion = all (conf: !(conf.enableACME && conf.useACMEHost != null)) (attrValues virtualHosts);
797+ assertion = all (host: !(host.enableACME && host.useACMEHost != null)) (attrValues virtualHosts);
788798 message = ''
789799 Options services.nginx.service.virtualHosts.<name>.enableACME and
790800 services.nginx.virtualHosts.<name>.useACMEHost are mutually exclusive.
nixos/modules/services/web-servers/nginx/vhost-options.nix+12
@@ -118,6 +118,18 @@ with lib;
118118 '';
119119 };
120120
121+ rejectSSL = mkOption {
122+ type = types.bool;
123+ default = false;
124+ description = ''
125+ Whether to listen for and reject all HTTPS connections to this vhost. Useful in
126+ <link linkend="opt-services.nginx.virtualHosts._name_.default">default</link>
127+ server blocks to avoid serving the certificate for another vhost. Uses the
128+ <literal>ssl_reject_handshake</literal> directive available in nginx versions
129+ 1.19.4 and above.
130+ '';
131+ };
132+
121133 sslCertificate = mkOption {
122134 type = types.path;
123135 example = "/var/host.cert";