Play Varnish cache proxy

Source: Internet
Author: User
Tags password protection varnish

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
  1. Push content to users' CDN Cache
  2. Cache proxy similar to Varnish
  3. Application Layer cache Redis and Memcache
  4. Database cache

 

Why is cache proxy important?
  1. 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.
  2. Latency usually has a great impact on user experience and business. All Internet companies are trying to reduce latency and improve user experience.
  3. 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

  1. unset req.http.cookie;
  2. set req.http.X-EOOD ="EOOD";

Modify the Response Header

  1. unset beresp.http.Set-Cookie;
  2. set beresp.http.Cache-Control="public, max-age=31536000";

 

2. Content exposure in the Varnish state

Cache hit or not

  1. if(obj.hits >0){
  2. set resp.http.X-Cache="HIT";
  3. }else{
  4. set resp.http.X-Cache="MISS";
  5. }

 

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.

  1. set req.grace =48h;

 

5. Access Control

You can route or shield certain accesses according to rules, for example:

  1. req.http.User-Agent
  2. req.http.X-Forwarded-For
  3. req.http.referer
  4. ...

Simple Password protection: Basic Auth

  1. if(! req.http.Authorization~"Basic XXXXXXX"){
  2. error 401"Restricted";
  3. }

 

6. Merge multiple backend instances

Define multiple backend

  1. backend ads {
  2. .host ="ads.eood.cn";
  3. .connect_timeout =1s;
  4. .first_byte_timeout =30s;
  5. .between_bytes_timeout =5s;
  6. }
  7. backend blog {
  8. .host ="blog.eood.cn";
  9. .connect_timeout =1s;
  10. .first_byte_timeout =30s;
  11. .between_bytes_timeout =5s;
  12. }

These rules can be user IP addresses or even user cookies.

  1. sub vcl_recv {
  2. if(req.http.host ~"ads"|| req.url ~"^/ads/"){
  3. set req.backend = ads;
  4. ...
  5. }elseif(eq.http.host ~"blog"){
  6. set req.backend = blog;
  7. }
  8. }

 

7. cache based on Rules
  1. set beresp.ttl =120s;

 

Common Varnish O & M commands

Request URL hotspot ranking, Cache Policy Optimization Based on Hotspot

  1. varnishtop -i rxurl

Real-time request log

  1. varnishlog

You can use grep to filter and view the information you need.

  1. 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:

  1. sub vcl_pipe {
  2. set bereq.http.connection ="close";
  3. }

 

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:

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.