具体的实例,假设我们有一个网站www.example.com, 需要增加varnish,来提升性能。
http header头信息中的 Cache-Control: s-maxage 参数可以设置页面在varnish中缓存的时间
nginx配置:
server {
listen 80
server_name www.example.com;
location / {
proxy_pass http: // 127.0 . 0.1 : 800 ; # 反向代理到varnish
proxy_set_header Host $host ;
proxy_set_header X - Real - IP $remote_addr ;
proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for ;
}
}
server {
listen 8080 ;
server_name www.example.com;
root / var / www / example;
}
listen 80
server_name www.example.com;
location / {
proxy_pass http: // 127.0 . 0.1 : 800 ; # 反向代理到varnish
proxy_set_header Host $host ;
proxy_set_header X - Real - IP $remote_addr ;
proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for ;
}
}
server {
listen 8080 ;
server_name www.example.com;
root / var / www / example;
}
example.vcl配置:
backend
default
{
.host = " 127.0.0.1 " ;
.port = " 8080 " ;
.connect_timeout = 5s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 5s;
}
sub vcl_recv {
set req.backend = default ;
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_hit {
# 只要页面刷新(F5、CTRL+F5),页面的varnish cache均失效
if (req.http.Cache - Control~ " no-cache " || req.http.Cache - Control~ " max-age=0 " || req.http.Pragma~ " no-cache " ){
set obj.ttl = 0s;
return (restart);
}
return (deliver);
}
sub vcl_deliver {
set resp.http.x - hits = obj.hits;
if (obj.hits > 0 ){
set resp.http.X - Cache = " hit " ;
}
else {
set resp.http.X - Cache = " MISS " ;
}
return (deliver);
}
sub vcl_fetch {
if ( ! beresp.cacheable) {
return (pass);
}
if (beresp.http.Set - Cookie) {
return (pass);
}
# if(beresp.http.Pragma~"no-cache"||beresp.http.Cache-Control~"no-cache"||beresp.http.Cache-Control~"private"){
# return (pass);
# }
if (req.request == " GET " && req.http.host~ " bbs.xiazh.dev.aifang.com$ " ){
set beresp.ttl = 1200s;
}
if (req.request == " GET " && req.url~ " \.(png|swf|txt|jpg|css|js|html|htm)$ " ){
set beresp.ttl = 3600s;
}
return (deliver);
}
.host = " 127.0.0.1 " ;
.port = " 8080 " ;
.connect_timeout = 5s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 5s;
}
sub vcl_recv {
set req.backend = default ;
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_hit {
# 只要页面刷新(F5、CTRL+F5),页面的varnish cache均失效
if (req.http.Cache - Control~ " no-cache " || req.http.Cache - Control~ " max-age=0 " || req.http.Pragma~ " no-cache " ){
set obj.ttl = 0s;
return (restart);
}
return (deliver);
}
sub vcl_deliver {
set resp.http.x - hits = obj.hits;
if (obj.hits > 0 ){
set resp.http.X - Cache = " hit " ;
}
else {
set resp.http.X - Cache = " MISS " ;
}
return (deliver);
}
sub vcl_fetch {
if ( ! beresp.cacheable) {
return (pass);
}
if (beresp.http.Set - Cookie) {
return (pass);
}
# if(beresp.http.Pragma~"no-cache"||beresp.http.Cache-Control~"no-cache"||beresp.http.Cache-Control~"private"){
# return (pass);
# }
if (req.request == " GET " && req.http.host~ " bbs.xiazh.dev.aifang.com$ " ){
set beresp.ttl = 1200s;
}
if (req.request == " GET " && req.url~ " \.(png|swf|txt|jpg|css|js|html|htm)$ " ){
set beresp.ttl = 3600s;
}
return (deliver);
}