Last active
December 29, 2015 20:59
-
-
Save ahmedsbytes/7727593 to your computer and use it in GitHub Desktop.
My Varnish Files
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
# server. | |
# | |
backend default { | |
.host = "10.0.1.153"; | |
.port = "80"; | |
# .probe = { | |
# .url = "/index.php"; | |
# .interval = 3s; | |
# .timeout = 2s; | |
# .window = 5; | |
# .threshold = 3; | |
# } | |
} | |
backend websiteServer1 { | |
.host = "10.0.1.151"; | |
.port = "80"; | |
.probe = { | |
.interval = 3s; | |
.timeout = 2s; | |
.window = 3; | |
.threshold = 2; | |
.request = | |
"GET / HTTP/1.1" | |
"Host: www.website.com" | |
"Connection: close"; | |
} | |
} | |
backend websiteServer2 { | |
.host = "10.0.1.152"; | |
.port = "80"; | |
.probe = { | |
.interval = 3s; | |
.timeout = 2s; | |
.window = 3; | |
.threshold = 2; | |
.request = | |
"GET / HTTP/1.1" | |
"Host: www.website.com" | |
"Connection: close"; | |
} | |
} | |
backend websiteServer3 { | |
.host = "10.0.1.156"; | |
.port = "80"; | |
.probe = { | |
.interval = 3s; | |
.timeout = 2s; | |
.window = 3; | |
.threshold = 2; | |
.request = | |
"GET / HTTP/1.1" | |
"Host: www.website.com" | |
"Connection: close"; | |
} | |
} | |
backend websiteServer4 { | |
.host = "10.0.1.157"; | |
.port = "80"; | |
.probe = { | |
.interval = 3s; | |
.timeout = 2s; | |
.window = 3; | |
.threshold = 2; | |
.request = | |
"GET / HTTP/1.1" | |
"Host: www.website.com" | |
"Connection: close"; | |
} | |
} | |
director websiteServer_director round-robin { | |
{ .backend = websiteServer1; } | |
{ .backend = websiteServer2; } | |
{ .backend = websiteServer3; } | |
{ .backend = websiteServer4; } | |
} | |
#vcl_recv is the function which requests varnish will handle | |
sub vcl_recv { | |
#stale object serving | |
if (! req.backend.healthy) { | |
set req.grace = 5m; | |
} else { | |
set req.grace = 15s; | |
} | |
#Director Controlling | |
if ( req.http.host ~ "((?i)website.com|media.websitestatic.com)$" ) | |
{ | |
set req.http.X-Infra = "BigServer"; | |
set req.backend = websiteServer_director; | |
}else{ | |
set req.http.X-Infra = "OtherSmallServers"; | |
set req.backend = default; | |
} | |
#this to fix some ip things | |
if (req.restarts == 0) { | |
if (req.http.x-forwarded-for) { | |
#unset req.http.X-Forwarded-For; | |
#set req.http.X-Forwarded-For = client.ip; | |
}else{ | |
set req.http.X-Forwarded-For = client.ip; | |
} | |
} | |
#disable ETAG | |
remove req.http.Etag; | |
# req.http.host is variable that holds the request host/doamin name | |
# the ~ is used to match this as regex | |
if (req.http.host ~ "^(www1.website.com|www2.website.com|media.websitestatic.com)$") { | |
#if (req.request == "PURGE") { | |
# #if (!client.ip ~ purge) { | |
# # error 405 "Not allowed."; | |
# #} | |
# return (lookup); | |
#} | |
#this delete cookies from media http get requests (varnish stop when there's cookie) | |
#and find it in cache | |
#if (req.request == "GET" && req.url ~ "\.(js|css|gif|png|woff|jpg)" ) { | |
# unset req.http.cookie; | |
# return (lookup); | |
#} | |
if (req.request == "GET" && ( | |
# req.url ~ "/(en|ar)/search(.*)" || | |
req.url ~ "/(en|ar)/account(.*)" || | |
req.url ~ "/(en|ar)/reset(.*)" || | |
req.url ~ "index_dev.php(.*)" | |
) ) { | |
return (pass); | |
} | |
### Cookie normalization | |
# Remove has_js and Google Analytics __* cookies. | |
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", ""); | |
# Remove a ";" prefix, if present. | |
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", ""); | |
# Remove empty cookies. | |
if (req.http.Cookie ~ "^\s*$") { | |
unset req.http.Cookie; | |
} | |
# varnish will not care for any non standard request (for security, not needed much) | |
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); | |
} | |
#varnish will handle GET and HEAD requests only (NO POST request) | |
if (req.request != "GET" && req.request != "HEAD") { | |
/* We only deal with GET and HEAD by default */ | |
return (pass); | |
} | |
#### This will disable varnish when it find cookie or Releam Authorization | |
#if (req.http.Authorization || req.http.Cookie) { | |
# /* Not cacheable by default */ | |
# return (pass); | |
#} | |
#If cookie contains welcome=1 , varnish will not handle it | |
if (req.http.Cookie ~ "(^|;\s*)(welcome=1)(;|$)"){ | |
return (pass); | |
} | |
#if varnish reached this step , probably there;s no need for cookie (cookie == cache per user) | |
unset req.http.Cookie; | |
return (lookup); | |
} | |
#varnish will pass/ignore anything not in the domains listed above | |
return (pass); | |
} | |
# 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 { | |
# set obj.http.X-PASS = "true"; | |
# rturn (pass); | |
#} | |
#this function tells varnish to make the has (cache key) based on what | |
sub vcl_hash { | |
hash_data(req.http.Cookie); | |
hash_data(req.url); | |
hash_data(req.http.host); | |
return (hash); | |
} | |
#sub vcl_hit { | |
# set beresp.http.X-Cached = "DamnYes"; | |
# return (deliver); | |
#} | |
# | |
# sub vcl_miss { | |
# return (fetch); | |
# } | |
# We can set varnish ttl (cache time) and u can add extra response header too | |
sub vcl_fetch { | |
#Doing some tweaks | |
if (beresp.status == 500) { | |
set beresp.saintmode = 3s; | |
return(restart); | |
} | |
set beresp.grace = 30m; | |
if (beresp.status != 200) { | |
set beresp.ttl = 0s; | |
return (deliver); | |
} | |
# if (req.request == "GET" && req.http.host ~ "media.websitestatic.com" ) { | |
# unset beresp.http.set-cookie; | |
# set beresp.ttl = 30d; | |
# return (deliver); | |
# } | |
unset beresp.http.Etag; | |
#unset beresp.http.Cache-Control; | |
#set media ttl | |
#if (req.request == "GET" && req.url ~ "\.(js|css|gif|png|woff|jpg)" ) { | |
# set beresp.ttl = 30d; | |
# return (deliver); | |
# } | |
if (req.request == "GET" && req.url ~ "/(en|ar)/new-car" ) { | |
set beresp.ttl = 2h; | |
return (deliver); | |
} | |
if (req.request == "GET" && req.url == "/(en|ar)/car" ) { | |
set beresp.ttl = 15m; | |
return (deliver); | |
} | |
set beresp.ttl = 1h; | |
if (beresp.status == 404) { | |
set beresp.ttl = 2s; | |
} | |
return (deliver); | |
} | |
sub vcl_deliver { | |
#deliver | |
if (obj.hits > 0) { | |
set resp.http.X-Cache = "HIT"; | |
} else { | |
set resp.http.X-Cache = "MISS"; | |
} | |
} | |
sub vcl_hit { | |
if (req.request == "PURGE") { | |
purge; | |
error 200 "Purged."; | |
} | |
} | |
sub vcl_miss { | |
if (req.request == "PURGE") { | |
purge; | |
error 200 "Purged."; | |
} | |
} | |
# | |
# 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); | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment