Varnish Cache

One of the HTTP Reverse Proxy Servers that partially implements ESI is Varnish Cache. This HTTP Cache Server was originally thought out by its creators, Poul-Henning Kamp, Anders Berg and Dag-Erling Smørgrav, as being a highly needed [5] replacement for Squid, a well-known HTTP forward proxy server (client proxy). It was possible to make Squid work as a Reverse Proxy (server proxy), but it was very difficult to set it up to act in this way.

The original meeting that led to the creation of Varnish Cache took place in Oslo in February of 2006. The basic concept behind the project was to find a way to quickly manipulate bytes that would be taken from passing network traffic and a way to determine what, where and when to cache those bytes. Many years later, Varnish Cache has become one of the most important HTTP cache servers on the web with almost three million websites using it in production [6].

In order to better understand how Varnish Cache works, let's take the time to install it inside a Linux for the PHP base container.

In a new Terminal window, please enter this Docker command:

# docker run -it -p 6082:6082 -p 8484:80 asclinux/linuxforphp-8.1:src /bin/bash 

Then, enter these commands:

# pip install --upgrade pip
# pip install docutils sphinx

You should now see the following messages on the CLI:

Confirmation that the requested Python modules have been installed

Then, enter these commands:

# cd /tmp
# wget https://github.com/varnishcache/varnish-cache/archive/varnish-6.0.0.tar.gz

Once done, you should see a screen similar to this one:

The download of the archive containing the source code of Varnish Cache is completed

Finally, please finish the installation by unpacking, configuring and installing Varnish Cache with the following commands:

# tar -xvf varnish-6.0.0.tar.gz
# cd varnish-cache-varnish-6.0.0/
# sh autogen.sh
# sh configure
# make
# make install
# varnishd -a 0.0.0.0:80 -T 0.0.0.0:6082 -b [IP_ADDRESS_OR_DOMAIN_NAME_OF_WEB_SERVER]:80

Once completed, you should receive the following message:

The Varnish Cache daemon is now running and waiting for connections

As we mentioned in Chapter 2, Continuous Profiling and Monitoring, of this book when we were installing the TICK stack through Docker containers, you can get the IP addresses of the two containers (the one running the Apache server and this new one that is running the Varnish server), by issuing this command:

# docker network inspect bridge 

Once you get the results, you can replace the [IP_ADDRESS_OR_DOMAIN_NAME_OF_WEB_SERVER] placeholder in the previous command with the IP address of the container running Apache (the Linux for PHP container). In my case, the IP address of the Apache Web server is 172.17.0.2 and the IP address of the Varnish Cache server is 172.17.0.3. The command would therefore be:

# varnishd -a 0.0.0.0:80 -T 0.0.0.0:6082 -b 172.17.0.2:80 

Once started, you can point a browser to the IP address of the Varnish Cache server and you should get the Apache Web server's content. In my case, when pointing my browser to 172.17.0.3, I obtain the expected result:

Varnish is caching and returning the response obtained from the Apache server

We can confirm that the Varnish Cache server is using our Apache Web server as its backend by issuing the following curl command in a new Terminal window and piping the results to grep in order to see the request and response headers:

# curl -v 172.17.0.3 | grep Forwarded 

The result should be similar to the following screenshot:

The Varnish Cache headers are added to the Apache headers

As we can see, the headers show that the Apache server is responding via the Varnish Cache server.

Thus, with proper DNS configuration, it would become possible to redirect all the web traffic to the Varnish Cache server and use the web server as its backend only.

This example shows us how easy it is to configure a Varnish Cache server and how simple it is to start using it and benefiting from it right away in order to quickly boost web server performance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset