How To Use Hosts File - A Scenario

Domain Name

 
Instead of accessing our machines via IP address, we use a user-friendly name. This name + IP mapping is done in Domain Name System. These user-friendly names are called domains. For example, www.google.com, www.c-sharpcorner.com. 
 

Domain Name to IP Resolution

 
Domain Name to IP address mapping is added in DNS (Domain Name System). When we try to access a host by hostname or domain name, the name is resolved to IP address using DNS Servers. 
 
NsLookup
 
NSLookup stands for Name Server Lookup. It can be used to find IP address against the host/domain name or domain name by IP address. Finding the domain name against an IP address is called Reverse DNS Lookup. It is a very useful command-line utility to resolve DNS issues. Here is an example of how to use it on command prompt.
 
nslookup learninginurdu.pk 
 

What is the hosts file?

 
It is a plain text file. Operating System uses this file to map the hostname to IP address. When we try to access a host by host/domain name, OS first checks if there is an entry for this in the hosts file or not. If it finds a mapping, it uses this mapped IP address for communication with the host machine. Here is an example of how we define entries in this file
10.100.10.101 learninginurdu.pk
 
Location of hosts file on windows: %SystemRoot%\System32\drivers\etc\host
 
Location of hosts file on Linux: /etc/hosts
 
Let see how we can use hosts file in a load balancer scenario to access website from specific server.
 

Case Study

 
We have a website which was deployed on a single web server earlier. For scalability purposes, we have deployed it on multiple web servers. The request doesn’t go directly to the web servers now. Accessibility is through Load Balancer now and the domain of the website is mapped with IP address of load balancer server. The request goes to load balancer and it forwards requests to specific web server considering the load of web servers.
 
Here is a sample configuration for understanding purposes.
 
Server 1 - Website configuration in Web Server
 
Website learninginurdu.pk is mapped with IP: 10.100.10.101
 
Server 2 - Website configuration in Web Server
 
Website learninginurdu.pk is mapped with IP: 10.100.10.111
 

Load Balancer Configuration

 
VIP: 10.100.10.121 is mapped with (10.100.10.101, 10.100.10.111)
 
DNS Entry for Website & Load Balancer VIP
 
IP: 10.100.10.121 Domain Mapped: learninginurdu.pk
 
Now if we type learninginurdu.pk in browser; Domain to IP resolution happens through DNS and request goes to Load Balancer (10.100.10.121) and then considering load/settings, LB forwards request to 10.100.10.101 or 10.100.10.111 web servers.
 

Question

 
What if we want to send our requests to a specific web server only for testing purposes?
 

Solution 1

 
In load balancer configuration, we can disable mappings of other web servers & keep one mapping enabled. But if our website is in production, the web server may be overloaded and end users may face performance issues. And also it will require a change in load balancer which may impact the overall accessibility of website during production hours.
 

Solution 2

 
We can update hosts file on our machine and add mapping of domain and IP of a specific web server in it. After this when we’ll access the website in a browser, it will not go to load balancer but will go directly to specific web server. 
 
For example, we want to access learninginurdu.pk which is hosted on 10.100.10.101, we can make the following entry in our hosts file.
 
10.100.10.101 learninginurdu.pk
 
Now on command prompt, we can check if it is working or not
 
ping learninginurdu.pk
 
It should show 10.110.10.101. Now if we type http://learninginurdu.pk in the browser, our request will be going directly to web server, not to load balancer.
 
Another use of hosts file can be when you are working on your local machine and you want to access your working website with a domain name (e.g. myweb.com) instead of localhost. You may add following entry in the hosts file to achieve this. 
 
127.0.0.1 myweb.com 
 

Summary

 
Hosts file on our machine allows us to define domain to IP mapping. We can add mappings in this file to access the website from the specific server if website is behind a load balancer. Sometimes, we use this for testing/debugging purposes and sometimes we intentionally route our traffic to specific server to gain performance.