All Projects → emgag → varnish-cache-reaper

emgag / varnish-cache-reaper

Licence: MIT license
Simple python/twisted HTTP daemon forwarding PURGE and BAN requests to multiple varnish (or other proxy) instances

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to varnish-cache-reaper

pacman.store
Pacman Mirror via IPFS for ArchLinux, Endeavouros and Manjaro
Stars: ✭ 65 (+441.67%)
Mutual labels:  cache, cluster
React Esi
React ESI: Blazing-fast Server-Side Rendering for React and Next.js
Stars: ✭ 537 (+4375%)
Mutual labels:  cache, varnish
Koa Redis
Redis storage for Koa session middleware/cache with Sentinel and Cluster support
Stars: ✭ 324 (+2600%)
Mutual labels:  cache, cluster
Laravel Responsecache
Speed up a Laravel app by caching the entire response
Stars: ✭ 1,874 (+15516.67%)
Mutual labels:  cache, varnish
Nfx
C# Server UNISTACK framework [MOVED]
Stars: ✭ 379 (+3058.33%)
Mutual labels:  cache, cluster
souin
An HTTP cache system, RFC compliant, compatible with @TykTechnologies, @traefik, @caddyserver, @go-chi, @bnkamalesh, @beego, @devfeel, @labstack, @gofiber, @go-goyave, @gin-gonic, @zalando, @zeromicro, @nginx and @apache
Stars: ✭ 269 (+2141.67%)
Mutual labels:  cache, varnish
netlify-plugin-cache
⚡ Generic plugin for caching any files and/or folders between Netlify builds
Stars: ✭ 19 (+58.33%)
Mutual labels:  cache
apollo-magic-refetch
magically refetches relevant apollo graphql queries after creates, deletes, and association changes
Stars: ✭ 32 (+166.67%)
Mutual labels:  cache-invalidation
WP-Stash
Bridge between WordPress and StashPHP, providing a PSR6-compliant caching system for WordPress
Stars: ✭ 31 (+158.33%)
Mutual labels:  cache
NodeServer
Compare node.js servers
Stars: ✭ 35 (+191.67%)
Mutual labels:  cluster
tacky
Primitive Object Memoization for Ruby
Stars: ✭ 14 (+16.67%)
Mutual labels:  cache
deploykit
A toolkit for creating and managing declarative, self-healing infrastructure.
Stars: ✭ 2,246 (+18616.67%)
Mutual labels:  cluster
kube-alive
Some tools to experiment with Kubernetes to observe it's real-life behavior
Stars: ✭ 32 (+166.67%)
Mutual labels:  cluster
OL3-AnimatedCluster
OL3-AnimatedCluster is now part of the ol-ext project
Stars: ✭ 65 (+441.67%)
Mutual labels:  cluster
memoize
Caching library for asynchronous Python applications.
Stars: ✭ 53 (+341.67%)
Mutual labels:  cache
go-memoize
An easy, no-frills memoizer for Go. Cache your expensive function calls.
Stars: ✭ 63 (+425%)
Mutual labels:  cache
bazel-cache
Minimal cloud oriented Bazel gRPC cache
Stars: ✭ 33 (+175%)
Mutual labels:  cache
nginx-cache
Node.js module to find files in an Nginx cache based on partial URL keys
Stars: ✭ 24 (+100%)
Mutual labels:  cache
redis cluster
a openresty nginx lua redis cluster
Stars: ✭ 26 (+116.67%)
Mutual labels:  cluster
tiny-cache
Cache WordPress post content, template part, translations and nav menu output in persistent object cache
Stars: ✭ 26 (+116.67%)
Mutual labels:  cache

varnish-cache-reaper

UNMAINTAINED (2018/06/28): While this tool is stable and has been used in production for 4 years, this is no longer maintained and is superseded by varnish-towncrier.

--

Simple python/twisted HTTP daemon forwarding PURGE and BAN requests to multiple varnish (or other proxy) instances.

The daemon forwards all HTTP PURGE and BAN requests using the original Host-header and URL to all configured endpoints. It also supports surrogate keys (cache tags) using the xkey module, it will forward XKey and XKey-Purge headers if present.

This script is designed to run in a supervised environment like supervisord, daemontools or runit. For runit example code see runit-run and runit-log-run.

Usage

usage: varnish-cache-reaper.py [-h] [-v] [-l IP] [-p PORT]
                               targets [targets ...]

Varnish cache reaper

positional arguments:
  targets               Endpoint(s) to send PURGE/BAN requests to

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -l IP, --listen-ip IP
                        IP to listen on, default *
  -p PORT, --listen-port PORT
                        TCP port to listen on, default 8042

VCL example

Varnish 4.x / 5.x

Varnish documentation on purging and banning in varnish 4, or in varnish 5

[...]
# use xkey module
import xkey;

# purgers acl
# - who is allowed to issue PURGE and BAN requests
# 
acl purgers {
	"localhost";
}

sub vcl_recv {
	[...]
    if (req.method == "PURGE") {
        if (!client.ip ~ purgers) {
            return(synth(405,"Method not allowed"));
        }
        
        if(req.http.xkey) {
            set req.http.n-gone = xkey.softpurge(req.http.xkey);
            return (synth(200, "Got " + req.http.xkey + ", invalidated " + req.http.n-gone + " objects"));
        }
        
        return (purge);
    }

    if (req.method == "BAN" && client.ip ~ purgers ) {
        # remove leading / to not confuse regular expression
        ban(
            "obj.http.x-host == " + req.http.host + " && " +
            "obj.http.x-url ~ " + regsub(req.url, "^/", "")
        );

        return(synth(200, "Banned"));
    } else {
        return(synth(405,"Method not allowed"));
    }

	[...]

	return(hash);
}

sub vcl_backend_response {
    [...]

	# be friendly to ban lurker
	set beresp.http.x-url = bereq.url;
	set beresp.http.x-host = bereq.http.host;
	
	[...]
}

sub vcl_deliver {
    [...]
	
	# remove some variables we used before
	unset resp.http.x-url;
	unset resp.http.x-host;
	unset resp.http.xkey;
	
    [...]
}

Varnish 3

Varnish documentation on purging and banning in varnish 3.

# purgers acl
# - who is allowed to issue PURGE and BAN requests
# 
acl purgers {
	"localhost";
}

sub vcl_recv {
	[...]
	if (req.request == "PURGE" && !client.ip ~ purgers) {	
		error 405 "Method not allowed";
	}

	if (req.request == "BAN") {
		if (client.ip ~ purgers) {
			# remove leading / to not confuse regular expression
			ban(
				"obj.http.x-host == " + req.http.host + " && " +
				"obj.http.x-url ~ " + regsub(req.url, "^/", "")
			);
			error 200 "Banned";
		} else {
			error 405 "Method not allowed";
		}
	}

	[...]

	return(lookup);
}

sub vcl_hit {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged";
        }
}

sub vcl_miss {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged";
        }
}

sub vcl_fetch {
        # be friendly to ban lurker
        set beresp.http.x-url = req.url;
        set beresp.http.x-host = req.http.host;
}

Example

The following command line will start a service listening on 127.0.0.1:8042 (default port) and will forward all incoming PURGE or BAN requests to http://127.0.0.1:8080 and http://127.0.0.1:8081.

./varnish-cache-reaper.py -l 127.0.0.1 http://127.0.0.1:8080 http://127.0.0.1:8081

To issue a PURGE request, use

curl -X PURGE -H "Host: vhost.whatever" "http://127.0.0.1:8042/foo/bar"  > /dev/null

which will send PURGE requests to all endpoints using vhost.whatever as Host: header and /foo/bar as URL.

To issue a PURGE request using a surrogate key, use

curl -X PURGE -H "Host: vhost.whatever" -H "Xkey: some-cache-tag" "http://127.0.0.1:8042/foo/bar"  > /dev/null

Dependencies

  • Python 2.7
  • Twisted, Debian/Ubuntu Package: python-twisted

License

varnish-cache-reaper is licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].