High-performance HTTP accelerator Varnish (installation and configuration) (1)

Source: Internet
Author: User
Tags varnish

1. Install Varnish
The installation of Varnish is very simple. Next we will introduce it step by step:
1. Preparations before installation
The following table 1 shows the installation environment of Varnish:
Table 1
Host Name operating system IP Address
Varnish-server CentOS release 5.4 192.168.12.246
Web-server CentOS release 5.4 192.168.12.26
Create a varnish user and user group, and create the Varnish cache directory and log directory:
[Root @ varnish-server ~] # Useradd-s/sbin/nologin varnish
[Root @ varnish-server ~] # Mkdir/data/varnish/cache
[Root @ varnish-server ~] # Mkdir/data/varnish/log
[Root @ varnish-server ~] # Chown-R varnish: varnish/data/varnish/cache
[Root @ varnish-server ~] # Chown-R varnish: varnish/data/varnish/log
2. Obtain varnish Software
Varnish's official site is http://varnish-cache.org, which contains the latest varnish instructions and version update history, from which you can find varnish download link in SourceForge, currently, the latest version of varnish is Varnish 2.1.2. The package after the download is varnish-2.1.2.tar.gz. Here we will take this version as an example to install and configure it.
3. Install pcre
If Pcre is not installed, the system will prompt that the pcre Library cannot be found when compiling varnish2.0 or later versions. The pcre Library must be installed first to ensure compatibility with regular expressions.
[Root @ varnish-server ~] # Tar zxvf pcre-7.9.tar.gz
[Root @ varnish-server ~] # Cd pcre-7.9/
[Root @ varnish-server ~] #./Configure -- prefix =/usr/local/pcre/
[Root @ varnish-server ~] # Make & make install
4. Install varnish
Install varnish in the/usr/local/directory as follows:
[Root @ varnish-server ~] # Tar-zxvf varnish-2.1.2.tar.gz
[Root @ varnish-server ~] # Cdvarnish-2.1.2
[Root @ varnish-server ~] # Export PKG_CONFIG_PATH =/usr/local/pcre/lib/pkgconfig
[Root @ varnish-server ~] #./Configure -- prefix =/usr/local/varnish \
> -- Enable-dependency-trackin
> -- Enable-debugging-symbols
> -- Enable-developer-warnings
[Root @ varnish-server ~] # Make
[Root @ varnish-server ~] # Make install
[Root @ varnish-server ~] # Cp redhat/varnish. initrc/etc/init. d/varnish
[Root @ varnish-server ~] # Cp redhat/varnish. sysconfig/etc/sysconfig/varnish
"PKG_CONFIG_PATH" specifies the path for varnish to find the pcre Library. If pcre is installed in another path, specify the corresponding path here, by default, Varnish finds the pcre Library path/usr/local/lib/pkgconfig. The last two steps are to copy some initialization script files of the varnish daemon. These scripts are used for varnish startup and shutdown management. The following sections will explain in detail.
So far, varnish has been installed.

Ii. Configure Varnish
1. VCL instructions
VCL, that is, Varnish Configuation Language, is used to define varnish access policies. VCL syntax is relatively simple, similar to C and perl, you can use the specified operator "= ", comparison operator "=", collation operator "!, &&,!!" . It also supports regular expression samples and "~" You can also use a keyword such as "set" to specify a variable.
Note that the "\" character has no special meaning in VCL, which is slightly different from other languages. In addition, VCL is only a configuration, not a real programming language and has no loops, no custom variables.
Before configuring Varnish, you must first understand the varnish configuration syntax, VCL, and detailed introduction to some built-in functions and public variables commonly used by VCL.
VCL built-in functions
1) vcl_recv Function
It is used to receive and process requests. When a request arrives and is successfully received, it is called. It determines how to process the request by judging the request data.
This function ends with the following keywords:
Pass: Indicates entering the pass mode and giving the request control to the vcl_pass function.
Pipe: Indicates entering the pipe mode and giving the request control to the vcl_pipe function.
Error code [reason]: indicates that "code" is returned to the client and the request is not processed. "code" is the error identifier, for example, 200 or 405, "reason" is the error message.
2) vcl_pipe Function
This function is called when it enters pipe mode. It is used to pass requests directly to the backend host. If the request and returned content are not changed, the unchanged content is returned to the client, until the link is closed.
This function ends with the following keywords:
Error code [reason]
Pipe
3) vcl_pass Function
This function is called when the pass mode is enabled. It is used to pass requests directly to the backend host. The backend host responds to the data and sends the data to the client without any caching, the latest content is returned for each connection.
This function ends with the following keywords:
Error code [reason]
Pass
4) lookup
Indicates to search for the requested object in the cache, and give control to the function vcl_hit or function vcl_miss based on the search result.
5) vcl_hit Function
After the lookup command is executed, if the request content is found in the cache, the function is automatically called.
This function ends with the following keywords:
Deliver: sends the found content to the client and gives control to the function vcl_deliver.
Error code [reason]
Pass
6) vcl_miss Function
After the lookup command is executed, this method is automatically called if the requested content is not found in the cache. This function can be used to determine whether the content needs to be retrieved from the backend server.
This function ends with the following keywords:
Fetch: gets the requested content from the backend and gives control to the vcl_fetch function.
Error code [reason]
Pass
7) vcl_fetch Function
Call this method after updating the cache from the backend host and obtaining the content. Then, you can determine whether to put the content into the cache or directly return it to the client by determining the obtained content.
This function ends with the following keywords:
Error code [reason]
Pass
Deliver
8) vcl_deliver Function
After finding the request content in the cache, call this method before sending it to the client. This function ends with the following keywords:
Error code [reason]
Deliver
9) vcl_timeout Function
This function is called before the cache content expires. It ends with the following keywords:
Discard: indicates to clear the content from the cache.
Fetch
10) vcl_discard Function
This method is automatically called when the cached content expires or the cache space is insufficient. It ends with the following keywords:
Keep: indicates that the content is retained in the cache.
Discard


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.