The Name-based Virtual Host Support explains the matching order:
When a request arrives, the server will find the best (most specific) matching <VirtualHost>
argument based on the IP address and port used by the request. If there is more than one virtual host containing this best-match address and port combination, Apache will further compare the ServerName
and ServerAlias
directives to the server name present in the request.
Typically, there are ports serving both HTTP and HTTPS sites. (This day and age the HTTP is mostly used for redirections to the HTTPS.) TLS must either be enabled or disabled on a port; a single port cannot serve both TLS and non-TLS requests. That is one concrete reason to use:
<VirtualHost *:8123>
ServerName www.example.com
</VirtualHost>
The ServerName
does have optional scheme and port:
Syntax: ServerName [scheme://]domain-name|ip-address[:port]
However, that is for a specific situation:
Sometimes, the server runs behind a device that processes SSL, such as a reverse proxy, load balancer or SSL offload appliance. When this is the case, specify the https://
scheme and the port number to which the clients connect in the ServerName
directive to make sure that the server generates the correct self-referential URLs.