How to test VirtualHost
Apache and other web server daemons like nginx and lighttpd are capable of serving multiple websites with different domains from the same server. Sites hosted in this manner are called "virtual hosts".
The most common type of virtual host is a "name-based virtual host", where the virtual hosts share the same IP address. A web server decides which name-based virtual host to serve up based on the domain name in the URL the visitor used to get there.
Virtual hosts and domains
A name-based virtual host depends on the browser visiting a particular domain, so testing that virtual host can be hard if the domain's DNS doesn't point to the server. If you haven't purchased the domain yet, or if the domain has to continue pointing to another server while you test, for simple testing you might use just the server's IP address to test the site or create a separate subdomain as a temporary measure.
However, there might be occasions when you want to demonstrate the website to your clients as the way it would look as a finished product. You might also need to test a new website or web application using the same domain as it would use in production - otherwise there could be discrepancies between the production and testing websites, and things may go wrong when you switch the website to your production environment.
The hosts file
There is a way to trick a computer into thinking a domain exists before you create DNS records for the domain, or to change the DNS results it would get for an existing domain without needing to change public DNS records.
To do that, all you need to do is to append a line to /etc/hosts in Linux or C:\Windows\System32\drivers\etc\hosts in Windows or /private/etc/hosts if you have an Apple Mac in the format of “IP + <blank space> + Domain”, like this:
220.127.116.11 is the IP address of www.rackspace.com but in this example, we set a non-exist domain www.rackspace-example.com. When done, you can visit www.rackspace-example.com and get the contents of 18.104.22.168.
Please note that this trick works only on the computer where you modified the hosts file. The browser you use to test and the modified hosts file have to be on the same system.
© 2014 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
See license specifics and DISCLAIMER