높 은 명중률 의 varnish 캐 시 설정 공유
시스템:centos 5.x
소프트웨어:varnish-3.0.5
1.varnish 설치
어떻게 설치 하 는 지 는 말 하지 않 겠 습 니 다.직접 찾 아 보 겠 습 니 다.
2.varnish 설정
backend slogra {
.host = "172.0.0.1";
.port = "80";
.connect_timeout = 20s;
.first_byte_timeout = 20s;
.between_bytes_timeout = 20s;
}
#
acl purgeAllow {
#
"localhost";
}
# Below is a commented-out copy of the default VCL logic. If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
sub vcl_recv {
# ,
if(req.http.host ~ "^(.*)(slogra.com)")
{
set req.backend=slogra;
}else{
error 408 "Hostname not found";
}
#grace
# backend , grace 5s, backend , grace 1m。
# ,5s ;
# 1m ,backend , , backend 。。。
if (req.backend.healthy) {
set req.grace = 5s;
} else {
set req.grace = 1m;
}
#
if (req.request == "PURGE"){
if(!client.ip ~ purgeAllow) {
error 405 "Not allowed.";
}
# # hit miss
return (lookup);
}
# cookie
if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)" ) {
# cookie, varnish
unset req.http.cookie;
}
#Accept-Encoding ,
# Accept-Encoding
# deflate, gzip
if (req.http.Accept-Encoding) {
if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz)($|\?)" ) {
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 if (req.http.Accept-Encoding ~ "sdch"){
#chrome
set req.http.Accept-Encoding = "sdch";
}else {
remove req.http.Accept-Encoding;
}
}
# X-Forwarded-For , ip
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.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization) {
/* Not cacheable by default */
return (pass);
}
#js,css Cookie,
#if (req.http.Cookie) {
# /* Not cacheable by default */
# return (pass);
#}
#
if (req.url ~ "^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)") {
return (pass);
}
return (lookup);
}
sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want to
# have it set for all requests, make sure to have:
# set bereq.http.connection = "close";
# here. It is not set by default as it might break some broken web
# applications, like IIS with NTLM authentication.
return (pipe);
}
# ,
sub vcl_pass {
return (pass);
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
# ,
if(req.http.Accept-Encoding){
hash_data(req.http.Accept-Encoding);
}
return (hash);
}
# lookup :hit
sub vcl_hit {
# , TTL 0,
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
# ( )
return (deliver);
}
# lookup :miss
sub vcl_miss {
# ,
#if (req.request == "PURGE") {
# error 404 "Not in cache.";
#}
# ( )
return (fetch);
}
# ,
sub vcl_fetch {
#
# ,
if (req.url ~ "^(.*)\.(php|jsp|do|aspx|asmx|ashx)($|.*)") {
set beresp.http.Cache-Control="no-cache, no-store";
unset beresp.http.Expires;
return (deliver);
}
# , beresp.grace, , beresp.grace
if (beresp.ttl > 0s) {
set beresp.grace = 1m;
}
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120 s;
# lookup, pass
return (hit_for_pass);
}
# TTL
if (req.url ~ "^(.*)\.(pdf|xls|ppt|doc|docx|xlsx|pptx|chm|rar|zip)($|\?)")
{
# cookie
unset beresp.http.Set-Cookie;
#
set beresp.ttl = 30d;
return (deliver);
}else if(req.url ~ "^(.*)\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm)($|\?)"){
# cookie
unset beresp.http.Set-Cookie;
#
set beresp.ttl = 15d;
return (deliver);
}else if(req.url ~ "^(.*)\.(mp3|wma|mp4|rmvb|ogg|mov|avi|wmv|mpeg|mpg|dat|3pg|swf|flv|asf)($|\?)"){
# cookie
unset beresp.http.Set-Cookie;
#
set beresp.ttl = 30d;
return (deliver);
}
# response , ,
if (beresp.http.Pragma ~"no-cache" || beresp.http.Cache-Control ~"no-cache" || beresp.http.Cache-Control ~"private") {
return (deliver);
}
return (deliver);
}
#
sub vcl_deliver {
# Header , 。
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from cache";
#set resp.http.X-Varnish = "HIT from cache";
} else {
set resp.http.X-Cache = "MISS from cache";
#set resp.http.X-Varnish = "MISS from cache";
}
# header
unset resp.http.Vary;
unset resp.http.X-Powered-By;
unset resp.http.X-AspNet-Version;
return (deliver);
}
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
set obj.http.Retry-After = "5";
synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>"} + obj.status + " " + obj.response + {"</title>
</head>
<body>
<h1>Error "} + obj.status + " " + obj.response + {"</h1>
<p>"} + obj.response + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>cache server</p>
</body>
</html>
"};
return (deliver);
}
sub vcl_init {
return (ok);
}
sub vcl_fini {
return (ok);
}
3.효과 도자,여러분 이 관심 이 있 으 면 직접 하 셔 도 됩 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
varnish 4.0 공식 문서 번역 16-Backend servers/M/B/D/Hvarnish는 여러 개의 백엔드를 정의할 수도 있고, 몇 개의 백엔드를 한 백엔드 집단에 두는 것도 부하 균형의 목적에 도달할 수 있다. 어떤 경우 varnish에 여러 개의 백엔드 내용을 캐시해야 할 수도 있습니...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.