# CloudFront not caching static files

**URL:** https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818
**Category:** Self-hosting
**Created:** [February 22, 2019, 12:43pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818 "2019-02-22T12:43:05Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![soke](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@soke](https://meta.discourse.org/u/soke)
#### Post date: [February 22, 2019, 12:43pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/1 "2019-02-22T12:43:05Z")

</div>

Hi.

I have setup a discourse version v2.3.0.beta2 installation on AWS.

This attachment shows how I have it deployed. I’m using the official docker image installation on each instance of the auto scaling group

 ![diagram](https://global.discourse-cdn.com/meta/original/3X/d/1/d12ba1f9504b8f7590b857c8dc92af405e25a7c1.png)

I have 2 CloudFront distributions  
One ([cdn.civicxclub.com](http://cdn.civicxclub.com)) pointing to the S3 buckets for serving the uploaded images directly from the CDN. THIS WORKS.  
The other one ([civicxclub.com](http://civicxclub.com)) pointing to the balancer, in order to serve the app and all the static files from it. THIS DOESN’T WORK  
This is the behaviours configuration that I have:

 ![image](https://global.discourse-cdn.com/meta/original/3X/0/2/028d24872bdb5b913c48b9a13247b92b05615970.png)

The default one has Cache Based on Selected Request Headers option set to **All** in order to not allow caching from the backend.  
All the others has Cache Based on Selected Request Headers set to \*_Whitelist_ and I whitelist **Host, Origin and Referer** headers.

When I access the web from the browser and inspect headers from for example an avatar It always says Miss from cloudfront, but when I access the file twice directly from the brownser, it says Hit from cloudfront, so the caching is working, but not when loading files from the forum web page.

The Discourse CDN Variable is set:

```plaintext
  DISCOURSE_CDN_URL: https://civicxclub.com

```

Any thoughts?

Thanks in advance, regards.

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [February 22, 2019, 1:02pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/2 "2019-02-22T13:02:31Z")

</div>

You’re doing it wrong!

You need to split the container into Data and Web or you can use RDS for Postgresql to host data and load-balance among the web containers.

~~Also, It is not recommended to use cloudflare optimizations as they cause many issues.~~

> [@soke](#):
>
> DISCOURSE\_CDN\_URL: [https://civicxclub.com](https://civicxclub.com)

And You need to set up your cloudfront URL in the discourse CDN field not the name of the server.

---

<div class="post-metadata">

### Author: ![soke](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@soke](https://meta.discourse.org/u/soke)
#### Post date: [February 22, 2019, 1:09pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/3 "2019-02-22T13:09:37Z")

</div>

I deploy the web only container on the EC2 servers. The database is outside, on an RDS instance, as well as the Redis.

> [@itsbhanusharma](#):
>
> And You need to set up your cloudfront URL in the discourse CDN field not the name of the server.

I have a CloudFront for uploads (the setting is on admin panel, and it works) and other for serving the whole webpage.

Now, I’m looking into the static headers, and it seems to work, some files yes, some don’t.

---

<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: [February 22, 2019, 2:25pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/4 "2019-02-22T14:25:31Z")

</div>

> [@soke](#):
>
> DISCOURSE\_CDN\_URL: [https://civicxclub.com](https://civicxclub.com)

DISCOURSE\_CDN\_URL should be different from the DISCOURSE\_HOSTNAME. Use a dedicated URL for the CDN.

Also, in our experience making all traffic go into the cloudfront tunnel increases the latency.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 23, 2019, 5:51am UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/5 "2019-02-23T05:51:14Z")

</div>

> [@itsbhanusharma](#):
>
> Also, It is not recommended to use cloudflare optimizations as they cause many issues.

Nobody said anything about cloudflare. Read more closely?

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [February 23, 2019, 6:23am UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/6 "2019-02-23T06:23:26Z")

</div>

Maybe I mis-read title as cloudflare.

---

<div class="post-metadata">

### Author: ![soke](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@soke](https://meta.discourse.org/u/soke)
#### Post date: [February 25, 2019, 4:17pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/7 "2019-02-25T16:17:09Z")

</div>

> [@Falco](#):
>
> DISCOURSE\_CDN\_URL should be different from the DISCOURSE\_HOSTNAME. Use a dedicated URL for the CDN

How can I then serve avatars, css and js files from this CDN if they are stored on the server??  
I’ve tried to use the [cdn.civicxclub.com](http://cdn.civicxclub.com) url which is mapping to the S3 bucket and add all the behaviours related to discourse static content, and it failed.

---

<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: [February 25, 2019, 5:40pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/8 "2019-02-25T17:40:04Z")

</div>

Please read our CDN guide:

> [@Enable a CDN for your Discourse](https://meta.discourse.org/t/enable-a-cdn-for-your-discourse/14857):
>
> So you’d like to use a CDN to speed up worldwide access to common assets on your Discourse forum? You might want to go all-in and [Set up file and image uploads to S3](https://meta.discourse.org/t/setting-up-file-and-image-uploads-to-s3/7229) (see also [Configure an S3 compatible object storage provider for uploads](https://meta.discourse.org/t/configure-an-s3-compatible-object-storage-provider-for-uploads/148916)). If you want a CDN without the added complexity of S3 and having two CDNs, this is the guide for you. The CDN will reduce some load on your server by letting the CDN serve those assets so that the server doesn’t have to. It also can speed things up by deliver…

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [March 27, 2022, 4:26pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/9 "2022-03-27T16:26:43Z")

</div>



---

<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 27, 2022, 7:18pm UTC](https://meta.discourse.org/t/cloudfront-not-caching-static-files/109818/10 "2022-03-27T19:18:38Z")

</div>


