CentOS 7.0 varnishキャッシュの構成

5596 ワード

1、varnishのインストール
rpm -ivh http://mirrors.opencas.cn/epel/epel-release-latest-7.noarch.rpm
yum install -y varnish
systemctl start varnish
systemctl enable varnish

2、リスニングポートの設定
vim /etc/varnish/varnish.params
    VARNISH_LISTEN_PORT=6081

3、バックエンドサーバーの設定
vim /etc/varnish/default.vcl
backend default {
    .host = "172.16.1.21";
    .port = "80";
}

4、VCLファイルの構成
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "80";
}
sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
    if (req.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") {
        return (pass);
    }
     
    if (req.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {
        unset req.http.cookie;
        return (hash);
    }
     
    if (req.restarts == 0) {
        if (req.http.x-forwarded-for) {
            set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
        } else {
            set req.http.X-Forwarded-For = client.ip;
        }
    }
    if (req.http.Cache-Control ~ "(?i)no-cache") {
        if (!(req.http.Via || req.http.User-Agent ~ "(?i)bot" || req.http.X-Purge)) {
            return (purge);
        }
    }
     
    if (req.method != "GET" && 
        req.method != "HEAD" && 
        req.method != "PUT" && 
        req.method != "POST" && 
        req.method != "TRACE" && 
        req.method != "OPTIONS" && 
        req.method != "PATCH" && 
        req.method != "DELETE") {        
        return (pipe);
    }
     
    if (req.method != "GET" && req.method != "HEAD") {
        return (pass);
    }
     
    if (req.http.Authorization) {
        return (pass);
    }
     
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$") {
            unset req.http.Accept-Encoding;        
        } elseif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elseif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            unset req.http.Accept-Encoding;
        }
    }
    if (req.http.Upgrade ~ "(?i)websocket") {
        return (pipe);
    }
     
     
    if (req.http.x-pipe && req.restarts > 0) {
        unset req.http.x-pipe;
        return (pipe);
    }
     
    return (hash);
}
sub vcl_pipe {
    if (req.http.upgrade) {
        set bereq.http.upgrade = req.http.upgrade;
    }
     
    return (pipe);
}
 
sub vcl_pass {
    if (req.method == "PURGE") {
        return (synth(502, "PURGE on a passed object."));
    }
}
 
sub vcl_hash {
    hash_data(req.url);
     
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
     
    if (req.http.Cookie) {
        hash_data(req.http.Cookie);
    }
     
    if (req.http.Accept-Encoding ~ "gzip") {
        hash_data("gzip");
    } elseif (req.http.Accept-Encoding ~ "deflate") {
        hash_data("deflate");
    }
}
 
sub vcl_hit {
    if (req.method == "PURGE") {
        return (synth(200, "Purged."));
    }
     
    if (obj.ttl >= 0s) {
        return (deliver);
    }
     
    return (deliver);
}
sub vcl_miss {
    if (req.method == "PURGE") {
        return (synth(404, "Purged."));
    }
     
    return (fetch);
}
sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}
sub vcl_purge {
    if (req.method != "PURGE") {
        set req.http.X-Purge = "Yes";
        return (restart);
    }
}
#        X-Cache
sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
     
    unset resp.http.X-Powered-By;
    unset resp.http.Server;
     
    unset resp.http.Via;
    unset resp.http.X-Varnish;
     
    unset resp.http.Age;
}

5、chromeブラウザでヒットしたかどうか
CentOS 7.0 配置varnish缓存_第1张图片
参考文献:http://sofar.blog.51cto.com/353572/1653683/