One way to improve the performance of the ad blocker is to serve either a 1x1 transparent image, or a blank HTML page. This can be done in a number of ways so we'll show a couple of the options available to you.

Pixelserv

The first, and maybe most common is to use pixelserv. This is a really lightweight perl web server that simply serves a 1x1 transparent GIF to any requests made to it. Thus, anytime ads get redirected by dnsmasq to pixelserv, you'll actually receive a tiny image that won't be visible on the page.

To start with, download the pixelserv file:
sudo curl /usr/local/bin/pixelserv http://proxytunnel.sourceforge.net/files/pixelserv.pl.txt >pixelserv.pl.txt
And also change the permissions:
sudo chmod 755 /usr/local/bin/pixelserv
Now, open the file with nano:
sudo nano /usr/local/bin/pixelserv
You can see that the pixelserv is a fairly small perl script. Let's edit it to change the IP address that we're using (192.168.42.49) to redirect the ads to. Find the line with LocalHost, and change it to the following:
$sock = new IO::Socket::INET (  LocalHost => '192.168.42.49',
Save the file by typing in Control-X then Y then return

You could try running the server now, but you'd get the following error:
pi@raspberrypi /usr/local/bin $ ./pixelserv
error : cannot bind : Cannot assign requested address exit
We can resolve this by adding this IP address to our wlan0 interface (Thanks to this site for this fix!). Open nano and the interfaces file:
sudo nano /etc/network/interfaces
Update your iface wlan0 inet static section to look like the following. We're adding the last two lines (post-up and pre-down):
iface wlan0 inet static
  address 192.168.42.1
  netmask 255.255.255.0
  post-up ip addr add dev wlan0 192.168.42.49/24
  pre-down ip addr del dev wlan0 192.168.42.49/24
Save the file by typing in Control-X then Y then return

Now reboot your Pi so the settings take effect:
sudo reboot
Once your system comes back up, try running the server. It won't output anything in the console, but you can try refreshing a page you know that has ads that get blocked:
sudo /usr/local/bin/pixelserv
It could get annoying having to always run that command. The next logical step would be to create a service that will start the server for us whenever we start our Pi. Let's do that.

First, kill the server you're running by typing "Ctrl-C".

Now, create a new file with nano:
sudo nano /etc/init.d/pixelserv
Copy and paste the following to that file:
#!/bin/bash

### BEGIN INIT INFO
# Provides:          pixelserv
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: pixelserv server for ad blocking
# Description:       Server for serving 1x1 pixels
### END INIT INFO

case "$1" in
   start)
     echo "pixelserv: starting"
     /usr/local/bin/pixelserv &
     ;;
   stop)
     echo "pixelserv: stopping"
     killall pixelserv
     ;;
   *)
     echo "Usage: service $0 {start|stop}"
     exit 1
     ;;
esac

exit 0
Save the file by typing in Control-X then Y then return

Change the permissions on that file:
sudo chmod 744 /etc/init.d/pixelserv
Test that the script works by starting and stopping it:
sudo /etc/init.d/pixelserv start
sudo /etc/init.d/pixelserv stop
Now, enable the script on startup of your Pi:
update-rc.d pixelserv defaults
You can manually start/stop the pixelserv server by executing the following:
sudo service pixelserv start
sudo service pixelserv stop
If you decide you'd rather use the Apache solution, you can disable the pixelserv service on startup:
sudo update-rc.d -f pixelserv remove

Apache

As an alternative to pixelserv, we can use Apache to serve a blank html file with an HTTP 200 response. Apache is a bit heavier but likely much more stable than pixelserv.

Let's start by installing Apache (make sure to stop and disable pixelserv if you've already installed that):
sudo apt-get install apache2 -y
By default apache is listening to all IP addresses on port 80. You can change this if you like, but it's not necessary.

Test that apache is picking up our redirected requests:
curl doubleclick.com
You should get the following:
pi@raspberrypi ~ $ curl doubleclick.com
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
pi@raspberrypi ~ $ 
That's a bit more than what we want. Let's modify it so we basically just get an OK from apache instead of any content.

First enable the apache2 rewrite engine by executing the following:
sudo a2enmod rewrite
Next, let's update the default VirtualHost. Execute the following to open the default VirtualHost file in nano:
sudo nano /etc/apache2/sites-available/default
Edit the <Directory /var/www/> section to look like the following (we're adding the last three lines):
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                RewriteEngine on
                RedirectMatch 200 (.*)$
                ErrorDocument 200 " "
        </Directory>
Save the file by typing in Control-X then Y then return

Make the same change for the default-ssl file as well (sudo nano /etc/apache2/sites-available/default-ssl).

At this point if you restart Apache, you'll get an error about not being able to determine the server's fully qualified domain name. We can fix that by executing the following command:
echo "ServerName raspberrypi" | sudo tee -a /etc/apache2/conf.d/fqdn
Ok, now we can restart apache:
sudo service apache2 restart
And test that the response from Apache has changed:
pi@raspberrypi /etc/apache2/sites-available $ curl doubleclick.com
 pi@raspberrypi /etc/apache2/sites-available $ 
That's it, you're all setup for super-speedy ad-blocking from your Raspberry Pi!

This guide was first published on Sep 13, 2013. It was last updated on Mar 08, 2024.

This page (Improving Performance) was last updated on Mar 08, 2024.

Text editor powered by tinymce.