Varnish as HTTP Cache

Varnish as HTTP Cache

Overview

Varnish is an HTTP proxy-cache that sits as a reverse proxy between your application and the client. It caches responses according to rules you define, reducing load on your application. Clever Cloud provides Varnish 8.0 and varnish-modules 0.27.

Supported runtimes

Varnish is available on all runtimes that support Request Flow.

Enable Varnish for your application

Create a varnish.vcl file in the clevercloud/ folder at the root of your application. You can also set the CC_VARNISH_FILE environment variable to a custom path within your application root, written as an absolute path starting at / (for example CC_VARNISH_FILE=/config/varnish.vcl). If the file does not exist, deployment fails.

This file describes how Varnish caches your application’s responses and when it returns a cached resource. To learn how to write your varnish.vcl file, refer to the Varnish documentation.

The vcl 4.1; declaration and backend section are not necessary as they are already handled by Clever Cloud. If your varnish.vcl file is stored on an FS Bucket, redeploy the application for changes to take effect.

Listen on the right port

Varnish is managed through Request Flow. Once Varnish is enabled, your application must listen on port 9000 instead of 8080. Request Flow places Varnish (and any other configured middleware) between the public port (8080) and your application. In runtimes where Clever Cloud manages the port configuration (FrankenPHP, Java, PHP, Static), this is handled transparently.

Configure the cache size

Set the CC_VARNISH_STORAGE_SIZE environment variable to configure the Varnish cache size (default: 1G).

CC_VARNISH_STORAGE_SIZE=2G

Varnish migration

If you have a configuration for an older version of Varnish, read:

Example files

Clever Cloud provides example Varnish configuration files. Download the one that fits your needs, rename it to varnish.vcl and place it in the clevercloud/ folder at the root of your application.

Varnish with a monorepo

If you use a monorepo, you may want to use Varnish for only some of its applications. Use CC_VARNISH_FILE to point to a specific configuration file. A clevercloud/varnish.vcl file at the root of your monorepo activates Varnish for all applications. To limit Varnish to specific applications, place the file elsewhere and create a symlink during deployment only for the applications that need it:

CC_PRE_BUILD_HOOK="mkdir $APP_HOME/clevercloud; ln -s $APP_HOME/path/to/your/file/varnish.vcl $APP_HOME/clevercloud/varnish.vcl"

Applications without this hook or without CC_VARNISH_FILE set will not use Varnish.

Last updated on

Did this documentation help you ?