Summary of the use of varnish

Source: Internet
Author: User
Tags varnish
Original source of the article and author information and this statement
Http://iyubo.blogbus.com/logs/35085709.html

This log will be updated at any time, of course, with the accumulation of my application:

implementing static file Compression

Varnish itself does not compress or decompress objects, although this has been on we wish list for quite a while. However, it would operate correctly with backends that support compression.

From the official website can be learned that the varnish itself does not provide compression, but we want to use compression, how to deal with it. (regarding the compression aspect may refer to the official website http://varnish.projects.linpro.no/wiki/FAQ/Compression)

The following configuration is added to the VCL_RECV to specify the compression algorithm for varnish and improve the efficiency. (even though there are few possible values for accept-encoding, varnish treats them-literally rather than semantically, so Even a small difference which makes no difference to the backend can reduce cache efficiency by making varnish cache Many different versions of an object.)

if (req.http.accept-encoding) {
if (req.url ~) \. ( Jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg) $ ") {
# No Point in compressing
Remove req.http.accept-encoding;
else if (req.http.accept-encoding ~ "gzip") {
Set req.http.accept-encoding = "gzip";
else if (req.http.accept-encoding ~ "deflate") {
Set req.http.accept-encoding = "deflate";
} else {
# Unkown algorithm
Remove req.http.accept-encoding;
}
}

In the Vcl_hash

Sub Vcl_hash {
Set Req.hash + = Req.url;
if (req.http.accept-encoding ~ "gzip") {
Set Req.hash + = "gzip";
}
else if (req.http.accept-encoding ~ "deflate") {
Set Req.hash + + "deflate";
}
Hash
}

This will be the compression of the work or to the backend server to do

add a header ID to hit

Sub Vcl_deliver {
        if (obj.hits > 0) {
                set resp.http.x-cache = "HIT";
        } else {
                set resp.http.x-cache = " MISS ";
        }
}

IP access restrictions on specific URLs

What if we only want certain IP to be accessible to certain URLs?

First define the IP list that is allowed to access

ACL Permit {
"10.1.1.5";
"10.0.2.5";
"10.10.1.3";
}

Then access control is made for the URL

if (req.url ~ "/test/.*" &&!client.ip ~ Permit) {
Error 403 "not allowed."
}

do not cache for a specific URL

Sub Vcl_fetch {
if (req.request = = "Get" && req.url ~ "/test/.*") {
set obj.ttl = 0s;
}
else {
Set Obj.ttl = 1800s;
}
#set obj.http.x-varnish-ip = Server.ip;
Set obj.http.Varnish = "Tested by Kevin";
Insert
}

clear the specified cache content

We can specify cache cleanup from the admin port by varnishadm this command

/usr/varnish/bin/varnishadm-t 127.0.0.1:3500 url.purge/test/*

/usr/varnish/bin/varnishadm-t 127.0.0.1:3500 Url.purge *$ (Clear all caches)

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.