为您的 Discourse 启用 CDN

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

3 个赞

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 个赞

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

2 个赞

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 个赞

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 个赞

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

3 个赞

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

2 个赞

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 个赞

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 个赞

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 个赞

我刚刚成功使用了 StackPath 作为 CDN。效果如预期!

2 个赞

现在情况还是这样吗?

来自 2019 年:

看起来我们必须格外小心,并且会失去 Cloudflare 的一些优势。

来自 2020 年:

来自 2021 年:

那么,Cloudflare 今天与 Discourse 配合是否可靠且高效?禁用“Rocket Loader”功能是否会最小化优势?

我曾考虑从 Stackpath 迁移到 Bunny CDN,但我论坛域名的所有者和前管理员拥有一个 Cloudflare 企业账户(他是 tinyURL 的所有者),他可以免费添加该域名(https://unicyclist.com/t/looking-for-a-decrease-in-the-forum-fees-in-the-future/274858/6)。

所以,我想知道使用 Cloudflare 而不是 Bunny CDN 是否正确、无痛且有效。

2 个赞

如果您的图片很多,那么直接增加固态硬盘的成本将远高于等效的 S3 成本。

S3 需要在其前面放置 CDN 才能具有成本效益,因为您需要按入口和出口付费,但这仍然比仅仅增加高速固态硬盘便宜得多。CloudFlare 可以放在 S3 前面,但资产缓存是唯一可以可靠使用的功能。将 CloudFlare 放在客户端和应用程序服务器之间也会增加跃点数并推高应用程序延迟。它不是万能药。

2 个赞

我们的图片量不大。内容分发网络(CDN)的目标是让我们的国际用户(主要是美国和欧洲用户)都能快速加载资源。

我们使用 S3,但仅用于存储备份。

1 个赞

快速提问。更换我们的 CDN 后是否必须重新烘焙?或者仅当 CDN URL 不同时才需要?或者根本不需要?


编辑:或者是否可以通过 Rails 脚本替换已烘焙帖子中的旧 URL 为新 URL?

例如,如果我有一个帖子包含:

<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;"
>

我能否进行搜索并用 newcdn.unicyclist.com 替换 f5i4i8k5.stackpathcdn.com 的字符串而不破坏任何东西?这似乎是安全的,但我想确保我所做的事情。

我不介意重新烘焙我所有的帖子,但第一次遇到的问题是,我有数千个嵌入的 YouTube 视频,在重新烘焙过程中,由于向 YouTube 发送了过多请求,我的 IP 被 YouTube 列入了黑名单。

所以,如果有一个比重新烘焙所有帖子更安全的方法,并且可以避免遇到同样烦人的问题,我宁愿选择这种替代方法……

3 个赞

有一个 rake 任务可以进行替换。我认为运行它很安全,也是个好主意。在所有帖子中替换字符串 应该就是你想要的。

2 个赞

嗨 Jay,

我不确定是否要使用 rake 任务。

CDN URL 似乎存在于 cooked 字段而不是 raw 字段中(除非消息是通过电子邮件发送的)。

据我所知,post:remap 只在 raw 字段中搜索字符串。

如果替换 rawcooked 字段中的 CDN URL 是安全的,我想我应该使用自定义的 rails 脚本?

2 个赞

你说得对。还有一个 . . . 东西 . . . 会替换数据库中所有字段中的一个字符串。那就是你要找的东西。抱歉,我现在找不到它了。有一些关于从一个 s3 迁移的话题,但似乎没有 #howto。

2 个赞

谢谢这个线索!

我猜是这个?

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

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

并在以下位置使用:

如果这是更改我们的 CDN 服务时,比重新烘焙更安全的选择,那么 howto 确实会很有用。

3 个赞

哦,太好了。感谢告知。我以为这足以让你找到它。很高兴你找到了。

3 个赞