Enable a CDN for your Discourse

So files will not synchronise with my system, cloudfront just cache them. Am i true?

3 Likes

Yes. That is correct. The CDN gets from your site just the assets that are requested of it.

Discourse will push to S3 static assets (like css and javascript) as well I uploads.

4 Likes

Is it possible to use cloudflare rules to cache static assets like css and javascript?

2 Likes

Yes, we use it that way and it works well. Just be very careful to disable any javascript optimizations. You can see our setup here:

4 Likes

Thanks for the response.

I have already added the second and third rule but I didn’t get the first rule. What’s the need for 301 redirect??

2 Likes

You don’t need it; it’s in the description.

3 Likes

Thanks for the response. What about the static assets like css and javascripts? Any rule possible for css and javascript??

2 Likes

I didn’t test that. I’m not sure that would work. At the very least you’d have to clear your Cloudflare cache after every rebuild.

2 Likes

It’s okay with me to clear the cache after every rebuild. What else would possibly go wrong if I make a rule to cache everything in https://myforum.com/assets/ ? Will this anyway affect user login? If you give a nod, I will test it for a week and report. I still have one unused rule.

2 Likes

Just to clarify, is it correct we do not use this field (as well as CDN URL) if we are using cloudflare CDN?

I have setup cloudflare.template in app.yml as per docs.

Is any further config needed if using cloudflare?

1 Like

I just successfully used StackPath as CDN. Works as expected!

2 Likes

Is it still the case nowadays?

From 2019:

It seems we have to be extra careful and that we lose some of Cloudflare benefits.

From 2020:

From 2021:

So, is Cloudflare reliable and efficient with Discourse today? Does disabling the “Rocket Loader” feature minimize the benefits?

I was thinking of moving from Stackpath to Bunny CDN, but the owner of my forum domain and former admin has a cloudflare enterprise account (he’s the owner of tinyURL btw) on which he could add the domain for free (Looking for a decrease in the forum fees in the future - #6 by Gilby - Forum Feedback - Unicyclist.com).

So, I wonder if it’s right, painless and effective to use Cloudflare instead of Bunny CDN.

2 Likes

If you’re image-heavy then what you’ll spend increasing directly attached SSD will be far costlier than the equivalent S3 costs.

S3 does need a CDN in front of it to be cost effective as you’re billed on ingress and egress, but it’s still vastly cheaper than just slapping on more high speed SSD. CloudFlare can front S3, but asset caching is really the only feature you can use reliably. Putting CloudFlare between the client and the application server will also increase the number of hops and push application latency up. It’s no magic bullet.

2 Likes

We’re not image-heavy. The goal of the CDN would be so the assets load fast to every user since our community is international (mainly US and Europe).

We use S3, but only to store backups.

1 Like

Quick question. Is a rebake mandatory after we change our CDN? Or is it just if the CDN URL is different? Or is it not needed at all?


edit: or is it simply possible to replace the old URLs in the cooked posts by the new one with a rails script?

For example, if I have a post containing:

<img class="ytp-thumbnail-image"
src="https://f5i4i8k5.stackpathcdn.com/uploads/default/original/3X/f/3/f3bc5ab19cc0394f33fd008e90ca06e081886a0b.jpeg"
title="Riding a unicycle part 76: Sullivan Canyon - Los Angeles"
width="480"
height="360"
loading="lazy"
style="aspect-ratio: 480 / 360;">

Could I do a search & replace strings from f5i4i8k5.stackpathcdn.com to newcdn.unicyclist.com without breaking anything? It seems safe, but I want to be sure about what I do.

I don’t mind rebaking all my posts, but the problem I faced the first time is that I have thousands of embedded youtube videos, and my IP was blacklisted by Youtube during the rebake because of too many requests to youtube.

So, if there’s a safe alternative than rebake all the posts and face the same annoying issue, I’d prefer to go for the alternative…

3 Likes

There is a rake task that will do a replacement. I think they running that is safe and a good idea. Replace a string in all posts is what you’re looking for, I think.

2 Likes

Hi Jay,

I’m not sure about using the rake task.

The CDN URLs seem to be present in the cooked fields rather than the raw fields (unless the message was sent by email).

From what I understand, post:remap search for the string in the raw field only.

If replacing the CDN URLs in both the raw and cooked fields is safe, I guess I should go with a custom rails script?

2 Likes

You’re right. There’s another . . . thing . . . that will replace a string in all fields in the database. That’s the one you’re looking for. Sorry but I can’t find it right now. There are some topics about moving from one s3, but there doesn’t seem to be a howto.

2 Likes

Thank you for this clue!

I guess it’s this?

DbHelper.remap('from', 'to')

Found on Rebaking old posts won't pull new S3 CDN URL after S3 bucket rename - #10 by Brad_Wood

And used here:

If it’s a safe alternative to a rebake when changing our CDN service, a howto could be nice indeed.

3 Likes

Oh hooray. Thanks for letting me know. I thought it would be enough for you to find it. Glad you got it.

3 Likes