CloudFront not caching static files

סיכום מבוסס בינה מלאכותית

The user soke is troubleshooting CloudFront caching issues for static files in a Discourse v2.3.0.beta2 deployment on AWS. They have two CloudFront distributions: one for S3-hosted uploads (working) and another for the web app (not caching static files properly). Despite CloudFront showing ‘Hit’ on repeated requests, files loaded from the forum page return ‘Miss’, indicating caching isn’t working as expected. The DISCOURSE_CDN_URL is set to https://civicxclub.com, but itsbhanusharma suggests using a dedicated CDN URL instead of the server name. Falco reinforces this, advising a separate CDN URL and referencing the official guide here. soke questions how to serve static content from a CDN when files are stored on the server, while codinghorror corrects a misinterpretation about Cloudflare. The discussion highlights configuration issues related to CDN setup and header settings in CloudFront.

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

I have 2 CloudFront distributions
One (cdn.civicxclub.com) pointing to the S3 buckets for serving the uploaded images directly from the CDN. THIS WORKS.
The other one (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:

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:

  DISCOURSE_CDN_URL: https://civicxclub.com

Any thoughts?

Thanks in advance, regards.

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.

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

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

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.

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.

Nobody said anything about cloudflare. Read more closely?

Maybe I mis-read title as cloudflare.

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 url which is mapping to the S3 bucket and add all the behaviours related to discourse static content, and it failed.

Please read our CDN guide: