Clarification for using whitenoise with CDN on digital ocean
BoPeng opened this issue · 2 comments
Python Version
3.11.2
Django Version
4.1.6
Package Version
6.4.0
Description
We currently use whitenoise to server static files on DigitalOcean (DO)'s app platform. Since DO Space offers CDN service, we are trying to add CDN support on top of whitenoise. Whereas the documentation is very clear on how to use CDN with cloudcloudfront
, I cannot make it work with DO by changing the configuration from
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATIC_URL = 'https://mycompany.com/static/'
to
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATIC_HOST = 'https://myspace.sfo2.digitaloceanspaces.com/myfolder'
STATIC_URL = STATIC_HOST + "/static/"
The problem is that python manage.py collectstatic
takes very long time to run and the static files are not uploaded to DO space.
My questions are:
-
With the new settings, when and how exactly does whitenoise upload the files to DO Space? As far as I can tell,
python manage.py collectstatic
put all files under the local staticfiles directory. Without these files uploaded, static files are not served with URLs likehttps://myspace.sfo2.digitaloceanspaces.com/myfolder/static/favicon.ico
. -
DO space uses a different URL for its CDN. For example, the CDN URL for
https://myspace.sfo2.digitaloceanspaces.com/myfolder/static/favicon.ico
would behttps://myspace.sfo2.cdn.digitaloceanspaces.com/myfolder/static/favicon.ico
. How would I configure the use of the latter URL? I tried to use thecdn
version ofSTATIC_HOST
and the files are not uploaded either.
I think I did not pay close attention to the Origin Domain Name part of the documentation. If I understand correctly, whitenoise + CDN works like this
- white noise continues to serve static files through
https://mycompany.com/static/
. This is whySTATICFILE_STORAGE
should not be changed. - The website tells the client to retrieve static files through CDN, say,
https://d4663kmspf1sqa.cloudfront.net/static
. - If the file has never been accessed before, the CDN server will grab its content from
origin domain name
, which ishttps://mycompany.com/static/
. After the first attempt, the file will be cached so subsequent access to static file will be handled by the CDN.
The problem with digital ocean, in my case, is that https://myspace.sfo2.cdn.digitaloceanspaces.com/myfolder/static/
will only serve files stored on https://myspace.sfo2.digitaloceanspaces.com/myfolder/static/
, so whitenoise will not work with my configuration. If I want to serve static files to CDN, I will have to use a boto3 storage to store static files to the bucket, or use cloudfront to point to the https://mycompany.com/static/
.
Yes, that is correct, thank you for your clear issue report and self-solve!