python – Caching static files in Django – Education Career Blog

I was profiling the performance of my web application using Google’s Page Speed plugin for Firebug and one of the things it says is that I should ‘leverage caching’ — “The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources”. When I dug deeper I found that all request for static files to the Django WSGI server lacked the Expires and the Cache-Control headers. Who should add these headers — should Django do it? If so, how?



Any static files you may have for your page should be served by your web server, e.g. Apache. Django should never be involved unless you have to prevent access of some files to certain people.

Here, I found an example of how to do it:

# our production setup includes a caching load balancer in front.
# we tell the load balancer to cache for 5 minutes.
# we can use the commented out lines to tell it to not cache,
# which is useful if we're updating.
<FilesMatch "\.(html|js|png|jpg|css)$">
ExpiresDefault "access plus 5 minutes"
ExpiresActive On
#Header unset cache-control:
#Header append cache-control: "no-cache, must-revalidate"

Leave a Comment