Enable a CDN for your Discourse

cdn

(Jeff Atwood) #1

So you’d like to use a CDN to speed up worldwide access to common assets on your Discourse forum?

Sign up with the CDN of your choice – we use and strongly recommend 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 origin pull CDN address for this Discourse instance
DISCOURSE_CDN_URL: //discourse-cdn.example.com

(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="http://discourse-cdn.codinghorror.com/assets/preload_store-4ea79c2f435becca86ac97a9c038f9c7.js"></script>
<script src="http://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.


Full site CDN acceleration for Discourse
How to configure CDN
Downloading remote images disabled due to disk space
How redirect non www to www in docker installation
How to load gzipped assets from cdn?
Discourse Math Plugin
Varnish config for discourse
Spoiler tag not working with CDN and CORS
Add a new site (android) 'was not found'
Azure Backup/CDN Integration
Installing discourse and connecting it to cloudflare
Is there a way to install discourse without using "discourse-setup"?
(Daniel Hollands) #11

I’m sorry guys, I’m trying to figure out how to do this, and I’m drawing a blank. I’ve tried to follow your instructions, but I think the Fastly site must have changed since you created the screenshot, because I can’t find where to enter in the details you’ve presented.

:confused: :confounded:

Minor update - this is the screen I get when I trying to use the service

It doesn’t match up with your example, so I don’t know what goes where.


(Sam Saffron) #12

Fastly is more sophisticated than the general CDN it supports accelerating both dynamic and static content.

To configure.

  1. Plug in the IP address of your Discourse web server in “Server Address”
  2. Plug in your desired domain name in “Domain name”
  3. Point your DNS CNAME for your domain at global.prod.fastly.net per: Redirecting...

That is it you should be done, no changes needed on Discourse side.


Add a new site (android) 'was not found'
(Erick Guan) #13

I wonder how to deal with CDN provider does not support https…

There is a cloud storage provider can just acted as CDN. But their spider don’t like SSL. There are APIs for upload assets. I would think the easiest way is to write a plugin to monitor public folder and upload them instantly…


(Kane York) #14

That sounds like a push CDN to me, which was supported in the past but not currently.


(Ionuț Staicu) #19

This should also work with other CDNs? Like S3?


(Jeff Atwood) #20

S3 is not a CDN, but it should work with Amazon CloudFront and any other Pull compatible CDN.


(Tarak'ha (Sara)) #22

If you want an inexpensive CDN, I think MaxCDN might be what you’re looking for. It is a pull-compatible CDN.

However, going to Fastly’s website from a quick google search led me to Fastly’s pricing page in a snap.

MaxCDN goes by a somewhat different pay model (as far as I can see in comparing with Fastly) which is easier to budget with.


(Nick) #35

I’m new to Discourse and I’ve been trying to configure it with Cloudflare. This tutorial focuses on fastly. I have used Cloudflare on my Wordpress sites in the past and it was quite simple. Typically, Cloudflare scans your website and all you have to do is change your DNS to what Cloudflare provides. My question is… do the same steps I just mentioned apply to discourse? If yes… what so I do with the CDN URL in app.yml? Thanks guys.


(Jeff Atwood) #36

You do not have to spend time designating which files to cache. CloudFlare’s technology builds the cache for you based on your traffic and what objects on your site are safe to serve from the edge.

Just be sure you do not enable any JavaScript weirdness / modifications in CloudFlare, as it will break the crap out of Discourse if you do.


Spoiler tag not working with CDN and CORS
(Jesse Perry) #37

Anyone have any experience with Cachefly?

They’re running a pay-as-you-go plan with no minimum spend, which seems to be the only one of their peers, since Fastly has a minimum $50/mo spend requirement. CacheFly | 404 Error

I’ve heard their name often before and I think they’re up there in the top tier CDN? Just checking others’ experience first…


(Sam Saffron) #38

Cachefly are good, have never used them but heard plenty good things


(Cody Arsenault) #39

Does anyone have any experience in using KeyCDN with Discourse?

They support HTTP/2, have 22 PoPs, and are amongst the cheapest CDN on the market (starting $0.04/GB). If all works well with Discourse it would definitely be something worth looking into.


(Tien Nguyen) #40

Yes, I using KeyCDN on my discourse: http://reactvn.org/. It’s working perfectly.

These are the steps to use KEYCDN:

  1. Register [KEYCDN] 1 account: Fill in your e-mail can register, you can using without having to recharge.

  2. Create a pull zone: Create a Pull Zone - KeyCDN Support

  3. Create a Zonealias: Create a Zonealias - KeyCDN Support

  4. Config Discourse: Modify app.yml, DISCOURSE_CDN_URL point to your CDN URL or an alias. Enable a CDN for your Discourse


(@SenpaiMass) #41

Any one knows how to configure with http://www.coralcdn.org/ ?


(Cody Arsenault) #43

Is there any way to change the CDN url without having to rebuild the Docker container or is updating the app.yml and rebuilding currently the only way?


(Jeff Atwood) #44

No, that is the only way.


#47

Why not use CloudFlare? They don’t charge for bandwidth at all. (Plus a bunch of other features, like free SSL).


#48

You can use Cloudflare along with another CDN. You don’t get full CDN advantages with Cloudflare unless you pay, and for serving up lots of images, attachments, or other media you’ll want a dedicated CDN to reduce the server load.


(徐徐浩) #58

after enable CDN, i can not visit my site any more.

I have this error in my log:

nginx: [emerg] host not found in upstream "avatars.discourse.org" in /etc/nginx/conf.d/discourse.conf:247