41a6c3
--- a/server/vhost.c	2013/07/15 11:50:50	1503188
41a6c3
+++ b/server/vhost.c	2013/10/03 18:31:22	1528958
41a6c3
@@ -577,14 +577,22 @@
41a6c3
      */
41a6c3
 
41a6c3
     for (s = main_s->next; s; s = s->next) {
41a6c3
+        server_addr_rec *sar_prev = NULL;
41a6c3
         has_default_vhost_addr = 0;
41a6c3
         for (sar = s->addrs; sar; sar = sar->next) {
41a6c3
             ipaddr_chain *ic;
41a6c3
             char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
41a6c3
-
41a6c3
+            /* XXX: this treats 0.0.0.0 as a "default" server which matches no-exact-match for IPv6 */
41a6c3
             if (!memcmp(sar->host_addr->ipaddr_ptr, inaddr_any, sar->host_addr->ipaddr_len)) {
41a6c3
                 ic = find_default_server(sar->host_port);
41a6c3
-                if (!ic || sar->host_port != ic->sar->host_port) {
41a6c3
+
41a6c3
+                if (ic && sar->host_port == ic->sar->host_port) { /* we're a match for an existing "default server"  */
41a6c3
+                    if (!sar_prev || memcmp(sar_prev->host_addr->ipaddr_ptr, inaddr_any, sar_prev->host_addr->ipaddr_len)
41a6c3
+                                  || sar_prev->host_port != sar->host_port) { 
41a6c3
+                        add_name_vhost_config(p, main_s, s, sar, ic);
41a6c3
+                    }
41a6c3
+                }
41a6c3
+                else { 
41a6c3
                     /* No default server, or we found a default server but
41a6c3
                     ** exactly one of us is a wildcard port, which means we want
41a6c3
                     ** two ip-based vhosts not an NVH with two names
41a6c3
@@ -592,6 +600,7 @@
41a6c3
                     ic = new_ipaddr_chain(p, s, sar);
41a6c3
                     ic->next = default_list;
41a6c3
                     default_list = ic;
41a6c3
+                    add_name_vhost_config(p, main_s, s, sar, ic);
41a6c3
                 }
41a6c3
                 has_default_vhost_addr = 1;
41a6c3
             }
41a6c3
@@ -609,8 +618,9 @@
41a6c3
                     ic->next = *iphash_table_tail[bucket];
41a6c3
                     *iphash_table_tail[bucket] = ic;
41a6c3
                 }
41a6c3
+                add_name_vhost_config(p, main_s, s, sar, ic);
41a6c3
             }
41a6c3
-            add_name_vhost_config(p, main_s, s, sar, ic);
41a6c3
+            sar_prev = sar;
41a6c3
         }
41a6c3
 
41a6c3
         /* Ok now we want to set up a server_hostname if the user was