2016-01-22 52 views
10

我通過nginx的服務於單個頁面的JavaScript應用程序更換指紋文件服務器的時候,當我部署新的版本,我想迫使瀏覽器無效的JS緩存和請求過期在瀏覽器中緩存的資產/使用最新版本可用。通過nginx的

因此,舉例來說,當我更換了服務器的文件夾,命名爲my-app-8e8faf9.js一個文件,一個叫my-app-eaea342.js文件,我不希望瀏覽器拉離其緩存my-app-8e8faf9.js了。但是當沒有可用的新版本時,我仍然希望他們從緩存中讀取資源。

如何通過nginx配置實現此目的?這是我現有的配置:

server { 
    listen 80; 

    server_name my.server.com; 

    root /u/apps/my_client_production/current; 
    index index.html; 

    # ~2 seconds is often enough for most folks to parse HTML/CSS and 
    # retrieve needed images/icons/frames, connections are cheap in 
    # nginx so increasing this is generally safe... 
    keepalive_timeout 10; 
    client_max_body_size 100M; 

    access_log /u/apps/my_client_production/shared/log/nginx.access.log; 
    error_log /u/apps/my_client_production/shared/log/nginx.error.log info; 

    location/{ 
    try_files $uri $uri/ =404; 

    gzip_static on; 
    expires max; 
    add_header Cache-Control public; 

    } 

    # Error pages 
    error_page 500 502 503 504 /500.html; 
} 

回答

4

通過更改資產url來緩存緩存是正常的做法。

但是對於工作,你需要你的HTML文件不被永遠緩存,以便瀏覽器會有一些信息時,這些名稱更改。

因此,HTML和資產的分離位置。匹配器可以是不同的,這取決於你如何存儲它們,例如:

location/{ 
    try_files $uri $uri/ =404; 
    gzip_static on; 
    } 

location ^~ /assets/ { 
    gzip_static on; 
    expires max; 
    add_header Cache-Control public; 
    }