Feed Facebook

A-A+

加快網頁瀏覽速度 – Local DNS Cache

2009年06月08日 Windows XP, 研究資源 暫無評論

A DNS server resolves domain names into IP addresses. So when you request “google.com” for example, the DNS server finds out the address for the domain, and sends your request the right way.

You can run a DNS cache on your computer. This will speed up the process of looking up domain names when browsing. The difference is about 30-60 ms for me. Multiply that difference by the number of websites you visit a day for an approximate estimate of the speed improvement. Of course, all this would be worth it if it weren’t for the fact that setting this up is way too easy.

The following instructions are for someone with a cable (broadband) internet connection, where the computer gets it’s local IP address using DHCP from the router in your house/office:

The package we will be using for caching nameserver lookups is called dnsmasq. So first, install it using:
$sudo apt-get install dnsmasq
(If you can’t find then, then you probably haven’t added the Universe repository to your list of repositories.)

No uncomment the following line (that is edit the line to NOT have a “#” in the beginning) in the file /etc/dnsmasq.conf:
listen-address=127.0.0.1

Now edit /etc/dhcp3/dhclient.conf and make sure the section below exactly like this, especially the line that says “prepend domain-name-servers 127.0.0.1;”

#supersede domain-name “fugue.com home.vix.com”;
prepend domain-name-servers 127.0.0.1;
request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, host-name,
netbios-name-servers, netbios-scope;

Explanation for the above change: In the normal case, when you get a new dhcp lease, the dhcp3 client (tool) on your computer gets a new lease, and updates the /etc/resolv.conf file on your computer with the right values for the DNS servers to use (usually some machine in the network of your hosting provider). Adding the “prepend” option as we did above ensures that “127.0.0.1″ will appear on the top of the list of DNS servers. That magic number refers to your own computer. So in the future, whenever your computer needs to resolve a domain name, it will forward that request to dnsmasq (which is running at 127.0.0.1 – your computer). If the details for the domain name are already in you cache, well and good, dnsmasq will serve it up and make the process real fast. If it is not in the cache, then dnsmasq will look at the /etc/resolv.conf file and use the nameservers listed below the “127.0.0.1″. I hope that explains things.

Now open the file /etc/resolv.conf in your text editor. It probably looks like:

search yourisp.com
nameserver 217.54.170.023
nameserver 217.54.170.024
nameserver 217.54.170.026

The 127.0.0.1 is missing right now since you haven’t renewed your lease after you edited the /etc/dhcp3/dhclient.conf file. So, let us add that in manually this one time. After you do, your /etc/resolv.conf file will look like the following:

search yourisp.com
nameserver 127.0.0.1
nameserver 217.54.170.023
nameserver 217.54.170.024
nameserver 217.54.170.026

Don’t worry if the numbers are different – if they are not, then hey – we must be neighbours 

Okay. We are almost done here. All we have to do now is to restart dnsmasq so that the changes we made to the configuration file take effect. You can do that using the command:
$sudo /etc/init.d/dnsmasq restart.

Now you are running a local DNS cache. If you want to measure your speed improvement, type the command:
$dig google.com
You will see something like “;; Query time: 38 msec” there.
Now type the command again, and you should see something like:”;; Query time: 2 msec”

See, the first time, since google.com’s details were not in your cache (you are using it for the first time), the query took 38 ms. The second time, the cache speeds up the lookup. I have been using this for over a month now, and haven’t had a problem.

The following is ONLY for dsl customers
Note: If you have a dsl connection, the following may work:
Basically, the differences are in how the “conf” files are edited and used.
Copy the /etc/resolv.conf file to /etc/resolv.dnsmasq.conf
Then, edit the /etc/dnsmasq.conf file as follows:

# Change this line if you want dns to get its upstream servers from
# somewhere other that /etc/resolv.conf
resolv-file=/etc/resolv.dnsmasq.conf

You also have to uncomment the line that says listen-address=127.0.0.1
Now, edit /etc/resolv.conf to have ONLY the following line in it:
nameserver 127.0.0.1
Next, edit /etc/ppp/peers/dsl-provider and change the line:
usepeerdns to
#usepeerdns
(that is, comment out that line)
The ppp client does not allow you to prepend the 127.0.0.1 entry to your /etc/resolv.conf file. So what we did in the above was to create a copy of your previous resolv.conf for dnsmasq to use for lookups, update the file to use a local cache, and then prevent the ppp client from overwriting the resolv.conf file the next time. Now you can restart the dnsmasq service as I explained above, and start enjoying faster name resolution.
I don’t have a dsl connection, and so all the above is to the best of my knowledge.

標籤:

給我留言