Custom CSS

Saturday, May 17, 2008

Using the hosts file and lighttpd to block ads

Using the hosts file to block ad sites is more efficient than say, using the adblock plugin for Firefox. With adblock, your computer will still send a request to the spam/advertising site; adblock just hides those ads from your view (at least it did the last time I checked). Also, you are out of luck if a non-web application decides to communicate with a spam site.

Setting up the hosts file

The hosts file can be modified to get around these limitations; you can force all spam sites to resolve to your own IP, and Firefox (or any other application) will request some ad from your own machine (and chances are, you won't have it). This cuts down on network traffic and keeps other pesky applications from contacting their parents. On linux, just head over to a site like this and grab an updated hosts file. Just in case you don't know what you're doing, backup your current /etc/hosts, then append the contents of this to it. Finally, delete these lines:  localhost  localhost.localdomain broadcasthost
::1  localhost

Fixing the ugly page not found errors

Now, wherever you would have seen an ad in Firefox, you get an ugly "Page not found" error instead. To fix this, configure the web server on your computer (assuming you're running one, assuming you're a web developer) to return an empty file on a 404. Assuming you're not a web developer and/or you don't have a web server running, grab lighttpd, or sudo apt-get install lighttpd.

Now edit the lighttpd.conf (located at /etc/lighttpd/lighttpd.conf on Ubuntu), and make sure you have these settings by either uncommenting, adding, or editing the config file. Don't touch any of the other settings.

server.error-handler-404  = "/404.html"
server.bind = "localhost"

Check the server.username and server.groupname parameters. They should both be www-data; keep this in mind.

Now go to the /var/www directory and create an empty 404.html:

cd /var/www
touch 404.html

Then check the permissions of the /var/www directory, and make sure they are owned by www-data (or whatever the server username/groupname for your machine should be). If not, change the ownership:

chown -R www-data:www-data /var/www

Now restart lighttpd (or restart your computer), and watch those page not found errors go away.
Post a Comment