對靜態資源的 nginx 快取進行故障排除

對靜態資源的 nginx 快取進行故障排除

我有一個 Django 網站,用戶可以在其中發布圖像供整個社區查看(有點像 9gag)。

我使用 Azure 儲存體來保存和提供映像。網路伺服器是 nginx 反向代理+gunicorn 雞尾酒。 Gzip 已在我的網站上啟動並運行此外,為了快取靜態資產,我的 nginx conf 檔案中有以下內容:

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

我的問題是,當我使用 Google Page Speed 外掛程式測試我的網站時,我被告知 Azure 儲存體提供的所有圖像都沒有快取: 在此輸入影像描述

我該怎麼做才能啟用這些快取?請指教。我對此還很陌生,所以這個階段的任何幫助都會有很大的幫助。提前致謝,祝週末愉快。


這是def _save我的自訂 Storage 類別上傳 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
    )

如何在其中設定 Cache-Control?

答案1

您需要在 Azure 儲存體/blob 中設定「Cache-Control」元標記資訊。

我使用 AWS,在那裡我可以轉到特定資產(您的圖像)並指定該資訊。

也;如果您使用某種 api 從應用程式上傳圖像,您應該能夠指定此設定。

相關內容