Varnish Cache
Varnish is an open-source HTTP accelerator and reverse proxy server. Its main features include:
(1) It is based on the memory cache, and the data will disappear after restart.
(2) Using the virtual memory mode, Io performance is good.
(3) supports setting 0 ~ Precise cache time within 60 seconds.
(4) VCL configuration management is flexible.
(5) the maximum size of cached files on 32-bit machines is 2 GB.
(6) powerful management functions, such as top, stat, admin, and list.
(7) Clever state machine design and clear structure.
(8) use the binary heap to manage cached files and actively delete the files.
Compared with the squid server, varnish has the following advantages:
1. High Stability. When the squid and squid servers work at the same load, the probability of a server failure is higher than that of varnish, because squid needs to be restarted frequently.
2. Faster access. With the "Visual page cache" technology, All cached data is directly read from the memory, while squid reads cached data from the hard disk, so varnish is faster in terms of access speed.
3. supports more concurrent connections. Because varnish TCP connections are released faster than squid, more TCP connections can be supported in High-concurrency connections.
4. You can use regular expressions to batch clear some caches through the Management port, but squid cannot.
Disadvantages include:
1. CPU, I/O, memory, and other resource overhead are higher than squid in high concurrency.
2. Once a process is suspended, crashed, or restarted, the cached data will be completely released from the memory. At this time, all requests will be sent to the backend server. In the case of high concurrency, this puts a lot of pressure on the backend servers.
Varnish installation:Wget http://repo.varnish-cache.org/source/varnish-4.0.0.tar.gz
Tar-zxvf varnish-4.0.0.tar.gz
CD Varnish-4.0.0
./Autogen. Sh
./Configure -- prefix =/usr/local/Varnish
Varnish run:Varnishd-F default. VCL-s malloc, 1024 M-T 127.0.0.1: 200-A 0.0.0.0: 80 Where-F specifies the VCL configuration file and-s specifies the memory allocation type and size, -T specifies the text management interface,-A specifies the Service Listening IP address and port;
VCL(Varnish configuration language) is the varnish configuration language. When VCL is executed, varnish converts it to binary code.
VCL syntax features
- Blocks are separated by curly brackets. The statement ends with a semicolon and uses '#' to add comments;
- VCL supports the following operators: = (Value assignment), = (comparison ),~ (Matching ),! (NO), & (logical and), | (logical or );
- In VCL, there are three important data structures: req (client request), beresp (backend response), and OBJ (Cache object );
- There is no operator between two strings;
- VCL does not have User-Defined variables, but you can use the set keyword to set variable values, such as set beresp. TTL = 5d;
- Use the set keyword to set the HTTP header, and use the unset keyword to remove the HTTP header, for example
set req.http.User-Agent = "unknown";unset req.http.Range;
- VCL has the IF/else Condition Statement, but there is no loop statement;
- Supports include and loading configuration files, such
include "foo.vcl";
- Supports import and Loading modules, such
import std;sub vcl_recv { std.log("foo");}
Define backend
Probe healthcheck {. url = "/test.jpg"; # defines the Health Check page. interval = 6 s; # The sending cycle of the probe request. The default value is 5 s. timeout = 0.3 s; # The expiration time of each probe request. The default value is 2 s. window = 8; # set the number of recent probes when determining the health status of the backend host. The default value is 8. threshold = 3; # In. in the number of times specified in window, at least how many times are successful before the backend host is declared healthy. The default value is 3. initial = 3; # the minimum number of successful detection times required for the backend host when varnish is started. threshold}
backend default { .host = "127.0.0.1"; .port = "80";
.probe = healthcheck;
.host_header = "www.example.com"; .connect_timeout = 60s; .first_byte_timeout = 60s; .between_bytes_timeout = 60s; .max_connections = 800;}
The host option must be explicitly assigned values, and other options are optional;
VCL can aggregate multiple backends into one group (director). When one backend in the group is suspended, the traffic can be imported to other healthy backends; director can use different algorithms to select backend:
- Random: Select backend Based on the configured weight (. Weight parameter;
- Round-Robin, round robin;
- Client: Select backend based on client. Identity. You can set the value of client. Identity to session cookie to identify backend;
director b2 random { .retries = 5; { // We can refer to named backends .backend = b1; .weight = 7; } { // Or define them inline .backend = { .host = "fs2"; } .weight = 3; }}
Define an ACL (Access Control List)ACL is used to define the access control mechanism of varnish.
acl localnetwork { "localhost"; # myself "192.0.2.0"/24; # and everyone on the local network ! "192.0.2.23"; # except for the dial-in router}
if (client.ip ~ localnetwork) { return (pipe);}
VCL code process
VCL built-in functions
- Vcl_recv
It is used to receive and process requests. When a request arrives and is successfully received, it is called. The data structure in vcl_recv is mainly req.
- Vcl_fetch
Call this method after obtaining the backend response. Determine whether the obtained content is cached or directly returned to the client. The main data structure of vcl_fetch is beresp;
- Vcl_pipe
Pass the request directly to backend;
- Vcl_pass
The request is directly sent to the backend and the backend response is sent to the client without any cache. The latest content is returned each time during the current connection;
- Vcl_hash
- Vcl_deliver
Call this method before sending the request content found in the cache to the client;
- Vcl_hit
After the lookup command is executed, the function is automatically called when the requested content is found in the cache;
- Vcl_miss
After the lookup command is executed, the function is automatically called if no requested content is found in the cache. This function can be used to determine whether to obtain content from backend;
- Vcl_error
This function is called when an error occurs;
VCL action (Actions)
- Pass to the vcl_pass function;
- Lookup: searches for the requested object in the cache, and gives control to the function vcl_hit or vcl_miss based on the search result;
- Pipe: Give the request control to the vcl_pipe function;
- Deliver: sends the content found in the cache to the client, and gives control to the function vcl_deliver;
- ESI, ESI-process,
- Error Code [reason], return the code to the client, and discard processing the request;
VCL public variables
When a request arrives at varnish:Req. backend specifies the corresponding backend host
Server. IP indicates the Server IP Address
Client. IP indicates the Client IP Address
Req. quest is only the request type, such as get and head.
Req. url specifies the request address
Req. proto indicates the HTTP protocol version of the Request initiated by the client
Req. http. header indicates the HTTP header information in the corresponding request.
Req. restarts indicates the number of restarts. The default value is 4.
When varnish sends a request to backend:Beresp. requset specifies the request type, such as get and head.
Beresp. url indicates the request address
Beresp. proto indicates the HTTP protocol version of the Request initiated by the client
Beresp. http. header indicates the HTTP header information in the request.
Beresp. TTL indicates the cache retention period (s)
When the backend obtains the content:OBJ. Status indicates the Request status code of the returned content, such as 200, 302, and 504.
Whether the returned content of obj. cacheable can be cached
Whether obj. Valid is a valid HTTP Request
OBJ. response returned Request status information
OBJ. proto: HTTP Version of the returned content
OBJ. TTL indicates the lifecycle of the returned content, that is, the cache time, in seconds.
OBJ. lastuse returns the current time interval of the last request, in seconds.
When responding to the client:The HTTP code status returned to the client by resp. Status
The HTTP Protocol version returned by resp. proto to the client
The HTTP header message returned by resp. http. header to the client.
The HTTP header status returned by resp. Response to the client
Varnish Cache Server