@@ -16,7 +16,17 @@ let
1616
1717 phpMajorVersion = head (splitString "." php.version);
1818
19− getPort = cfg: if cfg.port != 0 then cfg.port else if cfg.enableSSL then 443 else 80;
19+ defaultListen = cfg: if cfg.enableSSL
20+ then [{ip = "*"; port = 443;}]
21+ else [{ip = "*"; port = 80;}];
22+
23+ getListen = cfg:
24+ let list = (lib.optional (cfg.port != 0) {ip = "*"; port = cfg.port;}) ++ cfg.listen;
25+ in if list == []
26+ then defaultListen cfg
27+ else list;
28+
29+ listenToString = l: "${l.ip}:${toString l.port}";
2030
2131 extraModules = attrByPath ["extraModules"] [] mainCfg;
2232 extraForeignModules = filter isAttrs extraModules;
@@ -25,10 +35,13 @@ let
2535
2636 makeServerInfo = cfg: {
2737 # Canonical name must not include a trailing slash.
28− canonicalName =
29− (if cfg.enableSSL then "https" else "http") + "://" +
30− cfg.hostName +
31− (if getPort cfg != (if cfg.enableSSL then 443 else 80) then ":${toString (getPort cfg)}" else "");
38+ canonicalNames =
39+ let defaultPort = (head (defaultListen cfg)).port; in
40+ map (port:
41+ (if cfg.enableSSL then "https" else "http") + "://" +
42+ cfg.hostName +
43+ (if port != defaultPort then ":${toString port}" else "")
44+ ) (map (x: x.port) (getListen cfg));
3245
3346 # Admin address: inherit from the main server if not specified for
3447 # a virtual host.
@@ -224,7 +237,7 @@ let
224237 ++ (map (svc: svc.robotsEntries) subservices)));
225238
226239 in ''
227− ServerName ${serverInfo.canonicalName}
240+ ${concatStringsSep "\n" (map (n: "ServerName ${n}") serverInfo.canonicalNames)}
228241
229242 ${concatMapStrings (alias: "ServerAlias ${alias}\n") cfg.serverAliases}
230243
@@ -326,9 +339,10 @@ let
326339 </IfModule>
327340
328341 ${let
329− ports = map getPort allHosts;
330− uniquePorts = uniqList {inputList = ports;};
331− in concatMapStrings (port: "Listen ${toString port}\n") uniquePorts
342+ listen = concatMap getListen allHosts;
343+ toStr = listen: "Listen ${listenToString listen}\n";
344+ uniqueListen = uniqList {inputList = map toStr listen;};
345+ in concatStrings uniqueListen
332346 }
333347
334348 User ${mainCfg.user}
@@ -382,15 +396,15 @@ let
382396
383397 # Always enable virtual hosts; it doesn't seem to hurt.
384398 ${let
385− ports = map getPort allHosts;
386− uniquePorts = uniqList {inputList = ports;};
387− directives = concatMapStrings (port: "NameVirtualHost *:${toString port}\n") uniquePorts;
399+ listen = concatMap getListen allHosts;
400+ uniqueListen = uniqList {inputList = listen;};
401+ directives = concatMapStrings (listen: "NameVirtualHost ${listenToString listen}\n") uniqueListen;
388402 in optionalString (!version24) directives
389403 }
390404
391405 ${let
392406 makeVirtualHost = vhost: ''
393− <VirtualHost *:${toString (getPort vhost)}>
407+ <VirtualHost ${concatStringsSep " " (map listenToString (getListen vhost))}>
394408 ${perServerConf false vhost}
395409 </VirtualHost>
396410 '';
@@ -628,6 +642,8 @@ in
628642 message = "SSL is enabled for httpd, but sslServerCert and/or sslServerKey haven't been specified."; }
629643 ];
630644
645+ warnings = map (cfg: ''apache-httpd's port option is deprecated. Use listen = [{/*ip = "*"; */ port = ${toString cfg.port}";}]; instead'' ) (lib.filter (cfg: cfg.port != 0) allHosts);
646+
631647 users.extraUsers = optionalAttrs (mainCfg.user == "wwwrun") (singleton
632648 { name = "wwwrun";
633649 group = mainCfg.group;
@@ -712,5 +728,4 @@ in
712728 };
713729
714730 };
715−
716731}