1. varnish Introduction
Varnish is a high-performance and open-source reverse proxy server. verdens gang, Norway's largest online newspaper, uses three varnish servers instead of the original 12 squids, the performance is better than before. Varnish's author, Poul-Henning Kamp, is one of FreeBSD's core developers. He believes that today's computers are much more complicated than in 1975. In that era, there were only two types of storage media: memory and hard disk. However, in addition to the primary memory, the memory of the computer system also includes L1, L2 in the CPU, and even L3 cache. The hard disk also has its own cache device, so the Squid cache's architecture for processing object replacement by itself cannot be optimized by knowing these conditions, but the operating system can be informed of these conditions, so this part of work is handed over to the operating system for processing. This is varnish.
The design architecture of the cache.
Generally, there are three reasons for replacing squid with varnish:
1.1 varnish adopts the "Visual page cache" technology. In terms of memory utilization, varnish is more advantageous than squid. It avoids squid from frequently exchanging files in memory and hard disk, the performance is higher than squid.
1.2. varnish is highly stable, and the probability of failures of the squid server performing the same job seems higher than that of varnish.
1.3. Using varnish to manage ports, you can use regular expressions to clear some caches in batches. This is not available for squid.
2. Install and enable
2.1 installation preparation
Install dependency packages
# Yum install automake Autoconf libtool ncurses-devel libxslt Groff PCRE-devel pkgconfig-y
Download the installation package
# Wget http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz
2.2 install Varnish
# Tar xvf varnish-2.1.5.tar.gz
# Cdvarnish-2.1.5
#./Autogen. Sh // check whether the dependency of the software is satisfied.
#./Configure
# Make & make install
2.3 start Varnish
# Varnishd-F/usr/local/etc/Varnish/Default. VCL-A 0.0.0.0: 8080-s malloc, 1g-T 127.0.0.1: 2022
Parameter description:
-F specifies the configuration file used by varnishd
-S configures the storage type and storage capacity used by varnish. The storage type is divided into Directory files or memory, And/tmp directory file storage is used by default.
-A: The HTTP listening address and port.
-T the address and port used by the Telnet listener to execute some varnish management commands.
You can run the # varnishd -- version command to view all the parameters and meanings.
The preceding command indicates that varnishd uses/usr/local/etc/Varnish/default. the configuration parameters in VCL listen to all HTTP requests sent to port 8080 of the local machine, and cache the access results in the content with a maximum storage capacity of 1 GB. The varnish command can be sent and executed through port 2022.
2.4 Monitoring
View logs
# Varnishlog
View connections and hit rates
# Varnishstat
Enable log
# Varnishncsa-W/var/log/varnish. log &
3. Default. VCL configuration instructions
Vcl_recv
A request is called when it is successfully received and analyzed after it arrives. It ends with the following keywords.
Error Code [reason] returns the code to the client and discards the request
Pass enters the pass mode and gives control to vcl_pass
Pipe enters the pipe mode and gives control to vcl_pipe.
Lookup searches for the requested object in the cache and gives control to vcl_hit or vcl_miss Based on the query result.
Vcl_pipe
It is called when it enters pipe mode. The request is directly sent to the backend. The subsequent data between the backend and the client is not processed, but is simply transmitted until one party closes the connection. It generally ends with the following keywords.
Error Code [reason]
Pipe
Vcl_pass
It is called when you enter the pass mode. The request is sent to the backend, And the backend response data is sent to the client, but the request is not cached. The requests of the same connection are processed normally. It generally ends with the following keywords.
Error Code [reason]
Pass
Vcl_hash
Not used currently
Vcl_hit
After lookup, if you find the requested content in the cache, call it. It generally ends with the following keywords.
Error Code [reason]
Pass
Deliver sends the found content to the client and gives control to vcl_deliver.
Vcl_miss
It is called after lookup but no cached content is found. It can be used to determine whether the content needs to be retrieved from the backend server. It generally ends with the following keywords.
Error Code [reason]
Pass
Fetch obtains the request content from the backend and gives control to vcl_fetch.
Vcl_fetch
Obtain the content from the backend and call it. It generally ends with the following keywords.
Error Code [reason]
Pass
Insert inserts the obtained content into the cache, sends it to the client, and gives control to vcl_deliver.
Vcl_deliver
Cache content is called before it is initiated to the client. It generally ends with the following keywords.
Error Code [reason]
The deliver content is sent to the client.
Vcl_timeout
Called before the cached content expires. It generally ends with the following keywords.
Fetch gets the content from the backend
Discard discards this content
Vcl_discard
Called when cache content is discarded due to expiration or insufficient space. It generally ends with the following keywords.
Discard discard
Keep keep in cache
If these built-in routines are not defined, the default action is executed.
Some built-in Variables
Now current time, standard time point (1970) to current seconds
Backend. Host backend IP address or host name
Backend. Port the backend service name or port
Valid variable upon request arrival
Client. IP Client IP
Server. IP server IP
Req. Request request type, such as get, Head, or post
Req. URL request URL
HTTP version number of the req. proto request
The backend of the req. backend request
The HTTP header corresponding to Req. http. Header
Valid variable for future requests
Bereq. request, such as get or head
Bereq. url URL
Bereq. proto Protocol version
Bereq. http. header HTTP Header
Valid variable after the content is obtained from the cache or the backend
OBJ. proto HTTP Protocol version
OBJ. Status HTTP status code
OBJ. Response HTTP status information
Whether obj. Valid is a valid HTTP Response
Whether obj. cacheable can be cached. That is to say, if the HTTP return is 200, 203, 300, 301, 302, 404 and 410, and there is a non-zero lifetime, it can be cached.
OBJ. TTL lifetime, seconds
OBJ. lastuse last request to the current interval in seconds
Valid variable for client response
HTTP Version of resp. proto response
The HTTP status code returned by resp. Status to the client
The HTTP status information returned by resp. Response to the client
Resp. http. header HTTP Header