Hosts file: does it pass the domain name? I’m always redirected to the main site configured on the host

apachednshostsNetwork

My NEW server IP address is 123.456.789.123.

Pointing my browser to 123.456.789.123 (http://123.456.789.123) the server serves me site alpha.

Now, on the same NEW server I've configured a second site: beta.

Site beta is still online served from my OLD server with IP 321.987.654.321.

So, to reinstall and reconfigure it on the NEW server, I edited my hosts file this way, so it calls the NEW server when I go to domain beta.com:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
123.456.789.123 beta.com www.beta.com

The problem is that when I point my browser to http://www.beta.com, I see alpha.com on the New server, the same site I see if I go to http://123.456.789.123 (this is the NEW server).

So, practically, the hosts file correctly points me to the NEW server, but instead of showing me the site beta.com, shows me always the site alpha.com.

On 123.456.789.123, I have two different vhosts file: one for alpha and one for beta.

This is the vhost for alpha:

<VirtualHost *:80>
    ServerName alpha.com
    ServerAlias www.alpha.com

    ServerAdmin my.email@gmail.com
    DocumentRoot /var/www/alpha.com/public_html

    ErrorLog /var/www/alpha.com/log/error.log
    CustomLog /var/www/alpha.com/log/access.log combined

    <Directory /var/www/alpha.com>
        AllowOverride All
    </Directory>
</VirtualHost>

This is the vhost for beta:

<VirtualHost *:80>
    ServerName beta.com
    ServerAlias www.beta.com

    ServerAdmin my.email@gmail.com
    DocumentRoot /var/www/beta.com/public_html

    ErrorLog /var/www/beta.com/log/error.log
    CustomLog /var/www/beta.com/log/access.log combined

    <Directory /var/www/beta.com>
        AllowOverride All
    </Directory>
</VirtualHost>

Any ideas about how can I see the site beta when pointing my browser (and my hosts file) to the NEW server?

It seems that the hosts file simply calls 123.456.789.123, but without passing the domain I'm asking for and so the server responds with the default one and not with the site I'm asking for: beta.com.

Best Answer

Your reasoning about the hosts file is actually not how it works. The hosts file is only responsible for providing a mapping from the hostname (alpha.com) to an IP - it doesn't have any influence on which hostname the browser sends to the webserver for virtual hosting.

If you do actually end up on the new server, you have an error in your web server configuration so that virtual hosting is either not enabled or misconfigured. Depending on the version of Apache you're running, you might for example need to add a "NameVirtualHost *:80" line to the configuration. It could also be that you have a typo in the ServerName and/or ServerAlias lines. It is impossible to tell from your question, as you haven't provided the actual configuration.

If you instead end up on the old server, the problem is that your browser is not using the hosts file (i.e. it is not using the system default name resolver). An easy way to get around that is simply to use a different browser. You might also be running into caching if you have made a lot of changes and tests with the hosts file. A very easy manner of testing that is simply to reboot the Mac. You can also flush the cache, which you can find instructions on how to here on AskDifferent depending on your macOS version.