# 定义 DISCOURSE\_S3\_CDN\_URL 以链接 S3 CDN URL 中的资源

**URL:** <https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798>\
**Category:** Support\
**Created:** [2019年五月3日 21:44 UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798 "2019-05-03T21:44:07Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)\
**Post date:** [2020年三月4日 16:17 UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/16 "2020-03-04T16:17:47Z")

</div>

这里有一些相关信息，我上个月也处理过这个问题。

> [@Assets not serving from CDN in latest update](https://meta.discourse.org/t/assets-not-serving-from-cdn-in-latest-update/140705):
>
> Hello! I couldn’t figure out why in the latest update our installation stopped serving assets from the CDN. We are using Cloudfront and it was working good to serve both uploads and assets, but it looks like in the latest update assets are serving from the server instead. Another issue we are having is if we pass the variable DISCOURSE\_CDN\_URL, Discourse starts requesting assets that are not being generated or uploaded by the s3:upload\_assets task (stylesheets for example). This brea…

我的解决方法是同时设置两个变量（`DISCOURSE_S3_CDN_URL` 和 `DISCOURSE_CDN_URL`），并创建两个 CloudFront 分发：一个用于上传文件，源站为 S3 存储桶；另一个用于资源文件，源站为服务器。

以下是我们使用的代码：

- 以 S3 为源站的 CloudFront 分发：[infra/live/prod/services/discourse/cloudfront.tf at master · debtcollective/infra · GitHub](https://github.com/debtcollective/infra/blob/master/live/prod/services/discourse/cloudfront.tf#L18-L26)
- 以服务器为源站的 CloudFront 分发：[infra/live/prod/services/discourse/cloudfront.tf at master · debtcollective/infra · GitHub](https://github.com/debtcollective/infra/blob/master/live/prod/services/discourse/cloudfront.tf#L87-L98)

以下是我们的 app.yml（我们将其命名为 web.yml），我们在构建时替换变量：[infra/modules/services/discourse/web.yml at master · debtcollective/infra · GitHub](https://github.com/debtcollective/infra/blob/master/modules/services/discourse/web.yml)

---

_[View the full topic](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798)._
