Using a Varnish CDN Stack with KeyCDN

varnish cdn

What Is Varnish Cache?

Varnish Cache is an open source, front end accelerator also known as a caching HTTP reverse proxy. Varnish can be used to cache both dynamic and static content and has shown to dramatically increase website speed and server performance.

Adding Varnish to your stack is easy in that it can be done without any code changes. Additionally, when you start tweaking the settings in Varnish and experimenting with the advanced features, caching capabilities and overall performance can increase even more.

Why Use a Reverse Proxy?

As mentioned, using a reverse proxy such a Varnish can drastically increase website speed. This is due to a few factors:

  • Objects can be served more quickly from the cache server than the origin server as the cache server’s workload is less intensive and less mixed.
  • Off-loading assets that infrequently change such as CSS and JavaScript files reduce the load on the origin server, allowing it to perform more “useful” requests. This allows for the origin server to render pages quickly as it is not serving static content on every re-load.
  • In the case that Varnish is used as a dedicated server, the origin server receives fewer inbound connections since inbound connections would now see the caching proxy as the first server. The inbound connections that are received by the origin server are therefore more responsive, spending less time in context thread switching.
  • Using Varnish cache on the origin server is a great technique to reducing the Time To First Byte (TTFB). Varnish helps reduce the processing time to the backend database enabling the TTFB to be achieved faster.

How a Varnish CDN Stack Works with KeyCDN

Using a Varnish CDN stack increases website deliverability speed and helps further reduce origin server load. Varnish cache works with KeyCDN very similarly to how your origin web server works with KeyCDN. If the origin server serves assets from Varnish cache to KeyCDN, KeyCDN will treat Varnish just like any other origin and cache those assets. If the origin serves assets from the web server, then KeyCDN will cache those assets as expected.

It makes no difference where the assets are coming from (either Varnish or the origin server) KeyCDN treats both sources equally and will cache the objects either way.

The content-length HTTP header is enabled by default, however, if you have it disabled you will need to re-enable it in the header on your origin server. This is mandatory for a cache HIT, otherwise you will get a cache MISS.

Checking If the Content Was Served from Varnish

There are two ways to check if the content that it being served to your CDN is delivered from Varnish.

  1. Check the X-Varnish HTTP header. For a cache HIT, X-Varnish will have two IDs – 1) the ID of the current request and 2) the ID that populated the cache.
  2. Check the Via header for something similar to 1.1 Varnish. This tells you that the content was served from Varnish.


Using Varnish cache can greatly help reduce the load on your origin server and help deliver content faster to your visitors. Using Varnish cache in conjunction with a CDN further increases the speed of your website and is a great addition to sites that are delivering content to users across the globe.


  1. Matt

    I’m trying to use KeyCDN on my site which is using a reverse NGINX proxy and it just doens’t work. Do you have any advise?

Leave a Reply to Rafael Click here to cancel reply.