Servers

DIY: Speed up your Apache server with Varnish Cache

Varnish Cache will help to get your Apache server running 10-300 times faster without costing you a dime for a fatter pipe or fatter hardware.

All hail the Apache server -- the king of web servers is reliable, secure, and fast. But even with Apache's out of the box speed, some situations might call for an even faster experience. One tool created for speeding up Apache is Varnish Cache. The open source web application accelerator (also called a caching HTTP reverse proxy) can speed up your web server by a factor of 10x to 300x depending on architecture and do a number of tricks.

I will demonstrate how to install and configure Varnish Cache to get as much zoom out of it as possible. I will assume the web server is already running and configured to serve up pages or applications. For this demonstration, I am working on a Ubuntu 11.04 server.

Installing Varnish Cache

There are a couple of paths to installation: from source or from pre-compiled binary. From the Varnish Cache download page, make sure to go to the latest stable release and then look through the listing for pre-compiled binaries. You will find:

  • Debian
  • FreeBSD
  • Red Hat Enterprise Linux 5
  • Ubuntu

When you click the listing for your distribution, you will be taken to a page that describes how to add the correct repository. Before you do this, you might check your Add Remove Software tool to see if Varnish Cache is already included (it was for Ubuntu 11.04). Open your Add Remove Software tool and search for "varnish" (no quotes). If Varnish Cache is listed, mark it for installation and click Apply.

If you prefer to install from source, you need to make sure you meet the following dependencies:

  • automake
  • autoconf
  • libtool
  • ncurses-devel
  • libxslt
  • groff
  • pcre-devel
  • pkgconfig

With the above out of the way, here are the steps to install:

  1. Download the source from the download page.
  2. Unpack the .tar.gz file:
  3. Change into the newly created directory.
  4. Issue the command sh autogen.sh
  5. Issue the command ./configure.
  6. Using root privileges (either as the root user or with sudo) issue the command make.
  7. Using root privileges (either as the root user or with sudo) issue the command make check.
  8. Using root privileges (either as the root user or with sudo) issue the command make install.

If you install from source, more than likely this will install configuration files in /usr/local/varnish. If you install from a pre-compiled binary, you will find /etc/varnish holds your configuration files.

Configuring Varnish Cache

You need to let Varnish Cache know what the content is on the server. Like all other configurations, this is taken care of in /etc/varnish/default.vcl (assuming you installed from a pre-compiled binary). Near the top of the file, you will see a section that looks like this:

backend default {

.host = "127.0.0.1";

.port = "8080";

The above section is fairly self-explanatory. You should change the host address and the port used for your web server. You can have more than one backend defined, or you can even join backends together for load balancing. Make your changes and save the file.

Now set your web server port to match that of Varnish Cache. For example, if you have .port = set to 8080, you should make sure Apache is listening on port 8080 as well. Don't worry -- this only ensures that HTTP requests are now sent through Varnish Cache and not directly to Apache. After you make that change to Apache, be sure to restart the service.

Starting Varnish Cache

The Varnish daemon is varnishd and can be started with the command:

sudo varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000
  • The -f switch instructs Varnish Cache about which configuration file to use when starting.
  • The -s switch informs Varnish Cache about what type and how much memory to use.
  • The -T switch tells Varnish Cache you want to access the text-based administration interface.

Checking the logs

To make sure Varnish Cache is doing what it should, issue the command varnishlog. You should see output like this:

0 CLI          - Wr 200 19 PONG 1317310135 1.0

0 CLI          - Rd ping

0 CLI          - Wr 200 19 PONG 1317310138 1.0

0 CLI          - Rd ping

0 CLI          - Wr 200 19 PONG 1317310141 1.0

0 CLI          - Rd ping

0 CLI          - Wr 200 19 PONG 1317310144 1.0
0 CLI          - Rd ping
0 CLI          - Wr 200 19 PONG 1317310147 1.0

Now that you know Varnish Cache is running, open your website (the one configured as the Varnish Cache backend server), and you should see output like this in the log:

11 SessionOpen  c 127.0.0.1 58912 0.0.0.0:80

11 ReqStart     c 127.0.0.1 58912 595005213

11 RxRequest    c GET

11 RxURL        c /

11 RxProtocol   c HTTP/1.1

11 RxHeader     c Host: localhost:80

11 RxHeader     c Connection: keep-alive
Varnish Cache is working. Congratulations.

Cookies

Because every website is not created equally, you have to deal with certain issues. For example, Varnish Cache will not cache an object with a Set-Cookie header present or if the client sends a Cookie header. When this happens, Varnish Cache will bypass the cache. Because of this, Varnish Cache is set up to bypass cookies.

Varnish Cache tools

Two tools included with Varnish Cache that you might find incredibly useful are:

  • varnishadmin: To log on to the text-based administration console, issue the command (following the startup command from earlier): varnishadmin -T 127.0.0.1:2000. From here you can issue the help command to list all of the possible commands that can be used from within the administration console.
  • varnishtop: This tool will list out all of the URLs that are hitting the backend server (Figure A). To start this tool, issue the command varnishtop.
Figure A

This tool will give you lots of information about what is connecting to your Varnish-enhanced server.

For more information

There's a lot more to Varnish Cache than what I have explained, but I hope this post serves as a helpful introduction. I encourage you to look at the Varnish Cache documentation and read about topics that best apply to your needs. Some of the topics covered in the documentation are: sizing your cache, achieving a high hitrate, User-Agent, purging and banning, Edge Side includes, Directors, and Health checks.

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.

1 comments
libbkmz
libbkmz

Nginx are also very fast revesed proxy server, and it's more flexible than varnish. And using apache without nginx, in russia I mean, not accepted