0

I'm confused as to the difference between putting the port in the VirtualHost vs the HostName directives.

What is the difference between:

<VirtualHost *:8123>
    ServerName www.example.com
</VirtualHost>

and

<VirtualHost *:*>
    ServerName www.example.com:8123
</VirtualHost>

?

1 Answer 1

2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .