简体   繁体   中英

Nginx caching with Azure Storage Blob

Local caching works fine, however trying to cache files server by the Azure Blob Storage is not working.

Each request the Signature changes and it returns no caching headers. I think its because of this signature at the end? Anyone got an idea how to surpass that?

https://accountname.blob.core.windows.net/static/assets/js/jquery331.min.js?se=2020-02-10T15%3A06%3A35Z&sp=r&sv=2019-09-28&sr=b&sig=<Signature>

Below is my nginx config. It mostly based on this one = https://serversforhackers.com/c/nginx-caching

upstream app {
    server web:8000;
}

proxy_cache_path ./cache keys_zone=one:10m loader_threshold=300 loader_files=200;
proxy_cache_key "$scheme$request_method$host$request_uri";



# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/css                   max;
    application/javascript     max;
    ~image/                    max;
}

server {

    include mime.types;

    listen 80;
    listen [::]:80;
    root /usr/src/application/;
    index /usr/src/application/main/;

    gzip on;
    gzip_comp_level 2;
    gzip_http_version 1.0;
    gzip_proxied any;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript script text/script;
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    gzip_vary on;


    location / {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;

    }

    location /staticfiles/ {
        proxy_cache one;
        proxy_buffering on;
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_http_version      1.1;
        proxy_set_header        Connection "";
        proxy_set_header        Authorization '';
        proxy_set_header        Host https://accountname.blob.core.windows.net/static;
        proxy_pass              https://accountname.blob.core.windows.net;
    }

    location /media/ {
        proxy_http_version      1.1;
        proxy_set_header        Connection "";
        proxy_set_header        Authorization '';
        proxy_set_header        Host https://accountname.blob.core.windows.net/media;
        proxy_pass              https://accountname.blob.core.windows.net;
    }

    # cache.appcache, your document html and data
    location ~* \.(?:manifest|appcache|html?|xml|json)$ {

      expires -1;
    }

    # Feed
    location ~* \.(?:rss|atom)$ {


      expires 1h;
      add_header Cache-Control "public";
    }

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {


      expires 1M;
      access_log off;
      add_header Cache-Control "public";
    }

    # CSS and Javascript
    location ~* \.(?:css|js)$ {


      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }

  location /media/ {

    alias /usr/src/application/main/media;
  }

    proxy_connect_timeout 300s;
    proxy_read_timeout 300s;
    proxy_next_upstream error timeout http_502;
    location /index/ {
        alias /usr/src/application/main/;
    }
}

So I've found what caused the query strings. We are using django storages, in that the option for a variable AZURE_URL_EXPIRATION_SECS is possible which appends a query string. As of now caching works without CDN. However, I am surprised that with the CDN active and rules set to ignore query strings no caching was happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM