# Defining DISCOURSE\_S3\_CDN\_URL links to assets in 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:** [May 3, 2019, 9:44pm 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:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 3, 2019, 9:44pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/1 "2019-05-03T21:44:07Z")

</div>

I had this problem before and decided that I was crazy, confused, or the database on the site was suspect, but this is on a brand new site. Also, I was on Digital Ocean spaces, so I thought it might be a problem somehow.

I’m trying again to figure out how to keep images on AWS S3 like I think the Big Boys do.

Here’s what I have in the `env` section:

```plaintext
  DISCOURSE_S3_ACCESS_KEY_ID: 'key'
  DISCOURSE_S3_SECRET_ACCESS_KEY: 'lock'
  DISCOURSE_BACKUP_LOCATION: 's3'
  DISCOURSE_S3_BUCKET: 'lc-xyz'
  DISCOURSE_S3_BUCKET_NAME: 'lc-xyz'
  DISCOURSE_S3_BACKUP_BUCKET: 'lc-xyz/backups'
  DISCOURSE_S3_UPLOAD_BUCKET: 'lc-xyz/uploads'
  DISCOURSE_S3_CDN_URL: 'https://lc-rbx.s3.amazonaws.com'

```

When I include the `s3 cdn url` the site breaks because all of the links to the assets are on S3 like `https://lc-xyz.s3.amazonaws.com/assets/plugin-third-party-01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b.br.js`.

> [@Digital Ocean Spaces don’t implement the AWS S3 API for the CORS rule](https://meta.discourse.org/t/digital-ocean-spaces-don-t-implement-the-aws-s3-api-for-the-cors-rule/111736/2):
>
> Why do you need to push assets to S3? Can’t they be served from a push CDN like normal? Uploads assets means JS files not uploads.

Because if you define `s3 cdn url` Discourse looks for the assets on the s3\_cdn\_url.

I did a rebuild, but the assets are still missing. I can do a

```
rake s3:upload_assets

```

Is there an `after_bundling_assets` stanza that I could add that to? (I know about `after_db_migrate` and `after_bundle_exec`, but don’t know if those would work.)

Do assets get produced some other time? (It would seem like when themes get modified assets would change.)

If I also had a “push CDN like normal”, would that keep this from happening?

Is there some best practice that I’m missing?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [May 3, 2019, 9:50pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/2 "2019-05-03T21:50:48Z")

</div>

> [@pfaffman](#):
>
> Is there an `after_bundling_assets` stanza that I could add that to?

You can use `after_assets_precompile` as the hook for that rake task.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 3, 2019, 9:59pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/3 "2019-05-03T21:59:00Z")

</div>

Aha! Thanks very much for that. Where does the list of those things live?

I’m still confused why this is necessary, especially since you seemed confused before. (Or maybe I should be happy that I can push all this stuff to S3, now that I know how to do it–And I think that it was you who said that one could use CloudFlare’s free CDN to front an AWS bucket.)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 10, 2019, 5:47pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/4 "2019-09-10T17:47:31Z")

</div>

I’m trying this again. I’ve got uploads going to s3. That works.

I configured cloudflare to CDN the site. If I enter `https://lc-XXX.literatehosting.com/uploads` in the `s3_cdn_url` site setting, new uploads go to the S3 bucket and the image is linked to the CDN url and it works.

**BUT** if I try to set the `s3_cdn_url` with an ENV in `app.yml` (or edit `config/discourse.conf` by hand and `sv restart unicorn`, all of the assets are loaded from S3 (where they aren’t). That might be OK, but if I try to `rake s3:upload_assets`, it complains that S3 isn’t configured.

---

<div class="post-metadata">

### Author: ![LeoMcA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leomca/32/87233_2.png) [@LeoMcA](https://meta.discourse.org/u/LeoMcA)
#### Post date: [September 16, 2019, 1:21pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/5 "2019-09-16T13:21:56Z")

</div>

I happened across this while [figuring out some clashing settings names](https://github.com/discourse/discourse/pull/8061#issuecomment-531770779).

I _think_ (and **hope** , because it means I understand this and didn’t write absolute nonsense in the thread linked above) that if you remove these two lines:

```plaintext
DISCOURSE_S3_BUCKET: 'lc-xyz'
DISCOURSE_S3_BUCKET_NAME: 'lc-xyz'

```

Then you can set `DISCOURSE_S3_CDN_URL`, and it will only be used for uploads, not assets.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 29, 2019, 6:48pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/6 "2019-09-29T18:48:40Z")

</div>

> [@LeoMcA](#):
>
> I _think_ (and **hope** , because it means I understand this

I think that you do understand, and, thanks to you, I’m at least closer to understanding myself! Thanks very much!

---

<div class="post-metadata">

### Author: ![timkelty](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/timkelty/32/158090_2.png) [@timkelty](https://meta.discourse.org/u/timkelty)
#### Post date: [October 11, 2019, 3:06am UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/7 "2019-10-11T03:06:43Z")

</div>

Running into this and still very confused after reading this thread…

I’m trying to server s3 uploads (not compiled assets) from CDN (Cloudfront).

If I configure “s3 cdn url” via the settings screen, it works as expected (well…except for [System upload not using s3 cdn url](https://meta.discourse.org/t/system-upload-not-using-s3-cdn-url/100729))

However, if I configure via `DISCOURSE_S3_CDN_URL` and rebuild, the frontend is broken because its trying to load compiled assets from my s3 cdn url.

Seems `DISCOURSE_S3_CDN_URL` / `s3_cdn_url` should only affect uploads, and DISCOURSE\_CDN\_URL should only affect assets.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 11, 2019, 8:30am UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/8 "2019-10-11T08:30:03Z")

</div>

That is my experience too. I ended up making a plugin to set the S3\_CDN\_URL.

---

<div class="post-metadata">

### Author: ![timkelty](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/timkelty/32/158090_2.png) [@timkelty](https://meta.discourse.org/u/timkelty)
#### Post date: [October 11, 2019, 11:34am UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/9 "2019-10-11T11:34:42Z")

</div>

@pfaffman seems like this should be filed as a bug then, yes?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 11, 2019, 2:19pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/10 "2019-10-11T14:19:05Z")

</div>

Perhaps. It’s not a feature likely to be used by normal self hosters,so it’s going to be a low priority. Also, I think that there is soon to be a change in how global settings and shadowed by global work, so it’ll likely get worked out then.

---

<div class="post-metadata">

### Author: ![wanghaisheng](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wanghaisheng/32/163707_2.png) [@wanghaisheng](https://meta.discourse.org/u/wanghaisheng)
#### Post date: [January 4, 2020, 2:01pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/11 "2020-01-04T14:01:02Z")

</div>

seems the same issue turn me down here  
@pfaffman need your help  
do you use cloudfront to achieve the custom domain cdn url or just use the public prefix of bucket objects in cdn url

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 11:04am UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/12 "2020-03-04T11:04:21Z")

</div>

I’ve just hit this. @pfaffman did you ever get this resolved?

I’ve got uploads working by setting the `s3_cdn_url` manually in site settings, but ideally need to be able to set the ENV variable globally when deploying. When I do, I get the same issue, that Discourse tries to look for assets at the `s3_cdn_url` which seems like a bug to me in [discourse/lib/content\_security\_policy/default.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/master/lib/content_security_policy/default.rb#L36)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [March 4, 2020, 1:39pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/13 "2020-03-04T13:39:49Z")

</div>

I think that I just set the value in the database.

My guess is that the solution is to find the rake task that will push the assets to S3.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 2:05pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/14 "2020-03-04T14:05:54Z")

</div>

I was wondering if any of the [@team](https://meta.discourse.org/groups/team) could confirm whether this is expected behaviour, that you can’t use an S3 CDN (set globally) for uploads without Discourse also looking for assets there too. It seems like unexpected behaviour unless I’m misunderstanding something.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [March 4, 2020, 3:58pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/15 "2020-03-04T15:58:56Z")

</div>

Yes, that is the behavior.

Setting up S3 CDN will use it for every thing that would be a static file, be it uploads or JS assets.

---

<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: [March 4, 2020, 4:17pm 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>

Here’s some information about this, I was dealing with this last month.

> [@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…

I fixed it by setting both variables (`DISCOURSE_S3_CDN_URL` and `DISCOURSE_CDN_URL`) and creating two CloudFront distributions, one for the uploads with origin the S3 bucket, and one for the assets with origin the server.

Here’s the code we use for this

- Cloudfront distribution with S3 as origin [https://github.com/debtcollective/infra/blob/master/live/prod/services/discourse/cloudfront.tf#L18-L26](https://github.com/debtcollective/infra/blob/master/live/prod/services/discourse/cloudfront.tf#L18-L26)
- Cloudfront distribution with Server as origin [https://github.com/debtcollective/infra/blob/master/live/prod/services/discourse/cloudfront.tf#L87-L98](https://github.com/debtcollective/infra/blob/master/live/prod/services/discourse/cloudfront.tf#L87-L98)

Here’s our app.yml (we named it web.yml), we replace variables at build time [https://github.com/debtcollective/infra/blob/master/modules/services/discourse/web.yml](https://github.com/debtcollective/infra/blob/master/modules/services/discourse/web.yml)

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 4:25pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/17 "2020-03-04T16:25:17Z")

</div>

Amazing. Thanks very much. There doesn’t appear to be much documentation around this at all, and the settings imply that you can just use s3 for uploads only, which I think is what’s catching us all out.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [March 4, 2020, 4:27pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/18 "2020-03-04T16:27:56Z")

</div>

It is in my list to write a #howto on this. I hope to do it by the week end.

---

<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: [March 4, 2020, 4:29pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/19 "2020-03-04T16:29:16Z")

</div>

Yeah, once you understand you need two CloudFront distributions, makes a bit more sense. Also, remeber to upload assets to s3 after each rebuild. There’s an issue with upgrades from Docker Manager where you need to run `bundle exec rake s3:upload_assets` manually inside the docker container. If you do a rebuild instead it should work if you add these lines to your app.yml

```plaintext
hooks:
  after_assets_precompile:
    - exec:
        cd: $home
        cmd:
          - 'bundle exec rake s3:upload_assets'

```

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 4, 2020, 4:57pm UTC](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/20 "2020-03-04T16:57:00Z")

</div>

That task is complaining that I don’t have S3 configured. I’ve narrowed it down to this method in `global_setting.rb`:

```ruby
def self.use_s3?
    (@use_s3 ||=
      begin
        s3_bucket &&
        s3_region && (
          s3_use_iam_profile || (s3_access_key_id && s3_secret_access_key)
        ) ? :true : :false
      end) == :true
  end

```

Where is `GlobalSetting.s3_bucket` expected to be defined? It looks to me like we need to set `DISCOURSE_S3_UPLOAD_BUCKET` and `DISCOURSE_S3_BUCKET` environment variables. What is the difference between the two?

[Next page](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798.md?page=2)
