2

I have a Django website where users post images for the whole community to see (kind of like 9gag).

I use Azure storage to save and serve the images. The webserver is an nginx reverse proxy + gunicorn cocktail. Gzip is up and running on my website Moreover, in order to cache the static assets, I have the following in my nginx conf file:

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { root /home/mhb11/project/myproject; expires 24h; add_header Vary Accept-Encoding; access_log off; }

My problem is that when I test my website with Google Page Speed plugin, I get told that none of the images being served from Azure storage are being cached: enter image description here

What do I do to enable caching for these? Please advise. I'm quite new to this, so any assist at this stage will be a big help. Thanks in advance, and have a nice weekend.


Here's the def _save method of my custom Storage class that uploads a blob:

def _save(self,name,content):
    blob_service = BlobService(account_name=accountName, account_key=accountKey)
    import mimetypes
    small_content = content
    content.open()
    content_type = None
    if hasattr(content.file, 'content_type'):
        content_type = content.file.content_type
    else:
        content_type = mimetypes.guess_type(name)[0]
    content_str = content.read()
    blob_service.put_blob(
        'pictures',
        name,
        content_str,
        x_ms_blob_type='BlockBlob',
        x_ms_blob_content_type=content_type
    )

How do I set Cache-Control within that?

Hassan Baig
  • 2,325
  • 12
  • 29
  • 48

1 Answers1

0

You will need to set the "Cache-Control" meta-tag info in Azure Storage/blob.

I use AWS and there I can go to the specific asset ( your images ) and specify that info.

Also; you should be able to specify this setting if you are using some kind of api to upload images from your application.

XP.
  • 174
  • 1
  • 5
  • my problem is, these are user-uploaded static assets that keep changing every few minutes. So I can't manually specify their Cache-Control from Azure. I'm using `from azure.storage.blob import BlobService` to upload these, and override django's own `Storage` class (code now included in the question). Is that where I need to asset Cache Control? Please advise. – Hassan Baig May 15 '16 at 10:36
  • Aws allows you to use urls that will fix things, assuming the img source has the same name; say you store the asset info in db; store the unix like timestamp and some random characters along side + radom number 999, 9999; the when you ask for the resource ask for it as ?rnd=6234626326 2323; this will make sure that it fetches the fresh copy every time you use that url; if that object changed and the name of the asset is same your "rnd" val;ue will change – XP. May 15 '16 at 19:42
  • Thanks for the AWS tip, although in my case, I need an Azure related solution. It's something I can set when uploading the image. – Hassan Baig May 16 '16 at 12:13