博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Varnish使用小结
阅读量:1983 次
发布时间:2019-04-27

本文共 2031 字,大约阅读时间需要 6 分钟。

 文章原始出处和作者信息及

此日志会随时更新,当然,是随着我的应用积累:)

实现静态文件压缩

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

从官方网站可以得知,Varnish本身并不能提供压缩的功能,但是我们又想要使用压缩,该怎么处理呢?(有关压缩的方面可以参考官方网站)

在vcl_recv中加入如下配置,为Varnish指定压缩算法,提高效率。(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 too 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 these
                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;
                }
        }

在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;
}

这样就把压缩的工作还是交给后端服务器去做

添加一个Header标识是否命中

sub vcl_deliver {        if (obj.hits > 0) {                set resp.http.X-Cache = "HIT";        } else {                set resp.http.X-Cache = "MISS";        }}

对特定URL进行IP访问限制

如果我们对某些特定的URL只想某些IP能访问,又该怎么办呢?

首先定义允许访问的IP列表

acl permit {

        "10.1.1.5";
        "10.0.2.5";
        "10.10.1.3";
}

然后是针对URL进行访问控制

                if (req.url ~ "/test/.*" && !client.ip ~ permit) {

                        error 403 "Not Allowed.";
                }

对特定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;
}

清除指定缓存内容

我们可以通过varnishadm这个命令从管理端口进行指定缓存的清除

/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 *$ (清除所有缓存)

转载地址:http://atwvf.baihongyu.com/

你可能感兴趣的文章
python2和python3对比Urllib
查看>>
module pip has no attribute main问题解决
查看>>
LeetCode 120.Triangle (三角形最小路径和)
查看>>
线程池的原理及C++线程池的封装实现
查看>>
常见的多线程并发服务器设计模型
查看>>
LeetCode 121.Best Time to Buy and Sell Stock (买卖股票的最佳时机)
查看>>
LeetCode 122.Best Time to Buy and Sell Stock II (买卖股票的最佳时机 II)
查看>>
LeetCode 124.Binary Tree Maximum Path Sum (二叉树中的最大路径和)
查看>>
LeetCode 125.Valid Palindrome (验证回文串)
查看>>
LeetCode 128.Longest Consecutive Sequence (最长连续序列)
查看>>
LeetCode 129.Sum Root to Leaf Numbers (求根到叶子节点数字之和)
查看>>
LeetCode 130.Surrounded Regions (被围绕的区域)
查看>>
muduo网络库——详解muduo多线程模型
查看>>
muduo网络库源码分析——整体架构
查看>>
LeetCode 131.Palindrome Partitioning (分割回文串)
查看>>
LeetCode 132.Palindrome Partitioning II (分割回文串 II)
查看>>
LeetCode 134.Gas Station (加油站)
查看>>
LeetCode 135.Candy (分发糖果)
查看>>
LeetCode 136.Single Number (只出现一次的数字)
查看>>
LeetCode 137.Single Number II (只出现一次的数字 II)
查看>>