Enable a CDN for your Discourse

So you’d like to use a CDN to speed up worldwide access to common assets on your Discourse forum? You might want to go all-in and Set up file and image uploads to S3 (see also Configure an S3 compatible object storage provider for uploads).

If you want a CDN without the added complexity of S3 and having two CDNs, this is the guide for you. The CDN will reduce some load on your server by letting the CDN serve those assets so that the server doesn’t have to. It also can speed things up by delivering the files from a location closer to the end user.

Sign up with the CDN of your choice – CDCK/discourse.org has used and strongly recommends Fastly.

The configuration will look something like this:

What you’re looking for is three main settings:

  1. The origin address, which is the URL where your forum is currently located discourse.example.com. This is where the CDN will draw all its original content from on first request.

  2. The CNAME, which is the “friendly” name for your CDN that you’ll set up in your DNS, e.g. discourse-cdn.example.com

  3. The CDN URL, which is the “unfriendly” name for where the cached CDN assets will be coming from via the CDN’s worldwide network of distributed servers. It will look like 637763234.cdn-provider.com

You’ll need to edit your DNS to map the CNAME to the CDN URL, like so:

discourse-cdn.example.com IN CNAME 637763234.cdn-provider.com

(Once you’ve edited the DNS, give it a little bit of time to propagate.)

The actual Discourse part of the setup is fairly simple. Uncomment the CDN line in your app.yml and update it with the CNAME you just set up in your DNS:

## The http or https CDN address for this Discourse instance (configured to pull)
DISCOURSE_CDN_URL: https://discourse-cdn.example.com

:warning: The CDN URL must include a protocol (https:// or http://). Protocol-relative URLs (starting with //) are no longer supported and will cause the container build to abort.

(If you do not see this line in your app.yml, add it below the other DISCOURSE_ variables)

As with any other changes to your app.yml, you need to rebuild the container to reflect changes:

./launcher rebuild app

Once you’ve rebuilt, browse to your Discourse instance in the browser. View source and search for “cdn”. You’ll see that websites assets are now coming from your CDN:

<script src="https://discourse-cdn.codinghorror.com/assets/preload_store-4ea79c2f435becca86ac97a9c038f9c7.js"></script>
<script src="https://discourse-cdn.codinghorror.com/assets/locales/en-7084a68855205a9128245d2d0ce94ed9.js"></script>

This topic covers the more common scenario of static asset acceleration. See this topic for full site (both dyamic and static asset) CDN acceleration (like provided by Cloudflare). Cloudflare works differently from a traditional CDN, so this isn’t a good topic for discussing Cloudflare.

Other CDNs reported to work include:

Last edited by @pfaffman 2023-04-10T18:34:11Z

Check documentPerform check on document:
43 Likes
Full site CDN acceleration for Discourse
Azure Backup/CDN Integration
Downloading remote images disabled due to disk space
How do I change the legacy CDN URLs of images in posts?
Add a new site (android) 'was not found'
CloudFront not caching static files
How to enable AWS CDN to accelerate the discourse service
Cloudfront cdn with discourse?
Discourse load balancer encounters problems, hoping to help
How to solve super slow loading time for site?
S3 image bandwidth costs are getting annoying
Varnish config for discourse
How to add AWS Cloudfront as a Discourse CDN
I can't login to admin after backup due to Cloudflare
Forum not working after configuring CDN (ERR_CERT_COMMON_NAME_INVALID)
Change image folder to symlinked folder
Minimum server size based on backup?
How to add AWS Cloudfront as a Discourse CDN
Blank login and signup page
Understanding Uploads, Images, and Attachments
My discourse speed is very slow
MKJ's Opinionated Discourse Deployment Configuration
Uploads Subdomain For Cloudflare
How to install discourse without ssl?
How might we better structure #howto?
Conserving unicorns by serving images with nginx?
Discourse & Cloudflare
Mobile app URL issues
Forum does not work
How to make Discourse faster for image-heavy communities?
Rebuilds taking a long time with a large number of uploads (chown performance)
Latest commits in v3.3.0.beta3-dev seems to break Discourse
Using Discourse with Cloudflare: Best Practices
Optimizing Site For Speed - How To Improve?
Should I load third-party libraries from vendor or cdn?