为您的 Discourse 启用 CDN

那么您想使用内容分发网络 (CDN) 来加快全球用户访问您的 Discourse 论坛上的常用资源的访问速度吗?您可能希望完全投入并参考 https://meta.discourse.org/t/setting-up-file-and-image-uploads-to-s3/7229(另请参阅 https://meta.discourse.org/t/configure-an-s3-compatible-object-storage-provider-for-uploads/148916)。

如果您想要一个没有 S3 带来的额外复杂性并且不需要使用两个 CDN 的 CDN,那么本指南适合您。CDN 将通过让 CDN 为这些资源提供服务来减轻您服务器的部分负载,从而无需服务器处理。它还可以通过从更靠近最终用户的位置交付文件来提高速度。

请注册您选择的 CDN——CDCK/discourse.org 已经使用并强烈推荐 Fastly

配置将如下所示:

您需要查找三个主要设置:

  1. 源地址 (origin address),即您的论坛当前所在的 URL,例如 discourse.example.com。这是 CDN 在首次请求时从中获取所有原始内容的位置。

  2. CNAME,即您将在 DNS 中设置的 CDN 的“友好”名称,例如 discourse-cdn.example.com

  3. CDN URL,即缓存的 CDN 资产将通过 CDN 的全球分布式服务器网络提供的“不友好”名称。它看起来像 637763234.cdn-provider.com

您需要编辑您的 DNS,将 CNAME 映射到 CDN URL,如下所示:

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

(编辑 DNS 后,请给它一点时间进行传播。)

设置中实际的 Discourse 部分相当简单。取消注释 app.yml 中的 CDN 行,并用您刚刚在 DNS 中设置的 CNAME 更新它:

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

:warning: CDN URL 必须包含协议(https://http://)。 不再支持协议相对 URL(以 // 开头),这将导致容器构建中止。

(如果您在 app.yml 中看不到此行,请将其添加到其他 DISCOURSE_ 变量下方)

与对 app.yml 的任何其他更改一样,您需要重建容器以反映更改:

./launcher rebuild app

重建后,在浏览器中访问您的 Discourse 实例。查看源代码并搜索“cdn”。您将看到网站资产现在来自您的 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>

本主题涵盖了更常见静态资源加速的场景。有关完全站点的(动态和静态资源)CDN 加速(如 Cloudflare 提供的),请参阅此主题。Cloudflare 与传统 CDN 的工作方式不同,因此本主题不适合讨论 Cloudflare。

据报告可用的其他 CDN 包括:

43 个赞
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?
How to add AWS Cloudfront as a Discourse CDN
How to solve super slow loading time for site?
Varnish config for discourse
S3 image bandwidth costs are getting annoying
Discourse load balancer encounters problems, hoping to help
Is Hetzner Object Storage and Bunny CDN Supported?
I can't login to admin after backup due to Cloudflare
How to install discourse without ssl?
How to add AWS Cloudfront as a Discourse CDN
Conserving unicorns by serving images with nginx?
Is Hetzner Object Storage and Bunny CDN Supported?
Optimizing Site For Speed - How To Improve?
Latest commits in v3.3.0.beta3-dev seems to break Discourse
How to make Discourse faster for image-heavy communities?
Using Discourse with Cloudflare: Best Practices
Minimum server size based on backup?
Understanding Uploads, Images, and Attachments
Uploads Subdomain For Cloudflare
Discourse & Cloudflare
Mobile app URL issues
Forum does not work
Should I load third-party libraries from vendor or cdn?
Forum not working after configuring CDN (ERR_CERT_COMMON_NAME_INVALID)
Blank login and signup page
My discourse speed is very slow
MKJ's Opinionated Discourse Deployment Configuration
How might we better structure #howto?
Change image folder to symlinked folder

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.

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: http://docs.fastly.com/guides/getting-started/cname-instructions-for-most-providers

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

7 个赞

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…

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

This should also work with other CDNs? Like S3?

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

5 个赞

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.

1 个赞

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.

1 个赞

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.

8 个赞

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. https://www.cachefly.com/plans-pricing/pay-as-you-go-cdn/

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

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

2 个赞

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.

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: https://www.keycdn.com/support/create-a-pull-zone/

  3. Create a Zonealias: https://www.keycdn.com/support/create-a-zonealias/

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

7 个赞

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

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?

No, that is the only way.

1 个赞

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

3 个赞

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.

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