Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
test 123
vcl 4.0;
import std;
backend default {
.host = "176.62.169.146" ;
.port = "80";
}
acl purge {
"127.0.0.1";
"localhost";
"176.62.169.146";
}
# Handle the HTTP request received by the client
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]+", "");
# Include purge support for servers based on the ACL
if (req.method == "PURGE" || req.method == "PURGEALL" || req.method == "BAN" || req.method == "BANALL") {
if (!client.ip ~ purge) {
return (synth(405, "This IP is not allowed to send PURGE or BAN requests."));
}
if (req.method == "PURGE" || req.method == "BAN") {
ban("obj.http.x-url == " + req.url + " && obj.http.x-host == " + req.http.host);
return (synth(200, "Banned"));
}
if (req.method == "PURGEALL" || req.method == "BANALL") {
ban("obj.http.x-url ~ " + req.url + " && obj.http.x-host == " + req.http.host);
return (synth(200, "Banned"));
}
}
if (req.method == "REFRESH") {
set req.method = "GET";
set req.hash_always_miss = true;
}
# Only deal with "normal" types
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.method != "GET" && req.method != "HEAD") {
# We only deal with GET and HEAD by default
return (pass);
}
# Some generic URL manipulation, useful for all templates that follow
# First remove the Google Analytics added parameters, useless for our backend
if (req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=") {
set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "");
set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?");
set req.url = regsub(req.url, "\?&", "?");
set req.url = regsub(req.url, "\?$", "");
}
# Strip hash, server doesn't need it.
if (req.url ~ "\#") {
set req.url = regsub(req.url, "\#.*$", "");
}
# Strip a trailing ? if it exists
if (req.url ~ "\?$") {
set req.url = regsub(req.url, "\?$", "");
}
# Remove all cookies for static files
if (req.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|mp[34]|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|woff|xml|zip)(\?.*)?$") {
unset req.http.Cookie;
return (hash);
}
# Normalize Accept-Encoding header
# straight from the manual: https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(bmp|bz2|eot|flv|gif|gz|ico|jpeg|jpg|mp[34]|png|rar|swf|tar|tgz|wav|woff|zip)$") {
# No point in compressing these
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 {
# unkown algorithm
unset req.http.Accept-Encoding;
}
}
# Send Surrogate-Capability headers to announce ESI support to backend
set req.http.Surrogate-Capability = "key=ESI/1.0";
if (req.http.Cookie) {
set req.http.Cookie = ";" + req.http.Cookie;
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
set req.http.Cookie = regsuball(req.http.Cookie, ";(username)=", "; \1=");
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
if (req.http.cookie ~ "^\s*$") {
unset req.http.cookie;
}
}
if (req.http.Authorization) {
# Not cacheable by default
return (pass);
}
return (hash);
}
# The data on which the hashing will take place
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
# If the client supports compression, keep that in a different cache
if (req.http.Accept-Encoding) {
hash_data(req.http.Accept-Encoding);
}
#Avoid HTTPS redirect loops
if (req.http.X-Forwarded-Proto) {
hash_data(req.http.X-Forwarded-Proto);
}
if (req.http.Cookie ~ "username") {
hash_data(regsub( req.http.Cookie, "^.*username=([^;]*);*.*$", "\1" ));
}
return (lookup);
}
# Handle the HTTP request coming from our backend
sub vcl_backend_response {
# Set up X headers for ban lurker friendly bans
set beresp.http.x-url = bereq.url;
set beresp.http.x-host = bereq.http.host;
# Parse ESI request and remove Surrogate-Control header
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
unset beresp.http.Surrogate-Control;
set beresp.do_esi = true;
}
# If the request to the backend returns a code is 5xx, restart the loop
# If the number of restarts reaches the value of the parameter max_restarts,
# the request will be error'ed. max_restarts defaults to 4. This prevents
# an eternal loop in the event that, e.g., the object does not exist at all.
if (beresp.status >= 500 && beresp.status <= 599){
return (retry);
}
# Enable cache for all static files
if (bereq.url ~ "^[^?]*\.(bmp|bz2|css|doc|eot|flv|gif|gz|ico|jpeg|jpg|js|less|mp[34]|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|woff|xml|zip)(\?.*)?$") {
unset beresp.http.set-cookie;
}
# Set 2min cache if unset for static files
if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
set beresp.ttl = 120s;
# set beresp.ttl = 120s;
set beresp.uncacheable = true;
return (deliver);
}
return (deliver);
}
# The routine when we deliver the HTTP request to the user
# Last chance to modify headers that are sent to the client
sub vcl_deliver {
# Remove some headers
unset resp.http.X-Powered-By;
unset resp.http.Server;
unset resp.http.X-Drupal-Cache;
unset resp.http.X-Varnish;
unset resp.http.Via;
unset resp.http.Link;
unset resp.http.x-url;
unset resp.http.x-host;
return (deliver);
}
sub vcl_backend_error {
if (beresp.status >= 500 && beresp.status <= 599 && bereq.retries < 4) {
# 4 retry for 5xx error
return (retry);
} else {
set beresp.http.Content-Type = "text/html; charset=utf-8";
set beresp.http.Retry-After = "5";
synthetic({"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>"} + beresp.status + " " + beresp.reason + {"</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Backend Error page">
<meta name="author" content="Pascal A.">
<meta name="generator" content="vim">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<!-- Le styles -->
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.0/css/bootstrap-combined.min.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="page-header">
<h1 class="pagination-centered">Error "} + beresp.status + " " + beresp.reason + {"</h1>
</div>
<div class="alert alert-error pagination-centered">
<i class="icon-warning-sign"></i>
We're very sorry, but the page could not be loaded properly.
<i class="icon-warning-sign"></i>
</div>
<blockquote>This should be fixed very soon, and we apologize for any inconvenience.</blockquote>
<div class="accordion-heading pagination-centered">
<button class="btn accordion-toggle" data-toggle="collapse" href="#debug">
Show debug
</button>
</div>
<div id="debug" class="accordion-body collapse">
<div class="accordion-inner">
<table class="table table-striped table-bordered table-condensed"><caption><h2 class="pagination-centered">Debug Information</h2></caption>
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
<tr>
<td colspan="2">General</td>
</tr>
<tr>
<td width="20%">XID</td>
<td>"} + bereq.xid + {"</td>
</tr>
<tr>
<td>Time</td>
<td>"} + now + {"</td>
</tr>
<tr>
<td colspan="2">Request</td>
</tr>
<tr>
<td>HTTP host</td>
<td>"} + bereq.http.Host + {"</td>
</tr>
<tr>
<td>Request type</td>
<td>"} + bereq.method + {"</td>
</tr>
<tr>
<td>HTTP Protocol version</td>
<td>"} + bereq.proto + {"</td>
</tr>
<tr>
<td>URL</td>
<td>"} + bereq.url + {"</td>
</tr>
<tr>
<td>Cookies</td>
<td>"} + regsuball(bereq.http.cookie, "; ", "<br />") + {"</td>
</tr>
<tr>
<td>Accept-Encoding</td>
<td>"} + bereq.http.Accept-Encoding + {"</td>
</tr>
<tr>
<td>Cache-Control</td>
<td>"} + bereq.http.Cache-Control + {"</td>
</tr>
<tr>
<td>HTTP header</td>
<td>"} + bereq.http.header + {"</td>
</tr>
<tr>
<td>Backend</td>
<td>"} + bereq.backend + {"</td>
</tr>
<tr>
<td colspan="2">Server</td>
</tr>
<tr>
<td>Identity</td>
<td>"} + server.identity + {"</td>
</tr>
</table>
</div>
</div>
</div>
<footer class="container pagination-centered">
</footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.0/js/bootstrap.min.js"></script>
</body>
</html>
"});
}
return (deliver);
}
sub vcl_synth {
if (resp.status >= 500 && resp.status <= 599 && req.restarts < 4) {
# 4 retry for 5xx error
return(restart);
} else {
set resp.http.Content-Type = "text/html; charset=utf-8";
set resp.http.Retry-After = "5";
synthetic({"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>"} + resp.status + " " + resp.reason + {"</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Backend Error page">
<meta name="author" content="Pascal A.">
<meta name="generator" content="vim">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<!-- Le styles -->
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.0/css/bootstrap-combined.min.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="page-header">
<h1 class="pagination-centered">Error "} + resp.status + " " + resp.reason + {"</h1>
</div>
<div class="alert alert-error pagination-centered">
<i class="icon-warning-sign"></i>
We're very sorry, but the page could not be loaded properly.
<i class="icon-warning-sign"></i>
</div>
<blockquote>This should be fixed very soon, and we apologize for any inconvenience.</blockquote>
<div class="accordion-heading pagination-centered">
<button class="btn accordion-toggle" data-toggle="collapse" href="#debug">
Show debug
</button>
</div>
<div id="debug" class="accordion-body collapse">
<div class="accordion-inner">
<table class="table table-striped table-bordered table-condensed"><caption><h2 class="pagination-centered">Debug Information</h2></caption>
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
<tr>
<td colspan="2">General</td>
</tr>
<tr>
<td width="20%">XID</td>
<td>"} + req.xid + {"</td>
</tr>
<tr>
<td>Time</td>
<td>"} + now + {"</td>
</tr>
<tr>
<td colspan="2">Request</td>
</tr>
<tr>
<td>HTTP host</td>
<td>"} + req.http.Host + {"</td>
</tr>
<tr>
<td>Request type</td>
<td>"} + req.method + {"</td>
</tr>
<tr>
<td>HTTP Protocol version</td>
<td>"} + req.proto + {"</td>
</tr>
<tr>
<td>URL</td>
<td>"} + req.url + {"</td>
</tr>
<tr>
<td>Cookies</td>
<td>"} + regsuball(req.http.cookie, "; ", "<br />") + {"</td>
</tr>
<tr>
<td>Accept-Encoding</td>
<td>"} + req.http.Accept-Encoding + {"</td>
</tr>
<tr>
<td>Cache-Control</td>
<td>"} + req.http.Cache-Control + {"</td>
</tr>
<tr>
<td>HTTP header</td>
<td>"} + req.http.header + {"</td>
</tr>
<tr>
<td>GZIP supported</td>
<td>"} + req.can_gzip + {"</td>
</tr>
<tr>
<td>Backend</td>
<td>"} + req.backend_hint + {"</td>
</tr>
<tr>
<td colspan="2">Server</td>
</tr>
<tr>
<td>Identity</td>
<td>"} + server.identity + {"</td>
</tr>
</table>
</div>
</div>
</div>
<footer class="container pagination-centered">
</footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.0/js/bootstrap.min.js"></script>
</body>
</html>
"});
}
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.