Play Varnish cache proxy
Varnish is an important cache Proxy component in Internet architecture. Varnish generally runs at the traffic entrance, which is very important as a front-end defense.
Although Varnish is not as popular as Nginx. Varnish is as stable and efficient as Nginx. if used properly, it can greatly improve overall service performance and reduce resource consumption.
Multi-level caching in common Internet architectures
- Push content to users' CDN Cache
- Cache proxy similar to Varnish
- Application Layer cache Redis and Memcache
- Database cache
Why is cache proxy important?
- Some facts: content changes are much less than they do not change, and the user content is eventually consistent. Based on these two facts, it can save a lot of repetitive computing and resources.
- Latency usually has a great impact on user experience and business. All Internet companies are trying to reduce latency and improve user experience.
- In some cases, CDN may return a large amount of data to the origin site. For example, when clearing the CDN cache, the application architecture must be able to defend against 100% of the traffic.
Several features of Varnish
1. Modify the HTTP Header
Request Header Modification
unset req.http.cookie;
set req.http.X-EOOD ="EOOD";
Modify the Response Header
unset beresp.http.Set-Cookie;
set beresp.http.Cache-Control="public, max-age=31536000";
2. Content exposure in the Varnish state
Cache hit or not
if(obj.hits >0){
set resp.http.X-Cache="HIT";
}else{
set resp.http.X-Cache="MISS";
}
3. Server Load balancer
Varnish also supports Simple load balancing, such as backend server polling, but it should be used with caution.
4. backend protection and fault tolerance
This feature may not be noticed by some people, but it is indeed very useful. When the backend fails, the read-only page will still be successfully returned to the customer.
set req.grace =48h;
5. Access Control
You can route or shield certain accesses according to rules, for example:
req.http.User-Agent
req.http.X-Forwarded-For
req.http.referer
...
Simple Password protection: Basic Auth
if(! req.http.Authorization~"Basic XXXXXXX"){
error 401"Restricted";
}
6. Merge multiple backend instances
Define multiple backend
backend ads {
.host ="ads.eood.cn";
.connect_timeout =1s;
.first_byte_timeout =30s;
.between_bytes_timeout =5s;
}
backend blog {
.host ="blog.eood.cn";
.connect_timeout =1s;
.first_byte_timeout =30s;
.between_bytes_timeout =5s;
}
These rules can be user IP addresses or even user cookies.
sub vcl_recv {
if(req.http.host ~"ads"|| req.url ~"^/ads/"){
set req.backend = ads;
...
}elseif(eq.http.host ~"blog"){
set req.backend = blog;
}
}
7. cache based on Rules
set beresp.ttl =120s;
Common Varnish O & M commands
Request URL hotspot ranking, Cache Policy Optimization Based on Hotspot
varnishtop -i rxurl
Real-time request log
varnishlog
You can use grep to filter and view the information you need.
varnishlog -c | grep 'google'
Varnish considerations
Avoid connection sticking. If you have multiple different backend servers and do not add them, this will lead to confusion. It is estimated that many people have stepped on this pitfall:
sub vcl_pipe {
set bereq.http.connection ="close";
}
URL Planning
Generally, the cache proxy or CDN can use different cache policies for different URL modes by configuring URL rules. Therefore, URL planning is very important.
Distinguishes dynamic requests from static requests, and distinguishes URLs of different cache levels. This also facilitates cache cleaning Based on URLs.
Last
Varnish is an essential cache module for website applications or mobile applications. If you have not started using it, add it to the existing architecture immediately.
Concepts of Cache Server Varnish
Concepts of Cache Server Varnish
Structural notes for Varnish Cache
Install and configure Varnish-5.8 in CentOS 2.1.5
The RedHat script uses the CentOS source to update and install Nginx, PHP 5.3, and Varnish.
Using Varnish to build Cache Server notes
Install and configure the cache service Varnish
Preparations for Varnish compilation and Installation
Configuration Optimization of Varnish cache in Linux
Varnish details: click here
Varnish: click here
This article permanently updates the link address: