Put Varnish In Front Of Apache On Debian Squeeze

Valic —  April 18, 2011 — 6 Comments

Varnish is a web accelerator. Its mission is to sit in front of a web server an cache the content. It makes your web site go fast.
In this mode, Varnish will stop incomplete HTTP requests from reaching your Apache webserver.

Installing Varnish:

Varnish is distributed in the Debian package repositories, but the version there might be out of date, and  generally recommend using the packages provided by varnish-cache.org or packages from backports.debian.org.

To use the varnish-cache.org repository and install varnish, do the following:

Change Varnish settings:

1. First change the default port.  Edit /etc/default/varnish:

vim /etc/default/varnish.

Find the uncommented line starting with “DAEMON_OPTS” and change *:6081 to *:80 so it will listen on the default HTTP port.

2. Rename default.vcl to something else, like  “debian-tutorials.vcl”.

3. Edit the file and paste the  following content:

## Redirect requests to Apache, running on port 8000 on localhost
backend apache {
        .host = "127.0.0.1";
        .port = "8000";
}
## Fetch
sub vcl_fetch {
		## Remove the X-Forwarded-For header if it exists.
        remove req.http.X-Forwarded-For;

		## insert the client IP address as X-Forwarded-For.
               ##This is the normal IP address of the user.
        set    req.http.X-Forwarded-For = req.http.rlnclientipaddr;
		## Added security, the "w00tw00t" attacks are pretty annoying so
                ##lets block it before it reaches our webserver
        if (req.url ~ "^/w00tw00t") {
                error 403 "Not permitted";
        }
		## Deliver the content
        return(deliver);
}

## Deliver
sub vcl_deliver {
		## We'll be hiding some headers added by Varnish.
                ##We want to make sure people are not seeing we're using Varnish.
               ## Since we're not caching (yet), why bother telling people we use it?
        remove resp.http.X-Varnish;
        remove resp.http.Via;
        remove resp.http.Age;

		## We'd like to hide the X-Powered-By headers.
               ##Nobody has to know we can run PHP and have version xyz of it.
        remove resp.http.X-Powered-By;
}

Change Apache settings:

1.Change the apache2 default port to listen on localhost:

vim /etc/apache2/ports.conf

and change the 80 port with 8000. You will have to edit your vhosts as well. 2. Install Apache module to make sure the IP address of the user ends up correct. Since Varnish is basically talking with Apache2, you would see 127.0.0.1 as visitor IP.

apt-get install libapache2-mod-rpaf

rpaf is for backend Apache servers what mod_proxy_add_forward is for frontend Apache servers. It does exactly the opposite of mod_proxy_add_forward written by Ask Bjorn Hansen.

Restart daemons:

/etc/init.d/apache2 restart

/etc/init.d/varnish restart

Tutorial adapted from howtofrge.com.

Valic

Posts Twitter Facebook

Editor in Chief at Debian-Tutorials, Linux enthusiast.

6 responses to Put Varnish In Front Of Apache On Debian Squeeze

  1. What kind of performance gains do you see with this? I was getting long hangs with my WordPress site, so after some Googling, I replaced mpm-prefork with mpm-worker and that seems to be have solved the problem for the most part. I have also been using WP Super Cache, which saves the whole WordPress site as static pages and serves those (the hanging issue is for logged in users who don’t get cached pages), but I was wondering if this is a better / faster alternative.

    BTW, nice site. As a recent convert to Debian (Server), I’ve been looking for something like this.

  2. in the near future I will list here a Varnish configuration suitable for a 5000 visitors/day WordPress Blog.
    It’s cache hit rate is 95.6%, that means that only 4.4% of the requests reach the backend and use it’s resources

  3. What kind of performance gains do you see with this? I was getting long hangs with my WordPress site, so after some Googling, I replaced mpm-prefork with mpm-worker and that seems to be have solved the problem for the most part. I have also been using WP Super Cache, which saves the whole WordPress site as static pages and serves those (the hanging issue is for logged in users who don’t get cached pages), but I was wondering if this is a better / faster alternative.
    +1

  4. Check my previous answer and you will get your answer.

  5. Anytime I attempt to put Varnish in front of Apache… Things go wrong. Have it installed mind you, but never could get it to go directly in front of it.

Trackbacks and Pingbacks:

  1. Configure Varnish logs (varnishnsca), Logrotate and Awstats on Debian Squeeze | Linux Debian Tutorials - May 2, 2011

    [...] you have varnish installed on your server and you started to catch the pages on it.Your webserver is now missing the [...]

Leave a Reply