I was trying to have apache configured to have a default VirtualHost that would reply to any request that doesn't match any other VirtualHost, e.g when ip is typed in browser.
While trying a configuration like below, mixing <VirtualHost>
formats like *:443
but also hostname:443
and I got unexpected results, e.g accessing s1.mysite.com
matched the one from #2 backup.mysite.com
, after some tests I concluded that these match properly only when same format is used for all, e.g <VirtualHost *:443>
, I am just not sure if the unexpected results in below config are caused by apache not being able to properly compare and match the right one unless same format is used, OR the unexpected results can come up because the hostname before the IP can have various IPs values (ipv4, ipv6, etc) and hostname aliasses, causing redirects between hosts like localhost, mysite.com, etc causing unreliable matches.
Honestly the default VirtualHost documentation seems to address these issues but I can't seem to fully understand them.
So this does NOT work as expected, accessing https://s1.mysite.com
can match the vhost 2 (backup.mysite.com
).
# trying to define a default one first
# I was trying to make this match even if server's ip is entered in browser
<VirtualHost *:443>
ServerName s1.mysite.com
</VirtualHost>
# vhost 2
<VirtualHost backup.mysite.com:443>
ServerName backup.mysite.com
ServerAlias files.mysite.com
ServerAlias ftp.mysite.com
</VirtualHost>
# vhost 3
<VirtualHost store.mysite.com:443>
ServerName store.mysite.com
ServerAlias shop.mysite.com
ServerAlias buy.mysite.com
</VirtualHost>
This works as expected (when all are *:443):
# trying to define a default one first
# I was trying to make this match even if server's ip is entered in browser
<VirtualHost *:443>
ServerName s1.mysite.com
</VirtualHost>
# vhost 2
<VirtualHost *:443>
ServerName backup.mysite.com
ServerAlias files.mysite.com
ServerAlias ftp.mysite.com
</VirtualHost>
# vhost 3
<VirtualHost *:443>
ServerName store.mysite.com
ServerAlias shop.mysite.com
ServerAlias buy.mysite.com
</VirtualHost>