Skip to content

Instantly share code, notes, and snippets.

@mesaque
Last active May 23, 2016 21:41
Show Gist options
  • Save mesaque/d0359ae50adecfe6737e792e48d2fd3d to your computer and use it in GitHub Desktop.
Save mesaque/d0359ae50adecfe6737e792e48d2fd3d to your computer and use it in GitHub Desktop.
#
# 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.
# Update of varnish 4 to work with wordpress
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "172.17.0.1";
.port = "8080";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}
# Only allow purging from specific IPs
acl purge {
"localhost";
"127.0.0.1";
"172.17.0.1";
}
# This function is used when a request is send by a HTTP client (Browser)
sub vcl_recv {
# Normalize the header, remove the port (in case you're testing this on various TCP ports)
set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
# Remove has_js and CloudFlare/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*", "");
# Allow purging from ACL
if (req.method == "PURGE") {
# If not allowed then a error 405 is returned
if (!client.ip ~ purge) {
return(synth(405, "This IP is not allowed to send PURGE requests."));
}
# If allowed, do a cache_lookup -> vlc_hit() or vlc_miss()
if ( req.url ~ ".*" ) {
ban("req.url ~ " + req.url);
}
return(purge);
}
# Cache the following files extensions
##if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
## unset req.http.cookie;
##}
## unset req.http.cookie;
if (req.http.host == "correiodeuberlandia.com.br" || req.http.host == "www.correiodeuberlandia.com.br" || req.http.host == "www1.correiodeuberlandia.com.br" || req.http.host == "www2.correiodeuberlandia.com.br") {
set req.backend_hint = default;
}
### Only cache GET or HEAD requests. This makes sure the POST (and OPTIONS) requests are always passed.
if (req.method != "GET" && req.method != "HEAD") {
return (pass); // DO NOT CACHE
}
#Escape Assine
if ( req.url ~ "^/assine/*" ) {
return (pass);
}
# Wordpress: disable caching for some parts of the backend (mostly admin stuff)
# and WP search results.
# --- WordPress specific configuration
# Did not cache the admin and login pages
if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") {
return (pass);
}
# Remove the "has_js" cookie
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
# Remove any Google Analytics based cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
# Remove the Quant Capital cookies (added by some plugin, all __qca)
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");
# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");
# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");
# Are there cookies left with only spaces or that are empty?
if (req.http.cookie ~ "^ *$") {
unset req.http.cookie;
}
# Cache the following files extensions
if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
unset req.http.cookie;
}
# Normalize Accept-Encoding header and compression
# https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
if (req.http.Accept-Encoding) {
# Do no compress compressed files...
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
unset req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}
# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
return (pass);
}
if (!req.http.cookie) {
unset req.http.cookie;
}
# --- End of WordPress specific configuration
##if (req.url ~ "^/tryme/") {
## # do not use the cache
## return(pass); // DO NOT CACHE
##}
# WooCommerce
##if (req.url ~ "\?add-to-cart=") {
## # do not use the cache
## return(pass); // DO NOT CACHE
##}
# Kick DFind requests
##if (req.url ~ "^/w00tw00t") {
## return (synth(404, "Not Found"));
##}
###
### http header Cookie
### Remove some cookies (if found).
###
# https://www.varnish-cache.org/docs/4.0/users-guide/increasing-your-hitrate.html#cookies
# Unset the header for static files
if (req.url ~ "\.(css|flv|gif|htm|html|ico|jpeg|jpg|js|mp3|mp4|pdf|png|swf|tif|tiff|xml)(\?.*|)$") {
unset req.http.Cookie;
}
if (req.http.cookie) {
# Google Analytics
set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(__utm[a-z]+)=([^;]*)", "");
set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(_ga)=([^;]*)", "");
# Quant Capital
set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(__qc[a-z]+)=([^;]*)", "");
# __gad __gads
set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(__gad[a-z]+)=([^;]*)", "");
# Google Cookie consent (client javascript cookie)
set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(displayCookieConsent)=([^;]*)", "");
# Other known Cookies: remove them (if found).
set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(__CT_Data)=([^;]*)", "");
set req.http.Cookie = regsuball( req.http.Cookie, "(^|;\s*)(WRIgnore|WRUID)=([^;]*)", "");
# PostAction: Remove (once and if found) a ";" prefix followed by 0..n whitespaces.
# INFO \s* = 0..n whitespace characters
set req.http.Cookie = regsub( req.http.Cookie, "^;\s*", "" );
# PostAction: Unset the header if it is empty or 0..n whitespaces.
if ( req.http.cookie ~ "^\s*$" ) {
unset req.http.Cookie;
}
}
###
### Normalize the Accept-Language header
### We do not need a cache for each language-country combination! Just keep en-* and nl-* for future use.
### https://www.varnish-cache.org/docs/4.0/users-guide/increasing-your-hitrate.html#http-vary
if (req.http.Accept-Language) {
if (req.http.Accept-Language ~ "^en") {
set req.http.Accept-Language = "en";
} elsif (req.http.Accept-Language ~ "^nl") {
set req.http.Accept-Language = "nl";
} else {
# Unknown language. Set it to English.
set req.http.Accept-Language = "en";
}
}
###
### Varnish v4: vcl_recv must now return hash instead of lookup
return(hash);
}
sub vcl_backend_response {
set beresp.ttl = 1w;
set beresp.grace = 1h;
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client. You can do accounting or modifying the final object here.
# main variable = resp.
set resp.http.Server = "mine";
set resp.http.X-Powered-By = "madlab brazil";
}
sub vcl_pipe {
# https://www.varnish-software.com/blog/using-pipe-varnish
# 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,
# then make sure to use this: set req.http.connection = "close";
# (This code is not necessary if you do not do any request rewriting.)
set req.http.connection = "close";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment