The Varnish cache system

Your Simple Hosting instance (or VPS with a configured Web Accelerator) benefits from a powerful cache system that works with Varnish. This allows you to distribute the content of your website to a larger number of visitors without using the resources of your instance or VPS.

For example, a website visitor requests a file from your web server 'foo.jpg'. This file is then placed into the Varnish cache so that when the next visitor requests the same image, the image is served from the Varnish cache, relieving your web server from needing reply to the request.

Modify or deactivate the cache period

The default period an item will remain in the cache is 120 seconds. If you'd like to change this, you can do so by adding the following header and varying the value of max-age (in seconds):

HTTP Cache-Control: public, max-age=200

For development or testing purposes, you can reduce the cache to 1 second, with max-age=1. With max-age=0, the cache will be disabled, but be aware that the performance of your instance will be reduced without the cache system in place, and is not recommended. 

For example, in php:

header("Cache-Control: max-age=200");

Or in an .htaccess file if it is a static website:

Header add Cache-Control "max-age=200"

Advanced VPS customers: The Web Accelerator takes care of s-maxage

Purging cache using HTTP 'PURGE'/'PURGEALL' request

It is possible to purge a particular URL cache by sending an HTTP PURGE request. You can also purge the entire cache with an HTTP PURGEALL request.

The PURGE and PURGEALL requests will only work from within your virtual machine. For the moment, they cannot be issued remotely.

Here are a some examples:

curl -X PURGE

Alternatively, here is a PHP script that can be called from a browser to clear a particular URL cache:

/* purge.php
 * Purge a URL on this host
header("Cache-Control: max-age=1"); // don't cache ourself
ini_set("display_errors", 1);
// Set to true to hide varnish result
define("SILENT", false);
$path = isset($_GET["path"]) ? $_GET["path"] : "";
$purge_url = "http://" . $_SERVER["HTTP_HOST"] . "/$path";
if ( $ch = curl_init($purge_url) ) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PURGE");
    curl_setopt($ch, CURLOPT_NOBODY, SILENT);

For WordPress, we recommend using the Varnish HTTP Purge plugin.

Purging cache from web interface

It is possible to purge the cache directly from your instance's administration page.

To do this, once you are on your instance's control panel, scroll down to the “Access” table, and where it says “Administration of your instance”, click on “Login”.

You will then be prompted for your user name (this is your instance's ID) and password (defined when you created the instance):

Once logged in, find the “Varnish” section, and click on the link to purge all objects in cache.

If you see “200 = OK” then this means that the purge was successful.

The PURGEALL command can only be performed every 120 seconds. This limitation is to prevent abuse of the cache CPU.

Purging cache when deploying a git branch

For Simple Hosting instances that are utilizing the built-in git repository, executing the deploy command will automatically perform a PURGEALL at the end of the deployment process.


Your instance includes the enabled mod_expires module in Apache2, which allows you to set custom expiration dates for cached items based on either the time the source file was last modified, or to the time of the client access. 

You can use the directive “ExpiresDefault”” to set an expiration time for all cached items, or you can set the expiration time for specific file-types using “ExpiresByType”.

For more information please see the mod_expires Apache2 documentation

The following cookies are stripped by Varnish:


# sanitize cookies   
sub vcl_recv {
	if (req.http.Cookie) {
	# Remove unwanted cookies set by javascript for external services
	# try to track origin                      
	# __utm* _ga _gat: Google Analytics, __gads: Google Adsense                  
	# __qca __qcb: Quantcast                                                             
	# _jsuid: GetClicky                                                                  
	# __cfduid: CloudFare                                                                
	# __unam __switchTo5x: ShareThis                                                     
	# __atuvc: AddThis  (_atshc also?)                                                   
	set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)_(ga(|t)|jsuid|_(utm[a-z]+|gads|qc(a|b)|cfduid|unam|switchTo5x|atuvc))=[^;]*", "");

	# Cleanup
		set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
		if (req.http.Cookie == "") {
			unset req.http.Cookie;
	# Track filtered cookie to analyse, can be removed                                                      
	#std.log("cookie:" + req.http.Cookie);                                                                             

Additional Reading

最后更改: 2016/06/08 08:46 (外部编辑)