New Users: Quick hosts file: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
(New page: <span id="top"></span> <div style="padding:0;margin:0; border-bottom:3px inset #000000"> {| | MPG UniCenter || New Users: Home | [[Ne...)
(No difference)

Revision as of 21:47, 26 May 2008

MPG UniCenter

New Users: Home | Quick hosts file

Quick hosts file

hosts file

During web-site design and development the hosts file is your friend you will be amply rewarded for making the effort to understand its capabilities. Before I dive straight in a little background knowledge is useful. (Or zap straight to real site names to see its true power)

Resolving telephone numbers

A mobile telephone users checks to see if he/she has the name number pair stored locally. If not will resort to looking in a telephone directory (paper version) should this fail resort to calling an operator for a persons telephone number.

Resolving internet address

Resolving an internet address is similar, a computer first checks in its local address book this is a file named hosts. If it cannot find a match it looks in another larger local address book (this may or may not exists such as POSIDAS a personal DNS server). Should this search fail, a last resort is to put out a world wide request on the Internet DNS (domain name service), This also has a hierarchy with your ISP being the first destination in the resolving process.

Host

The hosts file may seem insignificant however it is very powerful, being the first port of call in the resolution process you can return an IP address of your own choice. Use it to speed up external site downloads, block undesirable external sites more importantly harness its power for redirection easing web page developmenteasing web page development.

At startup the file is loaded into memory (cache) all it contains are mappings of host names to IP addresses it overrides any other DNS. It is located in your system folder if not create one. When a browser attempts resolution it follows a defined sequence.

Top

Defined sequence.

During URL (internet address) IP (numeric) address resolution the follow sequence is followed:

  • The client checks to see if the name queried is its own.
  • The client then searches a local Hosts file, a list of IP address and names stored on the local computer.
  • The client then checks for a personal DNS server
  • If these fail makes a world wide DNS request

This above process is transparent to the user.

Important address

All hosts files contain one very important name-address pair localhost 127.0.0.1 When you try to connect using either localhost or 127.0.0.1 you are immediately looped back to your own machine. Any address within the range 127.*.*.* may be used for loop back however stick with convention and use 127.0.0.1

The hosts file is located in the systems folder.

Top

Hosts Location

The hosts file is located in the system folder. The path may be different depending on your setup, these paths are for typical default installations.

c:\windows\~system32\drivers\etc — Windows Vista
c:\windows\hosts — Windows 95/98/me
c:\winnt\system32\drivers\etc\hosts — Windows NT/2000/XP Pro
c:\windows\system32\drivers\etc\hosts — Windows XP Home

Create an hosts file

After checking the above location and or performing a search you cannot find a file create one.

The default format looks like this, save its as hosts (no file extension) into the appropriate path as above:

# Copyright (c) 1993-1999 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
127.0.0.1 localhost # loop back address

Make sure the file contains the following line: 127.0.0.1 localhost # loop back address

Note: You may use a different loop back address as mentioned above.

Top

Speed up page loads

Sites you visit regularly are good candidates to put in your hosts file this can speed up your access to them. A site address may be the result of clicking a page link or one typed in at the browser address bar. Before your computer can access the page it needs to resolve the address into a numeric form.

If this address is not available locally your browser as a last resort sends a DNS request to your service provider. The resolution may take several seconds, a long time compared to a few milliseconds for a local resolution. To speed this process up all you need to do is put the name-address pair in your hosts file.

Manual resolution

You already know the URL (name) for example www.uniformserver.com like a computer you need to resolve this to an IP address.

  • You can use Ping
  1. Open a command prompt start > run > type in cmd
  2. At the prompt type: ping www.uniformserver.com

After a short time ping will resolve the name to an IP address looks similar to this:

Pinging uniformserver.com [216.240.157.190] with 32 bytes of data:
Reply from 216.240.157.190: bytes=32 time=259ms TTL=54
Reply from 216.240.157.190: bytes=32 time=239ms TTL=54
Reply from 216.240.157.190: bytes=32 time=235ms TTL=54
Reply from 216.240.157.190: bytes=32 time=199ms TTL=54
Ping statistics for 216.240.157.190:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 199ms, Maximum = 259ms, Average = 233ms

With the name resolved all you need to do is add the following to the end of your hosts file:

216.240.157.190 www.uniformserver.com # Uniserver main site

Repeat the process for each site you would like to speed up.

Another use for the hosts file is to block undesirable sites and advertising.

Top

Blocking Sites

The hosts file makes it easy to block complete sites or to block adds on your favorite sites.

Blocking sites

Use something like this: 127.0.0.1 domain name of site

Blocking ad banners

This is similar to the above all you need to know is the banner URL

  1. Right click on the ad banner, select "Properties". This will show the address of the ad banner.
  2. Make sure it is not the address of the main site. You only need the ad banner address.

With that information just add it to your hosts file: 127.0.0.1 address for ad banner

Result

You are redirecting these requests to your machine, the pages or images will obviously not be found (shame that) your browser will just display an empty space where a image is supposed to be or moan that a page cannot be found.

If you pursue this route be warned, your hosts fill will quickly grow. Search the Net for hosts examples and you appreciate what I mean.

I am more interested in using the hosts file to make life a little easier for web development.

Top

Real Site Names

All the above tips although worthy of note pale into insignificance compared to this one. If you are developing a site to be hosted on another server or come to that your own server typing localhost or 127.0.0.1 into your browser address bar is not very informative a more desirable solution would be to type in your real Internet address. Using the host file allows you to do just this.

Just in case you missed the significance of the above suppose you install a third party piece of software requiring your real host name and MySQL server host name. You can simulate these locally using the hosts file. Type your web-site name into your browser address bar it instantly loops back and picks up your development server, it does not rush off and pick up the site from the Internet.

For example suppose my real site is www.johndoe.com

Add the following to the hosts file: 127.0.0.1 www.johndoe.com

That’s all its takes simple and very effective.

Full Paths

Your browser now thinks it’s looking at a real site displaying the real site address in its address bar. Click on a link within your site and you will be greeted with its full path again displayed in the address bar for example:

http://www.johndoe.com/pictures/2_friends2.html

Final piece of magic

One final piece of hosts magic, if you have set the Apache server up for Name based Virtual Hosting you can add all these site names to the hosts file. This will then loop them back to your development server this in turn will re-direct to the appropriate site. Hence while developing more than one site you can view them using real names in your browser.

Uniform Server 3.5-Apollo makes the above veru easy to set-up check out this section of the admin panel.

Ports

If you have changed the Apache listening port say to 81 then use hosts to redirect to this port change the above line to look like this:

127.0.0.1:81 www.johndoe.com

Alternative to the hosts file

There is an alternative to the hosts file and that is to use PAC if you need to run Uniform Server on a memory stick take a look at Virtual Hosting & PAC it may be of use.

Top


Ric