This article will detail the work flow of varnish, installation, configuration three aspects. Believe that after reading this article, the reader can master varnish.
Varnish Introduction
Varnish is a high-performance and open source of the reverse proxy server and HTTP Accelerator, the use of a new software architecture, and the current hardware system closely with the traditional squid compared with the varnish has a higher performance, faster, more convenient management and many other advantages, Many large web sites are beginning to try to replace squid with varnish, which promotes the rapid development of varnish.
Norway's largest online newspaper Verdens Gang (vg.no) used 3 varnish instead of the original 12 Squid, the performance is better than before, this is the varnish most successful application case.
Workflow for Varnish file caching
Varnish is similar to general Server Software and is divided into master process and child process. The master process reads the storage configuration file, invokes the appropriate storage type, and then creates/reads the cache file of the appropriate size, then master initializes the structure that manages the storage space, and then fork and monitors the child process. The child process, during initialization of the main thread, mmap the previously opened storage file into memory, creating and initializing the free structure, and hanging to the storage management structure to be allocated. The child process allocates several threads to work, mainly including some management threads and many worker threads.
Then, to start the real work, Varnish's responsible for receiving the new HTTP connection thread starts waiting for the user, and if there is a new HTTP connection, it is always responsible for receiving it, then waking up a waiting thread and handing it the specific process. The Worker thread reads the URI of the HTTP request, looks for an existing object, and returns and responds directly to the user if it is hit. If there is no hit, you need to fetch the requested content from the back-end server, save it in the cache, and then reply.
The process of allocating caching is this: it creates a cache file of the appropriate size based on the size of the object being read. For easy reading and writing, the program changes the size of each object to the memory page multiples that are closest to its size. It then looks up from the existing free storage structure, finds the most appropriate size of the free storage block, and assigns it to it. If the free block is not used up, put the extra memory into an additional free storage block, hanging on the management structure body. If the cache is full, release the oldest object according to the LRU mechanism.
The process of releasing the cache is as follows: There is a timeout thread that detects the lifetime of all the object in the cache, deletes it if the hyper-set TTL (time to Live) is not accessed, and frees the corresponding structure and storage memory. Note that the free memory blocks before or after the memory block are checked for release, and if the front or rear free memory and the freed memory are contiguous, they are merged into a larger chunk of memory.
The entire file cache management, does not consider the relationship between the file and memory, in fact, all the object is considered in memory, if the system is not enough memory, the system will automatically swap it to swap space, without the need for varnish program to control.
Varnish installation
Download Varnish installation Package
It is recommended to download the latest stable version (now the latest varnish version is 3.0.2), varnish provide the source code installation package and executable program installation package, according to your custom download suitable for your platform any installation package.
Install varnish
Source Code installation package installation
Install the Pcre library first, Pcre Library is a compatible regular expression, if not installed, you will be prompted not to find the Pcre library when you install the varnish2.0 version. The following is the installation process for Pcre, as shown in Listing 1:
Listing 1. Pcre Library Installation Code
Tar zxvf pcre.tar.gz
cd pcre/
./configure--prefix=/usr/local/pcre/make
&& make install
Install varnish, whose code is shown in Listing 2:
Listing 2. Varnish Installation Code
Tar xzvf varnish-3.0.2.tar.gz
CD varnish-3.0.2
export Pkg_config_path =/usr/local/pcre/lib/pkgconfig
./ Configure--prefix=/usr/local/varnish make make
install
Executable Program installation package installation
RedHat installation varnish in the system environment, you need to install the following software: Automake, autoconf, Libtool, Ncurses-devel, Libxslt, Groff, Pcre-devel, Pkgconfig, Then install the varnish and install the code as shown in Listing 3:
Listing 3. Varnish Installation Code
Rpm-i varnish-2.1.4-2.el5.x86_64.rpm
Start varnish
Listing 4. Varnish boot code
Varnishd-f/etc/varnish/default.vcl-s file,/var/varnish_cache,1g \
T 127.0.0.1:2000-a 0.0.0.0:9082