Last active
August 29, 2015 14:07
-
-
Save tuutti/5173f6f31421c0d008d0 to your computer and use it in GitHub Desktop.
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
# | |
# This is an example VCL file for Varnish. | |
# | |
# It does not do anything by default, delegating control to the | |
# builtin VCL. The builtin VCL is called when there is no explicit | |
# return statement. | |
# | |
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ | |
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples. | |
# Marker to tell the VCL compiler that this VCL has been adapted to the | |
# new 4.0 format. | |
vcl 4.0; | |
import directors; | |
import std; | |
# Default backend definition. Set this to point to your content server. | |
# define our first nginx server | |
backend nginx01 { | |
.host = "10.2.0.69"; | |
.port = "80"; | |
.probe = { | |
.url = "/"; | |
.interval = 30s; | |
.timeout = 1 s; | |
.window = 5; | |
.threshold = 3; | |
} | |
} | |
# define our second nginx server | |
backend nginx02 { | |
.host = "10.2.0.23"; | |
.port = "80"; | |
.probe = { | |
.url = "/"; | |
.interval = 30s; | |
.timeout = 1 s; | |
.window = 5; | |
.threshold = 3; | |
} | |
} | |
# configure the load balancer | |
sub vcl_init { | |
new rr = directors.round_robin(); | |
rr.add_backend(nginx01); | |
rr.add_backend(nginx02); | |
} | |
# When a request is made set the backend to the round-robin director named nginx | |
sub vcl_recv { | |
set req.backend_hint = rr.backend(); | |
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.method != "GET" && req.method != "HEAD") { | |
/* We only deal with GET and HEAD by default */ | |
return (pass); | |
} | |
# Remove all cookies that Drupal doesn't need to know about. ANY remaining | |
# cookie will cause the request to pass-through to Apache. For the most part | |
# we always set the NO_CACHE cookie after any POST request, disabling the | |
# Varnish cache temporarily. The session cookie allows all authenticated users | |
# to pass through as long as they're logged in. | |
if (req.http.Cookie) { | |
# Prepend semicolon for the regexes below to work. | |
set req.http.Cookie = ";" + req.http.Cookie; | |
# Remove spaces after the semicolons separating cookies. | |
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); | |
# Put space folloing semicolon back for whitelisted cookies. | |
set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[0-9a-f]+|NO_CACHE|csrftoken|sessionid|[a-z_]+session|sam_currency_country)=", "; \1="); | |
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", ""); | |
# Remove cookies not preceeded by a space. | |
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); | |
# Strip semicolons and spaces from the start and the end of the cookie string. | |
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); | |
if (req.http.Cookie == "") { | |
# If there are no remaining cookies, remove the cookie header. If there | |
# aren't any cookie headers, Varnish's default behavior will be to cache | |
# the page. | |
unset req.http.Cookie; | |
} | |
else { | |
# If there are any cookies left (a session or NO_CACHE cookie), do not | |
# cache the page. Pass it on to Apache directly. | |
return (pass); | |
} | |
} | |
// Skip the Varnish cache for install, update, and cron | |
if (req.url ~ "install\.php|update\.php|cron\.php") { | |
return (pass); | |
} | |
return (hash); | |
} | |
sub vcl_deliver { | |
set resp.http.grace = req.http.grace; | |
} | |
sub vcl_backend_response { | |
if (bereq.url ~ "\.(png|gif|jpg|swf|css|js)$") { | |
// For Varnish 2.0 or earlier, replace beresp with obj: | |
// unset obj.http.set-cookie; | |
unset beresp.http.set-cookie; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment