How to Use Varnish to Speed up my Website
Because Symfony’s cache uses the standard HTTP cache headers, the Symfony Reverse Proxy can be replaced with any other reverse proxy. Varnish is a powerful, open-source, HTTP accelerator capable of serving cached content fast and including support for Edge Side Includes.
Make Symfony Trust the Reverse Proxy
Varnish automatically forwards the IP as X-Forwarded-For and leaves the
X-Forwarded-Proto header in the request. If you do not configure Varnish as
trusted proxy, Symfony will see all requests as coming through insecure HTTP
connections from the Varnish host instead of the real client.
Remember to call the Request::setTrustedProxies() method in your front controller so that Varnish is seen as a trusted proxy and the X-Forwarded-* headers are used.
Routing and X-FORWARDED Headers
To ensure that the Symfony Router generates URLs correctly with Varnish,
an X-Forwarded-Port header must be present for Symfony to use the
correct port number.
This port number corresponds to the port your setup is using to receive external
connections (80 is the default value for HTTP connections). If the application
also accepts HTTPS connections, there could be another proxy (as Varnish does
not do HTTPS itself) on the default HTTPS port 443 that handles the SSL termination
and forwards the requests as HTTP requests to Varnish with an X-Forwarded-Proto
header. In this case, you need to add the following configuration snippet:
sub vcl_recv {
if (req.http.X-Forwarded-Proto == "https" ) {
set req.http.X-Forwarded-Port = "443";
} else {
set req.http.X-Forwarded-Port = "80";
}
}
Ensure Consistent Caching Behavior
Varnish uses the cache headers sent by your application to determine how
to cache content. However, versions prior to Varnish 4 did not respect
Cache-Control: no-cache, no-store and private. To ensure
consistent behavior, use the following configuration if you are still
using Varnish 3:
Tip
You can see the default behavior of Varnish in the form of a VCL file: default.vcl for Varnish 3, builtin.vcl for Varnish 4.
Enable Edge Side Includes (ESI)
As explained in the Edge Side Includes article, Symfony
detects whether it talks to a reverse proxy that understands ESI or not. When
you use the Symfony reverse proxy, you don’t need to do anything. But to make
Varnish instead of Symfony resolve the ESI tags, you need some configuration
in Varnish. Symfony uses the Surrogate-Capability header from the Edge Architecture
described by Akamai.
Note
Varnish only supports the src attribute for ESI tags (onerror and
alt attributes are ignored).
First, configure Varnish so that it advertises its ESI support by adding a
Surrogate-Capability header to requests forwarded to the backend
application:
sub vcl_recv {
// Add a Surrogate-Capability header to announce ESI support.
set req.http.Surrogate-Capability = "abc=ESI/1.0";
}
Note
The abc part of the header isn’t important unless you have multiple
“surrogates” that need to advertise their capabilities. See
Surrogate-Capability Header for details.
Then, optimize Varnish so that it only parses the response contents when there
is at least one ESI tag by checking the Surrogate-Control header that
Symfony adds automatically:
Tip
If you followed the advice about ensuring a consistent caching behavior, those VCL functions already exist. Append the code to the end of the function, they won’t interfere with each other.
Cache Invalidation
If you want to cache content that changes frequently and still serve the most recent version to users, you need to invalidate that content. While cache invalidation allows you to purge content from your proxy before it has expired, it adds complexity to your caching setup.
Tip
The open source FOSHttpCacheBundle takes the pain out of cache invalidation by helping you to organize your caching and invalidation setup.
The documentation of the FOSHttpCacheBundle explains how to configure Varnish and other reverse proxies for cache invalidation.