Last active
December 10, 2015 00:58
-
-
Save newphp/4354852 to your computer and use it in GitHub Desktop.
/etc/varnish/default.vcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
backend default { | |
.host = "127.0.0.1"; | |
.port = "80"; | |
.connect_timeout = 1s; | |
.first_byte_timeout = 30s; | |
} | |
backend siteapp { | |
.host = "10.4.83.14"; | |
.port = "80"; | |
.connect_timeout = 2s; | |
.first_byte_timeout = 300s; | |
} | |
backend google_commondatastorage { | |
.host = "173.194.67.132"; | |
.port = "80"; | |
.connect_timeout = 2s; | |
.first_byte_timeout = 300s; | |
} | |
acl purge { | |
"127.0.0.1"; | |
} | |
sub vcl_recv { | |
if (req.request == "PURGE") { | |
if (!client.ip ~ purge) { | |
error 405 "Not Allowed."; | |
return (lookup); | |
} | |
} | |
set req.backend = default; | |
if (req.http.host == "static.domain.com") { | |
set req.backend = google_commondatastorage; | |
set req.url = "/" + req.http.host + req.url; | |
set req.http.host = "commondatastorage.googleapis.com"; | |
## varnish hotlink for img | |
if (!(req.http.referer ~ "http://.*\.domain\.com/" || | |
req.http.referer ~ "http://.*\.domain\.net/") && | |
req.http.referer && | |
req.url ~ "^[^\?]+\.(jpg|jpeg|png|gif)$") | |
{ | |
error 403; | |
} | |
} | |
if (req.http.host == "www.domain.com" || req.http.host == "domain.com") { | |
set req.backend = siteapp; | |
} | |
if (! req.backend.healthy) { | |
set req.grace = 30s; | |
} | |
else { | |
set req.grace = 2h; | |
} | |
# 移除静态文件的cookie | |
if ((req.request == "GET" || req.request == "HEAD") && | |
req.url ~ "^[^\?]+\.(jpg|jpeg|png|gif|swf|ico|txt|css|js)($|\?)") { | |
unset req.http.cookie; | |
set req.url = regsub(req.url, "\?.*$", ""); | |
} | |
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") { | |
return (pass); | |
} | |
if (req.url ~ "\.(php|php5)($|\?)") { | |
return (pass); | |
} | |
if (req.http.Authorization || req.http.Cookie) { | |
return (pass); | |
} | |
if (req.http.Cache-Control == "no-cache") { | |
ban_url(req.url); | |
} | |
return (lookup); | |
} | |
sub vcl_pipe { | |
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 { | |
if (!obj.ttl > 0s) | |
{ | |
return (pass); | |
} | |
if (req.request == "PURGE") | |
{ | |
set obj.ttl = 0s; | |
error 200 "Purged."; | |
} | |
return (deliver); | |
} | |
sub vcl_miss | |
{ | |
if (req.request == "PURGE") | |
{ | |
error 404 "Not in cache."; | |
} | |
return (fetch); | |
} | |
sub vcl_fetch | |
{ | |
if (req.request == "GET" || req.request == "HEAD") | |
{ | |
if (req.url ~ "^[^\?]+\.(jpeg|jpg|gif|png|swf)$") | |
{ | |
remove beresp.http.Set-Cookie; | |
set beresp.ttl = 7 d; | |
} | |
if (req.url ~ "^[^\?]+\.(css|js|ico|txt)$") | |
{ | |
remove beresp.http.Set-Cookie; | |
set beresp.ttl = 3600 s; | |
} | |
if (req.url ~ "^[^\?]+\.(html|htm)$") | |
{ | |
set beresp.ttl = 600 s; | |
} | |
} | |
if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") | |
{ | |
set beresp.ttl = 600 s; | |
return (hit_for_pass); | |
} | |
return (deliver); | |
} | |
sub vcl_deliver { | |
if (obj.hits > 0) { | |
set resp.http.X-Cache = "HIT from Frontend"; | |
} | |
else { | |
set resp.http.X-Cache = "MISS from Frontend"; | |
} | |
remove resp.http.X-Varnish; | |
remove resp.http.Via; | |
remove resp.http.X-Powered-By; | |
remove resp.http.Vary; | |
remove resp.http.x-goog-sequence-number; | |
remove resp.http.x-goog-metageneration; | |
remove resp.http.x-goog-generation; | |
return (deliver); | |
} | |
sub vcl_error { | |
set obj.http.Content-Type = "text/html; charset=utf-8"; | |
set obj.http.Retry-After = "5"; | |
synthetic {"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | |
<html> | |
<head><title>"} + obj.status + " " + obj.response + {"</title></head> | |
<body bgcolor="white"> | |
<h1>"} + obj.response + {"</h1> | |
</body> | |
</html>"}; | |
return (deliver); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment