# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
#backend default {
# .host = "127.0.0.1";
# .port = "80";
#}
#
# 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.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") {
# /* Non-RFC2616 or CONNECT which is weird. */
# return (pipe);
# }
# if (req.request != "GET" && req.request != "HEAD") {
# /* We only deal with GET and HEAD by default */
# return (pass);
# }
# if (req.http.Authorization || req.http.Cookie) {
# /* Not cacheable by default */
# 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);
# }
# return (hash);
# }
#
# sub vcl_hit {
# return (deliver);
# }
#
# sub vcl_miss {
# return (fetch);
# }
#
# sub vcl_fetch {
# 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;
# return (hit_for_pass);
# }
# return (deliver);
# }
#
# sub vcl_deliver {
# 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>Varnish cache server</p>
# </body>
# </html>
# "};
# return (deliver);
# }
#
# sub vcl_init {
# return (ok);
# }
#
# sub vcl_fini {
# return (ok);
# }
backend test1 {
.host = "127.0.0.1"; #这里也只能用ip而不能用域名,如果用域名的话以第一个为准
.port = "88";
#注释掉不然出现503错误
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
#注释掉不然出现503错误
}
#定义访问控制列表
acl purge {
"localhost";
"127.0.0.1";
"192.168.100.0"/24;
"192.168.99.0"/24;
"192.168.0.0"/24;
}
sub vcl_recv {
#开启压缩模式,图片格式取消压缩
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|jpeg|flv)" ) {
remove req.http.Accept-Encoding;
remove req.http.Cookie;
} 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 {
remove req.http.Accept-Encoding;
}
}
#根据host设置后端服务器
if (req.http.Host ~ "(?i)(myci.local.com|varnish.local.com|www.phpjx.com)") {
set req.backend = test1;
}else
if (req.http.Host ~ "(?i)image.wdj.com"){
set req.backend = test1;
}else
{
error 401 "Hostname not found";
}
#如果为purge请求,客户端ip不在访问列表中,返回405拒绝
if (req.request == "PURGE") {
if (!client.ip ~purge) {
error 405 "Not Allowed";
}
#本地缓存查找
return(lookup);
}
#如果为GET请求,url后缀为jpg,png,gif等 取出cookie
if (req.request == "GET"&&req.url ~ "(?i)\.(jpg|png|gif|swf|jpeg|ico)$") {
unset req.http.cookie;
}
#如果GET请求,url为php,则穿过cache,不缓存
if (req.request =="GET"&&req.url ~ "(?i)\.php($|\?)"){
#return (pass);
return (pipe);
}
#简单防盗链
if (req.http.referer ~ "http://.*") {
if ( !(req.http.referer ~ "http://.*test1\.com"
|| req.http.referer ~ "http://.*test2\.com"
|| req.http.referer ~ "http://.*wdj\.com"
|| req.http.referer ~ "http://.*google\.com"
|| req.http.referer ~ "http://.*baidu\.com"
|| req.http.referer ~ "http://.*yahoo\.cn"
|| req.http.referer ~ "http://.*phpjx\.com"
)) {
error 404 "Not Found!";
}
}
#获取客户端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;
}
# }
#不是以下请求进入pipe模块
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
#不是GET 和HEAD请求不缓存
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
#return (pass);
return (pipe);
}
if (req.http.Authorization) {
/* Not cacheable by default */
#return (pass);
return (pipe);
}
return (lookup);
}
#
sub vcl_pipe {
return (pipe);
}
#
sub vcl_pass {
return (pass);
}
#使用url+host hash算法查找数据
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}
# 如果请求为purge 将清除缓存
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged";
}
return (deliver);
}
sub vcl_miss {
return (fetch);
}
#
sub vcl_fetch {
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 0 s;
return (hit_for_pass);
}
if (beresp.http.Pragma ~"no-cache" ||
beresp.http.Cache-Control ~"no-cache" ||
beresp.http.Cache-Control ~"private") {
return (deliver);
}
#为特定格式文件设置缓存时间
if (req.request == "GET"&&req.url ~ "(?i)\.(js|css|mp3|jpg|png|gif|swf|jpeg|ico)$") {
set beresp.ttl = 30d;
#set beresp.ttl = 120s;
}
#san fen zhong nei ,
if (req.request == "GET"&&req.url ~ "(?i)\.(html|htm|txt)$") {
#set beresp.ttl = 30d;
set beresp.ttl = 120s;
}
return (deliver);
}
# 设置返回状态
sub vcl_deliver {
set resp.http.x-hits = obj.hits;
if (obj.hits > 0) {
set resp.http.X-Cache = "Hit phpjx.com";
}else {
set resp.http.X-Cache = "Miss phpjx.com";
}
set resp.http.Server = "phpjx";
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>Varnish cache server</p>
</body>
</html>
"};
return (deliver);
}
sub vcl_init {
return (ok);
}
sub vcl_fini {
return (ok);
}