---
title: CDN
description: >-
  Learn how to improve performance of your Spree application by using a CDN (Content Delivery Network).
---

To improve performance of your Spree application, we recommend using a CDN (Content Delivery Network) to cache images and static files.

We recommend using [Cloudflare](https://www.cloudflare.com/) as your CDN provider. It's free and simple to set up.

After setting up your Cloudflare account and adding your domain name to Cloudflare, you will need to set your cache settings.

Go to your domain name in Cloudflare and click on "Caching" tab and select "Cache Rules".

![Cloudflare Cache Rules](/images/developer/deployment/cloudflare_caching.png)

Click on "Create Rule" and set up the rule as below:

![Cloudflare Cache Rule Settings](/images/developer/deployment/cloudflare_cache_rule_1.png)

Supply the first value with

```url
/rails/active_storage/representations/
```

and the second value with

```url
/assets
```

This will handle caching of both images and static files such as JavaScript and CSS files.

Now scroll down and set the settings as below:

![Cloudflare Cache Rule Settings](/images/developer/deployment/cloudflare_cache_rule_2.png)

Finally, click on "Deploy" and you are done!

## Dedicated CDN Host

The setup above caches on your primary domain and needs no extra configuration. If you instead serve assets and images from a separate CDN hostname (e.g. a CloudFront or Fastly distribution at `cdn.example.com` with your app as the origin), set the `CDN_HOST` environment variable:

```bash
CDN_HOST=cdn.example.com
```

Static asset URLs and image/attachment URLs (including those in API responses) will then use this host instead of [`RAILS_HOST`](environment_variables.md#urls-and-hosts). Host only, no protocol — URLs keep the scheme of your primary host.
